Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Lecture3 Basic Programming

Uploaded by

louis425llw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture3 Basic Programming

Uploaded by

louis425llw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

SEE1002

Introduction to Computing for Energy and Environment

Lecture 2: Basic programming Languages/Grammars


Basics: Logical operators
>>> weekday=True
>>> weekend=False
>>> not weekday
False
>>> not weekend
True
>>> weekday and weekend
False
>>> weekday or weekend
True
Basics: Logical operators
A B A and B A B A or B

False False False False False False

False True False False True True

True False False True False True

True True True True True True

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

>>> x1=input('Enter x1: ') >>> x1=int(input('Enter x1: '))


Enter x1: 2 Enter x1: 2
>>> x2=input('Enter x2: ') >>> x2=int(input('Enter x2: '))
Enter x2: 4 Enter x2: 4
>>> x2=x1+x2 >>> x2=x1+x2
>>> x2 >>> x2
'24' 6
Basics: Program flow

x1=int(input('Enter x1: ')) x1=int(input('Enter x1: '))


x2=int(input('Enter x2: ')) x2=int(input('Enter x2: '))
x2=x1+x2 print(x2)
print(x2) x2=x1+x2

Enter x1: 2 Enter x1: 2


Enter x2: 4 Enter x2: 4
6 4

Different result
(Sequence is important!)
Basics: Branching
=SUM(IF((B:B=Month)*(D:D>=33),1,0))
from Practical Lab1

If “Month” is equal to the target month,


IF and
If Max temperature is equal or above 33℃
branch

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')

elif: This code is shorter and easy to read.


It can be thought of as “else if”, i.e., code to be The risk of error is reduced
executed if another condition is satisfied. as compared to a longer one.
Basics: Branching
y=True
type(y)

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

We can do calculations for every cases manually,


but is there any efficiency way to do at once?

We need to have data with array


and process with loops
Basics: List
list
>>> 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
[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

Cannot update (change) values in array


Basics: List
Do not decide the size of the array at first.
>>> list=[]
>>> list.append(0)
>>> list.append(1)
>>> list.append(2)
>>> list
[0, 1, 2]
Basics: List Start & stop
range([start,]stop[,increment]) >>> a2=range(0,10)
>>> a2[0]
>>> a1=range(10) 0
>>> a1[0] >>> a2[1]
0 1
>>> a1[1] >>> a2[2]
1 2
>>> a1[10] >>> a2[3]
Traceback (most recent call last): 3
File "<stdin>", line 1, in <module>
IndexError: range object index out of range >>> a3=range(0,10,2)
>>> a1[9] >>> a3[0]
9 0
>>> a3[1]
Checking
2
length of array
Start is optional. By default, Start=0 >>> a3[2]
Increment is optional. By default, increment=1 integer 4
>>> len(a3)
5
Basics: List
Even number
>>> 6 in a3
True
Boolean
>>> 7 in a3
False Odd number

Dictionaries with braces {} (Curly Brackets)

>>> MTR={'KowloonTong':1982,'Admiralty':1980,'SouthHorizons':2016,'LOHAS Park':2009}


>>> MTR['KowloonTong']
1982
>>> MTR[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 0
Basics: List
>>> MTR.update({'TsimShaTsui':1979})
>>> MTR['TsimshaTsui']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'TsimshaTsui'
>>> MTR['TsimShaTsui']
1979
Basics: Loop

start, stop, increment


>>> for i in range(10,101,10):
... print( 'the sqrt of',i,' = ',float(i)**0.5)
...
the sqrt of 10 = 3.1622776601683795
the sqrt of 20 = 4.47213595499958
the sqrt of 30 = 5.477225575051661
the sqrt of 40 = 6.324555320336759
the sqrt of 50 = 7.0710678118654755
the sqrt of 60 = 7.745966692414834
the sqrt of 70 = 8.366600265340756
the sqrt of 80 = 8.94427190999916
the sqrt of 90 = 9.486832980505138
the sqrt of 100 = 10.0
Basics: Loop
Print only first 2 decimal places

>>> for i in range(10,101,10):


... print( 'the sqrt of %d = %.2f' %(i,float(i)**0.5))
...
the sqrt of 10 = 3.16
the sqrt of 20 = 4.47
the sqrt of 30 = 5.48
the sqrt of 40 = 6.32
the sqrt of 50 = 7.07
the sqrt of 60 = 7.75
the sqrt of 70 = 8.37
the sqrt of 80 = 8.94
the sqrt of 90 = 9.49
the sqrt of 100 = 10.00
Basics: Loop
>>> for i in range(12): >>> for i in range(12):
... print(data[i]) ... if data[i]>=33:
... ... print(data[i])
29.6 ...
28.4 33.8
30.3 33.3
29.4 34.2
29.7 35.1
30.3 36.2 Can be replaced with
31.6 n_hotday +=1
33.8
33.3 >>> n_hotday = 0
34.2 >>> for i in range(len(data)):
35.1 ... if data[i]>=33:
36.2 ... n_hotday = n_hotday+1
...
>>> n_hotday
5
Good job!
Now, you have learned branch (if) & loop (for) statements.

You can be a programmer!

Let’s have fun programming in practical labs!

You might also like