normal division always return a float
integer division always return an integer
A function need not use every parameter passed into it.
A function without any parameters or return value is still valid.
Functions do not necessarily need a return value.
Functions
1. Indexing:
cs = '1010'
print(cs[0] * 4)
>> '1111'
recursive
recursive function
o
a function that calls itself
o
requires two cases
-
base case — terminating condition
recursive case — function that calls itself to solve a similar but simpler problem
def f(x):
if x == 0:
return x
else:
return f(x // 2) + f(x // 2) + f(x // 2)
for f(33), function f is called 1093 times.
o
for each input that is not 0 to f, there will be 3 more calls to f.
o
with an input of 33, there will be 3 calls to f(16)
o
we will have 3 calls to f(8) from each f(16)
o
we can calculate the total number of calls as: 1 + 3 + 3² + 3³ + 3⁴ + 3⁵ + 3⁶ =
1093
o
linear recursive function One recursive call per function call. The recursive
call is usually followed (or preceded) by some simple computation (like
multiplication or addition). The function calls form a single path, like a linked
list or chain. It typically processes data in a sequential or step-by-step
fashion
o
def factorial(n):
o
if n == 0:
o
o
return 1
return n * factorial(n - 1)
each function call leads to one recursive call
linear chain of calls factorial(3) → factorial(2) → factorial(1) →
factorial(0)
o
non-linear recursive function A non-linear recursive function is one that
makes more than one recursive call per invocation, and these calls may
involve combining results in a non-linear way (e.g., multiplication,
exponentiation, etc.).
o
def f(x, y):
o
if x == 1:
o
return y
o
else:
o
return f(x // 2, y) + f(x // 2, y)
THIS IS A NON-LINEAR RECURSIVE FUNCITION
Non-Linear Recursive Functions are recursive functions that call
itself more than one time in at least one of the branches.
makes 2 recursive calls in 1 go
o
def f(x, y):
o
if x == 1:
o
return y
o
else:
o
z = f(x // 2, y)
o
return z + z
NOT A NON-LINEAR RECURSIVE FUNCTION
this function only makes one recursive call per execution
z = f(x // 2, y)
recursive call is stored in z and doubles the value through z + z
iteration
loop. does not call itself.
x = 13
while x % 5 != 0:
x += 1
if x % 3 == 0:
break
x *= 3
cycle breaks when x = 390 as it becomes not divisable by 5.
the break is never triggered. the loop exits naturally when x = 390.
cs = "1010"
while (cs >= "1010"):
temp = cs[0] * 4
if temp < "1010":
cs = temp[0]
break
else:
cs = temp * 2
there will be an infinite loop. temp is first set to "1111", and "1111" > "1010" due to
lexicographical ordering. cs is subsequently set to "11111111", and "11111111" >
"1010" again. Therefore, break is never reached, and the while loop never terminates,
resulting in infinite loop.
nesting loops
for y in range(1,10):
print(y, end = "")
result: 123456789
for y in range(1,10):
print(y)
result:
1
2
3
4
5
6
7
8
9
By default, print() ends with \n. But because of end = “”, it tells python to print the next thing
right after it, with no line in between.