Lecture3 Basic Programming
Lecture3 Basic Programming
and => both should meet the criteria simultaneously or => either one condition meet the criteria
Basics: Program flow
Make
thin
dough
Wrap up
Linear flow with
or ingredient
Sequential flow
Steam
and
enjoy!
Basics: Program flow
Different result
(Sequence is important!)
Basics: Branching
=SUM(IF((B:B=Month)*(D:D>=33),1,0))
from Practical Lab1
branch
In case for cold days,
If “Month” is equal to the target month,
and
If Min temperature is equal or below 12℃
Basics: Branching
x=34 x=12
if x>=33: if x>=33:
print("Hot day!")
print('Hot day!’)
Hot day!
The basic structure of the “if” statement:
if [condition]: [command]
IF x>=33:
print("Hot day!") small letter (lowercase)
Error
Basics: Branching
x=34 x=32
if x>=33: if x>=33:
print('Hot day!') print('Hot day!')
else: else:
print('Not hot day') print('Not hot day')
Basics: Branching
x=11
if x>=33:
print('Hot day!') if x>=33:
else: print('Hot day')
if x<=12: elif x<=12:
print('Cold day') print('Cold day')
else: else:
print('Normal day') print('Normal day')
if y==True:
print('y is True')
else:
print('y is False')
Basics: Branching
Operator Condition
< Less Than
> Greater Than
<= Less Than or Equal to
>= Greater Than or Equal to
== Equal to
!= Not Equal to
Basics: Branching
a=330 a=1
b=330 b=3
print("A") c=5
if a > b: if a>b and a>c:
print(">") print("Both conditions are True")
elif a == b: elif a>b or a>c:
print("=") print("At least one of the conditions is True")
else: print("<") else:
print("B") pass
Just passed
An example for Big Quiz on FEB 29
Using IF statement…
Basics: List & Loop
>>> data
[29.7, 28.4, 30.3, 29.4, 29.7, 30.3, 31.6, 33.8, 33.3, 34.2, 35.1, 36.2]
>>> data[0]
29.7 Index (location, address)
>>> data[11]
36.2
>>> data[12]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> data[10:12]
[35.1, 36.2]
Basics: List
>>> data[0]=29.6
>>> data[0]
29.6 Can update (change) values in array
>>> data2=(29.7,28.4,30.3,29.4,29.7,30.3,31.6,33.8,33.3,34.2,35.1,36.2)
>>> data2[0]
29.7
>>> data2[0]=29.6
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment