0% found this document useful (0 votes)
8 views58 pages

Basic Python For AI: Loops and Files

Uploaded by

dodaonam2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views58 pages

Basic Python For AI: Loops and Files

Uploaded by

dodaonam2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Basic Python for AI

Loops and Files

Quang-Vinh Dinh
PhD in Computer Science
Friday (07/06/2024)
Objectives
FOR Loop WHILE Loop Files

Code before for

Is this the True


last
element?

False

continue Execute code inside break


for loop
# …
while condition:
# code inside while
# …
Code after for
True/False  condition
Outline
for syntax
SECTION 1
# code before for
FOR Loop indentation for element in iterable:
# code inside for
# code after for

SECTION 2 Code before for

WHILE Loop
Is this the True
last
element?
SECTION 3
False
Files continue Execute code inside break
for loop

SECTION 4

Examples Code after for


All-in-One 2024 For Loop Section 1 Page 1
❖ Repeat action in Python

Loop 1 2 3 4 5

print(‘hello’) print(‘hello’) print(‘hello’) print(‘hello’) print(‘hello’)

Output
All-in-One 2024 For Loop Section 1 Page 2

keyword colon
Code trước for

Đã duyệt True
phần tử
cuối cùng?

False
Iterables
indentation
String Thực thi các câu lệnh
trong for
Tuple
List
Dictionary
Code sau for
range()
All-in-One 2024 For Loop Section 1 Page 3

Code trước for range(start=0, stop, step=1)

Đã duyệt True
phần tử range(start=0, stop=5, step=1)
cuối cùng?

False 0, 1, 2, 3, 4

Thực thi các câu lệnh


trong for range(5)

0, 1, 2, 3, 4
Code sau for
All-in-One 2024 For Loop Section 1 Page 4

𝑖=0 𝑖=2 𝑖=4


𝑖=1 𝑖=3
1 2 3 4 5
𝑖=0 𝑖=1 𝑖=2 𝑖=3 𝑖=4

print(i) print(i) print(i) print(i) print(i)

Output
All-in-One 2024 For Loop Section 1 Page 5

𝑖=0 𝑖=2 𝑖=4


𝑖=1 𝑖=3
1 2 3 4 5
𝑖=0 𝑖=1 𝑖=2 𝑖=3 𝑖=4

print(i*i) print(i*i) print(i*i) print(i*i) print(i*i)

Output
All-in-One 2024 For Loop Section 1 Page 6

𝑖=0 𝑖=2
𝑖=1 𝑖=3 𝑖=4
𝑣𝑎𝑙𝑢𝑒 = 3
𝑣𝑎𝑙𝑢𝑒 = 3 … … 𝑣𝑎𝑙𝑢𝑒 = 3
1 2 3 4 5
𝑖=0 𝑖=1 𝑖=2 𝑖=3 𝑖=4
𝑣𝑎𝑙𝑢𝑒 = 3 𝑣𝑎𝑙𝑢𝑒 = 3 𝑣𝑎𝑙𝑢𝑒 = 3 𝑣𝑎𝑙𝑢𝑒 = 3 𝑣𝑎𝑙𝑢𝑒 = 3
print(value+i) print(value+i) print(value+i) print(value+i) print(value+i)

Output
All-in-One 2024 For Loop Section 1 Page 7

𝑡𝑜𝑡𝑎𝑙 = 0

𝑖=0 𝑖=2 𝑖=4


𝑖=1 𝑖=3
𝑡𝑜𝑡𝑎𝑙 = 0
1 2 3 4 5
𝑖=0 𝑖=1 𝑖=2 𝑖=3 𝑖=4
𝑡𝑜𝑡𝑎𝑙 = 0 𝑡𝑜𝑡𝑎𝑙 = 0 𝑡𝑜𝑡𝑎𝑙 = 1 𝑡𝑜𝑡𝑎𝑙 = 3 𝑡𝑜𝑡𝑎𝑙 = 6
𝑡𝑜𝑡𝑎𝑙 𝑡𝑜𝑡𝑎𝑙 𝑡𝑜𝑡𝑎𝑙 𝑡𝑜𝑡𝑎𝑙
print(total) print(total) print(total) print(total) print(total)
𝑡𝑜𝑡𝑎𝑙 = 1 𝑡𝑜𝑡𝑎𝑙 = 3
𝑡𝑜𝑡𝑎𝑙 = 6
𝑡𝑜𝑡𝑎𝑙 = 0 𝑡𝑜𝑡𝑎𝑙 = 10

Output
All-in-One 2024 For Loop Section 1 Page 8

𝑡𝑜𝑡𝑎𝑙 = 0

𝑖=0 𝑖=2 𝑖=4


𝑖=1 𝑖=3
𝑡𝑜𝑡𝑎𝑙 = 0
1 2 3 4 5
𝑖=0 𝑖=1 𝑖=2 𝑖=3 𝑖=4
𝑡𝑜𝑡𝑎𝑙 = 0 𝑡𝑜𝑡𝑎𝑙 = 0 𝑡𝑜𝑡𝑎𝑙 = 1 𝑡𝑜𝑡𝑎𝑙 = 3 𝑡𝑜𝑡𝑎𝑙 = 6
𝑡𝑜𝑡𝑎𝑙 𝑡𝑜𝑡𝑎𝑙 𝑡𝑜𝑡𝑎𝑙 𝑡𝑜𝑡𝑎𝑙
total = total+i total = total+i total = total+i total = total+i total = total+i

𝑡𝑜𝑡𝑎𝑙 = 0 𝑡𝑜𝑡𝑎𝑙 = 1 𝑡𝑜𝑡𝑎𝑙 = 3 𝑡𝑜𝑡𝑎𝑙 = 6 𝑡𝑜𝑡𝑎𝑙 = 10

print(total)
Output
All-in-One 2024 For Loop Section 1 Page 9

𝑡𝑜𝑡𝑎𝑙 = 0

𝑖=0 𝑖=2 𝑖=4


𝑖=1 𝑖=3
𝑡𝑜𝑡𝑎𝑙 = 0
1 2 3 4 5
𝑖=0 𝑖=1 𝑖=2 𝑖=3 𝑖=4
𝑡𝑜𝑡𝑎𝑙 = 0 𝑡𝑜𝑡𝑎𝑙 = 0 𝑡𝑜𝑡𝑎𝑙 = 0 𝑡𝑜𝑡𝑎𝑙 = 0 𝑡𝑜𝑡𝑎𝑙 = 0
total = total+i total = total+i total = total+i total = total+i total = total+i

𝑡𝑜𝑡𝑎𝑙 = 0 𝑡𝑜𝑡𝑎𝑙 = 1 𝑡𝑜𝑡𝑎𝑙 = 2 𝑡𝑜𝑡𝑎𝑙 = 3 𝑡𝑜𝑡𝑎𝑙 = 4

variable address print(total)


1371313629456 Output
All-in-One 2024 For Loop Section 1 Page 10

1 2

3 4

6
All-in-One 2024 For Loop Section 1 Page 11

𝑟𝑒𝑠𝑢𝑙𝑡 = 1

𝑖=1 𝑖=3
𝑟𝑒𝑠𝑢𝑙𝑡 = 1 𝑖=2 𝑖=4

1 2 3 4
𝑖=1 𝑖=2 𝑖=3 𝑖=4
𝑟𝑒𝑠𝑢𝑙𝑡 = 1 𝑟𝑒𝑠𝑢𝑙𝑡 = 1 𝑟𝑒𝑠𝑢𝑙𝑡 = 3 𝑡𝑜𝑡𝑎𝑙 = 6
𝑟𝑒𝑠𝑢𝑙𝑡 𝑟𝑒𝑠𝑢𝑙𝑡 𝑟𝑒𝑠𝑢𝑙𝑡
result = result*i result = result*i result = result*i result = result*i

𝑟𝑒𝑠𝑢𝑙𝑡 = 1 𝑟𝑒𝑠𝑢𝑙𝑡 = 2 𝑟𝑒𝑠𝑢𝑙𝑡 = 6 𝑟𝑒𝑠𝑢𝑙𝑡 = 24

Output
print(result)
All-in-One 2024 For Loop Section 1 Page 12

Code trước for


break keyword

Đã duyệt True
phần tử
cuối cùng?

False

Thực thi các câu lệnh break


trong for

Code sau for


All-in-One 2024 For Loop Section 1 Page 13

𝑖=0 𝑖=1 𝑖=2

1 2 3
𝑖=0 𝑖=1 𝑖=2
if False: if False: if True:
break break break
print(i) print(i) print(i)

break

Output Code after FOR


All-in-One 2024 For Loop Section 1 Page 14

𝑖=0 𝑖=1 𝑖=2

1 2 3
𝑖=0 𝑖=1 𝑖=2
print(i) print(i) print(i)
if False: if False: if False:
break break break
print(1)
print(0)
print(2)
Output
break Code after FOR
All-in-One 2024 For Loop Section 1 Page 15

Code trước for


continue keyword

Đã duyệt True
phần tử
cuối cùng?

False

continue Thực thi các câu lệnh


trong for

Code sau for


All-in-One 2024 For Loop Section 1 Page 16

𝑖=0 𝑖=1 𝑖=2

1 2 3
𝑖=0 𝑖=1 𝑖=2
if False: if True: if False:
continue continue continue
print(i) print(i) print(i)

Output
All-in-One 2024 For Loop Section 1 Page 17

𝑖=0 𝑖=1 𝑖=2

1 2 3
𝑖=0 𝑖=1 𝑖=2
print(i) print(i) print(i)
if False: if True: if False:
continue continue continue
print(1)
print(0)
print(2)
Output
For Loop

Code trước for

Đã duyệt True
phần tử
cuối cùng?

False

Thực thi các câu lệnh


trong for

Code sau for


22
All-in-One 2024 Example Section 1 Page 19
❖ PI estimation
Gregory-Leibniz Series
1 𝑖=1 2 𝑖=2
𝑛
−1 𝑖+1 … …
𝑃𝐼 ≈ 4 ෍
2𝑖 − 1
𝑖=1

n=5
for i in range(1, n+1): +

3 𝑖=3 4 𝑖=4 5 𝑖=5


… … …
All-in-One 2024 Example Section 1 Page 20
❖ PI estimation
1 𝑖=1 2 𝑖=2
Gregory-Leibniz Series
𝑛 −𝟏 𝒊+𝟏 −𝟏 𝒊+𝟏
−1 𝑖+1 𝟐𝒊 − 𝟏 𝟐𝒊 − 𝟏
𝑃𝐼 ≈ 4 ෍
2𝑖 − 1
𝑖=1

n=5 how? +
for i in range(1, n+1):

3 𝑖=3 4 𝑖=4 5 𝑖=5

−𝟏 𝒊+𝟏 −𝟏 𝒊+𝟏 −𝟏 𝒊+𝟏


𝟐𝒊 − 𝟏 𝟐𝒊 − 𝟏 𝟐𝒊 − 𝟏
All-in-One 2024 Example Section 1 Page 21
❖ PI estimation
1 𝑖=1 2 𝑖=2
Gregory-Leibniz Series
𝑛 −𝟏 𝒊+𝟏 −𝟏 𝒊+𝟏
−1 𝑖+1 𝟐𝒊 − 𝟏 𝟐𝒊 − 𝟏
𝑃𝐼 ≈ 4 ෍
2𝑖 − 1
𝑖=1

n=5 +
result = 0
for i in range(1, n+1):
result = result + …
3 𝑖=3 4 𝑖=4 5 𝑖=5

−𝟏 𝒊+𝟏 −𝟏 𝒊+𝟏 −𝟏 𝒊+𝟏


𝟐𝒊 − 𝟏 𝟐𝒊 − 𝟏 𝟐𝒊 − 𝟏
All-in-One 2024 Example Section 1 Page 22
❖ PI estimation
Gregory-Leibniz Series 1 𝑖=1 2 𝑖=2
𝑛 −𝟏 𝒊+𝟏 −𝟏 𝒊+𝟏
−1 𝑖+1
𝑃𝐼 ≈ 4 ෍ 𝟐𝒊 − 𝟏 𝟐𝒊 − 𝟏
2𝑖 − 1
𝑖=1

3 𝑖=3 4 𝑖=4 5 𝑖=5

−𝟏 𝒊+𝟏 −𝟏 𝒊+𝟏 −𝟏 𝒊+𝟏


𝟐𝒊 − 𝟏 𝟐𝒊 − 𝟏 𝟐𝒊 − 𝟏
All-in-One 2024 Example Section 1 Page 23
❖ PI estimation

Gregory-Leibniz Series
𝑛
−1 𝑖+1
𝑃𝐼 ≈ 4 ෍
2𝑖 − 1
𝑖=1

Nilakantha Series
𝑛
−1𝑖
𝑃𝐼 ≈ 3 + 4 ෍
2𝑖 + 2 2𝑖 + 3 2𝑖 + 4
𝑖=0
All-in-One 2024
Example: Quadratic Root Section 1 Page 24
❖ Compute quadratic root for the number N
𝑎=9
9
𝑠𝑒𝑡 𝑥0 = = 4.5
Newton Method 2
Compute 9
𝑛=0
Given a and n = 0
Set a value for 𝑥0 (𝑥0 = a/2)
𝑛=0 𝑎
𝑥0 + 9
4.5 +
𝑥1 =
𝑥0
= 4.5 = 6.5 = 3.25
𝑎 2 2 2
𝑥𝑛 +
𝑥𝑛
𝑥𝑛+1 = 𝑛=1
2 𝑎
𝑥1 + 𝑥 3.25 +
9
𝑥2 = 1
= 3.25 = 6.019 = 3.009
2 2 2

n=n+1 𝑛=2 𝑎
𝑥2 + 𝑥 9
3.009 + 3.009
2
𝑥3 = = = 3.00001
2 2
All-in-One 2024 Example: Quadratic Root Section 1 Page 25
❖ Compute quadratic root for the number N
1
𝑎 n=4
𝑥0 =
Newton Method 2 for i in range(n):
𝑥0 …
Given a and n = 0
𝑎
Set a value for 𝑥0 (𝑥0 = a/2) 𝑥0 +
𝑥0 2
𝑥1 = n=4
2
𝑥1 for _ in range(n):
𝑎 𝑎
𝑥𝑛 + 𝑥1 + …
𝑥𝑛 𝑥1
𝑥𝑛+1 = 𝑥2 =
2 2
𝑥2
𝑎 Which one is better?
𝑥2 +
n=n+1
𝑥2
𝑥3 =
𝑥3 2 How to propagate
… information?
All-in-One 2024 Example: Quadratic Root Section 1 Page 26
❖ Compute quadratic root for the number N

𝑎
𝑥0 = n=4
Newton Method 2
𝑥0 global_info = …
Given a and n = 0
𝑎
Set a value for 𝑥0 (𝑥0 = a/2) 𝑥0 + for _ in range(n):
𝑥0
𝑥1 =
2
𝑥1 # do something
𝑎 𝑎
𝑥𝑛 + 𝑥1 +
𝑥𝑛 𝑥1 # update global_info
𝑥𝑛+1 = 𝑥2 =
2 2
𝑥2
𝑎
𝑥2 +
n=n+1
𝑥2
𝑥3 =
𝑥3 2

All-in-One 2024 Example: Quadratic Root Section 1 Page 27
❖ Compute quadratic root for the number N

