What is a function?
Something that recieves and input, does something to it, and gives an output.
Example
\(f(x) = x + 2\) is a function
In Python it looks like:
def f(x):
return x + 2
“return” exits the program and outputs \(x+2\)
Something that recieves and input, does something to it, and gives an output.
\(f(x) = x + 2\) is a function
In Python it looks like:
def f(x):
return x + 2
“return” exits the program and outputs \(x+2\)
A conditional checks some condition (expressed as a boolean) and uses it to decide what the program should do.
example:
input: boolean x
if x:
y = x + 1
else:
y = x + 2
return y
example:
input: boolean x
if not x:
y = x + 2
else:
y = x + 1
return y
A loop lets you look at every element in a list and do something each time.
example:
for x in [1,2,3,4]:
print x + 5
Prints out \(6,7,8,9\)
example:
x = 1
while x < 5:
print x + 5
x = x + 1
Also prints out \(6,7,8,9\)