Lecture 4 - Controlling The Execution and Modularization of Python Programs
Lecture 4 - Controlling The Execution and Modularization of Python Programs
COMPUTING
# Global scope
s = “Hello world"
f()
ASSIGNMENTS (1/2)
Syntax: <variable> = <expr>
<variable> = eval (input (<prompt>))
Interpretation:
– The expression is evaluated to produce a data value, which is
then associated with the name:
>>> a = 5 * 2
– The prompt does not participate in the evaluation and eval
evaluates the input as expression
>>> height = eval(input("Your height please: ")) # try to type in expression: 5*2
• The assignment determines the data type
>>> pi=3.14 # pi is a number
>>> pi="three fourteen" # pi is a string
– Note: for a variable, assignments can happen multiple times,
therefore types can be changed as shown above.
ASSIGNMENTS (2/2)
• Python supports simultaneous assignment.
Example-1: a, b = X+Y, X-Y
Syntax: <var-1> , <var-2> … = <expr-1> , <expr-2>…
Interpretation: Each of the variables <var> in the list on LHS are
associated with the result of the evaluation of an expressions
<expr> in the same position on RHS
• Example-2:
>>> height, weight=eval(input("Your height and weight: "))
Your height and weight: 1, 2
>>> print("Your height=", height," and weight=",weight)
CONDITIONS
Operation Example
if a>5 or a<-5:
x or y
print("Hello World")
if a>5 and a<-5:
print("Hello World")
x and y
else:
print("Sorry World")
If not(a>5):
not x
print("Hello World")
LOOPS - FOR
Syntax: for <var> in <sequence> :
<expression>
Interpretation: repeat the <expression>
with <var> is assigned by the values of
<sequence> each time
Example: ranging over listed sequence of values
DEFINITE LOOPS