𝑎
𝑟= n=4
Newton Method 2
𝑟 global_info = …
Given a and n = 0
𝑎
Set a value for 𝑥0 (𝑥0 = a/2) 𝑟+ for _ in range(n):
𝑟= 𝑟
2
𝑟 # do something
𝑎 𝑎
𝑥𝑛 + 𝑟+
𝑥𝑛 𝑟 # update global_info
𝑥𝑛+1 = 𝑟=
2 2
𝑟 n=4
𝑎 result = a/2
𝑟+
n=n+1 𝑟= 𝑟 for _ in range(n):
𝑟 2 value = (result + a/result)/2
result = value
Compute the right side first, then assign to r …
All-in-One 2024
Example: Quadratic Root Section 1 Page 28
❖ Compute quadratic root for the number N

Newton Method

Set a value for 𝑥0 ; n = 0


(𝑥0 = a/2)

𝑎
𝑥𝑛 +
𝑥𝑛
𝑥𝑛+1 =
2

n=n+1
Outline
SECTION 1

FOR Loop

SECTION 2
WHILE Loop
SECTION 3

Files
# …
while condition:
SECTION 4 # code inside while
# …
Examples True/False  condition
All-in-One 2024 While Loop Section 2 Page 29

Code trước while

keyword
colon
False
Condition

True

Thực thi các câu lệnh


trong while
indentation

Code sau while


All-in-One 2024 While Loop Section 2 Page 30

Code trước while

False
Condition

True

Thực thi các câu lệnh


trong while

Code sau while


While Loop

yes print(0) yes print(1)


𝑖=0 𝑖<5 ? 𝑖<5 ? 𝑖<5 ?
𝑖 =0+1=1 𝑖 =1+1=2

yes print(2) yes print(3)


𝑖<5 ? 𝑖<5 ? 𝑖<5 ?
𝑖 =2+1=2 𝑖 =3+1=4

yes print(4) no print(′Phần code này


𝑖<5 ? 𝑖<5 ?
𝑖 =4+1=5 khi đã thoát while′)
All-in-One 2024 While Loop Section 2 Page 32

while-True-break
All-in-One 2024 Exercises Section 2 Page 33

E estimation Compute quadratic root for the number a

Newton Method

Given a and n = 0
PI estimation Set a value for 𝑥0 (𝑥0 = a/2)
Simulation of
coin tossing
𝑎
𝑥𝑛 +
𝑥𝑛
𝑥𝑛+1 =
2

n=n+1
Outline
SECTION 1

FOR Loop

SECTION 2
WHILE Loop
SECTION 3

Files
SECTION 4

Examples
All-in-One 2024 File Section 3 Page 34
❖ Typical procedure
(1) open(file_path, ‘r’)
Read from a file (2) read()
Python (already exist)
(3) close()

(1) (3)
(2)

A file

(1) Connect to file


(2) Read from/write to file
(3) Disconnect to file
All-in-One 2024 File Section 3 Page 35
❖ Typical procedure
(1) open(file_path, ‘r’)
Read content
(2) readlines()
Python from a file as lines
(3) close()

(1) (3)
(2)

A file

(1) Connect to file


(2) Read from/write to file
(3) Disconnect to file
All-in-One 2024 File Section 3 Page 36
❖ Typical procedure
(1) open(file_path, ‘w’)
Write to a file (2) write()
Python (not exist)
(3) close()

(1) (3)
(2)

A file

(1) Connect to file


(2) Read from/write to file
(3) Disconnect to file
open(file_path, ‘a’)
File Write to a file
(appending content if
(1)
(2) write()
the file already exists) (3) close()
❖ Typical procedure

Python

(1) (3)
(2)

A file

(1) Connect to file


(2) Read from/write to file
(3) Disconnect to file
All-in-One 2024 File Section 3 Page 38
❖ Common Error
All-in-One 2024 File Section 3 Page 39

