Basic Python For AI: Loops and Files
Basic Python For AI: Loops and Files
Quang-Vinh Dinh
PhD in Computer Science
Friday (07/06/2024)
Objectives
FOR Loop WHILE Loop Files
False
WHILE Loop
Is this the True
last
element?
SECTION 3
False
Files continue Execute code inside break
for loop
SECTION 4
Loop 1 2 3 4 5
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
Đã duyệt True
phần tử range(start=0, stop=5, step=1)
cuối cùng?
False 0, 1, 2, 3, 4
0, 1, 2, 3, 4
Code sau for
All-in-One 2024 For Loop Section 1 Page 4
Output
All-in-One 2024 For Loop Section 1 Page 5
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
Output
All-in-One 2024 For Loop Section 1 Page 8
𝑡𝑜𝑡𝑎𝑙 = 0
print(total)
Output
All-in-One 2024 For Loop Section 1 Page 9
𝑡𝑜𝑡𝑎𝑙 = 0
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
Output
print(result)
All-in-One 2024 For Loop Section 1 Page 12
Đã duyệt True
phần tử
cuối cùng?
False
1 2 3
𝑖=0 𝑖=1 𝑖=2
if False: if False: if True:
break break break
print(i) print(i) print(i)
break
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
Đã duyệt True
phần tử
cuối cùng?
False
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
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
Đã duyệt True
phần tử
cuối cùng?
False
n=5
for i in range(1, n+1): +
…
n=5 how? +
for i in range(1, n+1):
…
n=5 +
result = 0
for i in range(1, n+1):
result = result + …
3 𝑖=3 4 𝑖=4 5 𝑖=5
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
𝑎
𝑥𝑛 +
𝑥𝑛
𝑥𝑛+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
keyword
colon
False
Condition
True
False
Condition
True
while-True-break
All-in-One 2024 Exercises Section 2 Page 33
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) (3)
(2)
A file
(1) (3)
(2)
A file
Python
(1) (3)
(2)
A file
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
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
𝑓(𝑥)
𝑘=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
𝜌 = 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: