Lecture01 BasicPython
Lecture01 BasicPython
18 + 69
add(18, 69)
add ( 18 , 69 )
Operator Operand Operand
30 15
add add(6, mul(4, 6)) mul(3, 5)
24
add 6 mul(4, 6) mul 3 5
This is called an
expression tree.
mul 4 6
Names
Names
result1 = x * y
result2 = x + y
💬 Will that code cause an error? If not, what will my_name store?
It will not error (similar code in other languages might, however). The
name my_name is now bound to the value 'Pamelaella'.
Functions
Defining functions
The first line is called the function signature, all lines after are
considered the function body.
def <name>(<parameters>): # Function signature
return <return expression> # Function body
x = 1
y = 2
add(x, y)
x = 3
add(x * x, x + x)
Return values
sum = add(2, 4)
Reminder: You can use function calls in expressions:
big_sum = add(200, 412) + add (312, 256)
...and nest function calls inside function calls:
huge_sum = add(add(200, 412), add (312, 256))
Spot the bug #1
sum = add(2, 4)
The code after the return statement will not be executed, that line
belongs before the return.
Spot the bug #2
sum = add(2, 4)
sum = add(2, 4)
The function body does not return any value. However, the code that
calls it tries to use the result of the expression. It should have a
return statement that returns the sum.
The None value
name = 'sosuke'
Assignment statement greeting = 'ahoy, ' + name
def greet(name):
Def statement return 'ahoy, ' + name
<separating header>:
<statement> # SUITE
<statement>
...
Execution rule for a sequence of statements:
Execute the first statement
Unless directed otherwise, execute the rest
Conditional Statements
A simple conditional:
clothing = "shirt"
clothing = "shirt"
if temperature < 0:
clothing = "snowsuit"
elif temperature < 32:
clothing = "jacket"
The else statement
if temperature < 0:
clothing = "snowsuit"
elif temperature < 32:
clothing = "jacket"
else:
clothing = "shirt"
Conditional statements summary
if num < 0:
sign = "negative"
elif num > 0:
sign = "positive"
else:
sign = "neutral"
Syntax tips:
Always start with if clause.
Zero or more elif clauses.
Zero or one else clause, always at the end.
Execution of conditional statements
num = 5
if num < 0:
sign = "negative"
elif num > 0:
sign = "positive"
else:
sign = "neutral"
Conditionals in functions
get_number_sign(50) # "positive"
get_number_sign(-1) # "negative"
get_number_sign(0) # "neutral"
Returns inside conditionals
get_number_sign(50) # "positive"
get_number_sign(-1) # "negative"
get_number_sign(0) # "neutral"
While loops
While loops
The code is significantly shorter than writing all print statements, and
it can easily be extended to loop for more or less iterations.
Using a counter variable
Uh oh …
counter = 1
while counter < 5:
total += pow(2, counter)
What one line of code would fix this?
counter += 1
counter = 6
while counter > 5:
total += pow(2, counter)
counter += 1
How do we save this code?
Intentions are unclear! Change the initial value and condition?
Execution of loops
sum_up_squares(1, 5)
The break statement