Python
❖ Example
(1) (3)
(2)

A file

❖ with keyword
Outline
SECTION 1

FOR Loop

SECTION 2
WHILE Loop
SECTION 3

Files
SECTION 4

Examples
All-in-One 2024 Example 1 Section 4 Page 40
❖ Compute the area of a unit circle

𝑓(𝑥1 ) 𝑓(𝑥2 ) 𝑓(𝑥𝑛 )


𝑓(𝑥)

𝐴𝑒𝑠𝑡 ≈ + + … +

∆𝑥1 ∆𝑥2 ∆𝑥𝑛

𝐴𝑒𝑠𝑡 ≈ ෍ 𝑓(𝑥𝑖 )∆𝑥𝑖


𝑖=1

lim 𝐴𝑒𝑠𝑡 = 𝐴𝑟𝑒𝑎𝑙


∆𝑥→0
All-in-One 2024 Example 1 Section 4 Page 41
❖ Compute the area of a unit circle

𝑦= 1 − 𝑥2

𝑦 = − 1 − 𝑥2
All-in-One 2024 Example 1 Section 4 Page 42
❖ Compute the area of a unit circle
[Link]=3.141592

𝑛 = 20 𝑛 = 200
𝐴𝑒𝑠𝑡 = 3.1045 𝐴𝑒𝑠𝑡 = 3.1404
All-in-One 2024 Example 1 Section 4 Page 43
❖ Compute the area of a unit circle
[Link]=3.141592

𝑛 = 200 𝑛 = 2000
𝐴𝑒𝑠𝑡 = 3.1404 𝐴𝑒𝑠𝑡 = 3.14155
All-in-One 2024 Example 1
Section 4 Page 44
❖ Compute the area of a unit circle

𝑓(𝑥)

𝐴𝑒𝑠𝑡 ≈ ෍ 𝑓(𝑥𝑖 )∆𝑥𝑖


𝑖=1
All-in-One 2024 Example 2 Section 4 Page 45
❖ Context
All-in-One 2024 Example 2 Section 4 Page 46
❖ Moving average

𝑘=2
𝑠𝑡−1 + 𝑠𝑡−2 + ⋯ + 𝑠𝑡−𝑘
3 8 6 5 1 7 9 0 8 4 𝑆𝑀𝐴𝑡 =
𝑘
5.5 7.0 5.5 3.0 4.0 8.0 4.5 4.0 6.0

3 8 6 5 1 7 9 0 8 4 𝐸𝑀𝐴𝑡 = 𝜌𝐸𝑀𝐴𝑡−1 + (1 − 𝜌)𝑠𝑡


3.0 5.5 5.8 5.4 3.2 5.1 7.0 3.5 5.8 4.9

𝜌 = 0.5
𝜌 = 0.5
Example 2
❖ Exponentially weighted averages

𝑉𝑡 = 𝜌𝑉𝑡−1 + (1 − 𝜌)𝑠𝑡

