Python
/
Functions
- 1 Language 9
-
Hello World S
-
Variables S
-
Functions S
-
Conditional A S
-
Operators S
-
While S
-
Turtle S
-
Script Mode S
-
Debugging S
- 2 Strings 7
-
Slice S
-
Raw Strings S
-
Regex A S
-
Validation S
-
Config S
-
Security S
-
Encrypt A S
- 3 Collections 5
-
Lists S
-
Dictionaries S
-
Efficiency S
-
Tuples S
-
References S
- 4 Functions 5
-
Recursion S
-
Factorial S
-
Modulus S
-
Reassignment S
-
Approximate S
- 5 Storage 8
-
Files S
-
Databases S
-
Pipes S
-
With open S
-
Shelve A S
-
Zip S
-
Csv S
-
Json S
- 6 Class 4
-
Definition S
-
Attributes S
-
Functional S
-
Methods S
- 7 Goodies 5
-
Conditional Expression S
-
List Comprehension A S
-
Generator S
-
Named Tuple S
-
Modules S
- 8 Applications 5
-
Pythagora A S
-
Palindrome A S
-
Binary Search A S
-
Conway Game A S
-
Coin Flip A S
- 9 Scheduler 4
-
Time S
-
Multithreading A S
-
Subprocess S
-
Logging S
- 10 Packages 6
-
Clipboard A S
-
Ocr A S
-
Socket S
-
Image S
-
Virtualenv S
-
Jupyter S
S
R
Q
Python Functions Factorial
0! = 1 n! = n(n-1)! def f(n): if (n==0): return 1 n = n * f(n-1) retur n
Factorial
p110 The definition of the factorial number is that 0! is 1, and n! = n(n-1)!
# Factorial definition:
#
# 0! is 1
# n! = n(n-1)!
#
# 3! is 3 times 2!, which is 2 times 1!, which is 1 times 0!.
# 3! = 3 * 2! = 3 * 2 * 1! = 3 * 2 * 1 * 0! = 6
def factorial(n):
if (n == 0): return 1
n = n * factorial(n-1)
return n
assert factorial(3) == 6
assert factorial(0) == 1
assert factorial(4) == 24
Fibonacci
p113 Each number is the sum of the two preceding ones.
# Fibonacci sequence:
#
# fb(0) = 0
# fb(1) = 1
# fb(n) = fb(n-1) + fb(n-2)
#
# Each number is the sum of the two preceding ones.
def fibonacci(n):
if (n==0): return 0
if (n==1): return 1
return fibonacci(n-1) + fibonacci(n-2)
assert fibonacci(0) == 0
assert fibonacci(1) == 1
assert fibonacci(2) == 1
assert fibonacci(3) == 2
assert fibonacci(4) == 3
assert fibonacci(5) == 5
➥ Questions
Last update: 60 days ago