𝜌 = 0.9 𝜌 = 0.98
All-in-One 2024 Cheat Sheet – For Loop
for syntax Common Iterables for loop applications
_ String: _ List: Coin tossing
# code before for
greeting = ‘Hello AIVIETNAM’ odds = [1, 3, 5, 7] 𝑒𝑣𝑒𝑛𝑡
indentation for element in iterable: 𝑃 𝑒𝑣𝑒𝑛𝑡 =
for character in greeting: for odd in odds: 𝑆
# code inside for
print(character) print(odd)
# code after for
_ Definition: _ Tuple: _ Dictionary: Euler’s number
+ for, in: python keywords 𝑛
fruits = (’apple’, ’banana’ parameters = {’lr’: 0.1, 𝑛 1
+ element: iterable element 1 lim 1 + =ℯ
’melon’, ’peach’) ’optimizer’: ’Adam’, ℯ ≈ 1+ 𝑛→∞ 𝑛
+ iterable: range(), list, string, tuple, and dictionary 𝑛
for fruit in fruits: ’metric’: ’Accuracy’}
+ colon: “:”
print(fruit) for key in parameters:
Quadratic Root
print(key,
parameters(key)) 𝑁
Code before for 𝑥0 = ;𝑛 = 0
2
_ range(start, end, step):
range(start=0, end=5, step=1) ~ range(5) 𝑁
𝑥𝑛 + 𝑥
𝑛
𝑥𝑛+1 =
# usage of range() 2
Is this the True # just like using a list
last for i in range(5): 𝑛 =𝑛 +1
element? [0, 1, 2, 3, 4] print(i)
PI estimation
False _ Monte Carlo Method:
Special keywords
𝑠 2 𝑁𝐶
𝜋≈
continue Execute code inside _ continue: _ break: 𝑁𝑆
break _ Gregory-Leibniz Series:
for loop for i in range(10): for i in range(10):
𝑛
if i == 5: if i == 5: (−1)𝑖+1
# code after continue # if true then the 𝜋 ≈ 4෍
2𝑖 − 1
will not be executed loop will be end 𝑖=1
continue break _ Nilakantha Series:
𝑛
−1𝑖
print(i) print(i) 𝜋 ≈ 3 + 4෍
Code after for (2𝑖 + 2)(2𝑖 + 3)(2𝑖 + 4)
#output: 0,1,2,3,4,6,7,8,9 #output: 0,1,2,3,4 𝑖=0
All-in-One 2024 Cheat Sheet 2
Random & Math module Activation Functions
Map x values into smaller ranges
_ Math module’s common methods and constants:

Definition Syntax Definition Syntax


Absolute [Link](n) Factorial [Link]()
Logarith [Link](n) Rounding 1 [Link]()
Sine [Link](n) Rounding 2 [Link]()
Cosine [Link](n) Rounding 3 [Link]()
Exponential [Link](n) Euler (ℯ) math.e While Loop
Square root [Link](n) PI (𝜋) [Link] _ while condition: _ while-True-break:
# … i = 0
_ Random module:
while condition: i = 0 while True:
+ Generate random floating-point in [0, 1): [Link]() while i < 5: print(i)
# code inside while
+ Generate random integer in [a, b]: [Link](a, b) # … print(i) i = i + 1
i = i + 1 if i == 5:
print(”done”) break
Random/Loop Examples True/False  condition
print(”done”)

Coin tossing PI estimation


Common Errors
𝑒𝑣𝑒𝑛𝑡
𝑃 𝑒𝑣𝑒𝑛𝑡 = _ Monte Carlo Method:
𝑆 _ NameError: _ SyntaxError: print(’aivietnam”)
𝑠 2 𝑁𝐶 a = 5
𝜋≈ _ ZeroDivisionError: print(5 / 0)
𝑁𝑆 c = a + b
Euler’s number print(c) # b not defined
_ Gregory-Leibniz Series: _ TypeError: print(5 + ”aivietnam”)
𝑛 𝑛
1 (−1)𝑖+1 Print(a) # Print not defined
ℯ ≈ 1+ 𝜋 ≈ 4෍ _ValueError: _ IndetationError: a = 1
𝑛 2𝑖 − 1
𝑖=1 print(int(”aivietnam”)) b = 2 # identation
Quadratic Root _ Nilakantha Series: _RecursionError: print(a + b)
𝑛
𝑎 −1𝑖 def a_func(n):
𝑎 𝑥𝑛 + 𝑥 𝜋 ≈ 3+4෍
_ ModuleNotFoundError: import mymodule
𝑥0 = ; 𝑖 = 0 → 𝑛_𝑙𝑜ops; 𝑥𝑛+1 = 𝑛
(2𝑖 + 2)(2𝑖 + 3)(2𝑖 + 4) return a_func(n)
2 2 𝑖=0 _ IndexError: print(”aivietnam”[50])
a_func(5) # infinite calls
58

You might also like