Advanced Python Programming
Hoang-Giang Cao (高黃江)
Sep 2023
Optimization Methods (Spring 2024)
Leetcode
Maximum Subarray | https://leetcode.com/problems/maximum-subarray/ |
Two sum | https://leetcode.com/problems/two-sum/ |
Fizz Buzz | https://leetcode.com/problems/fizz-buzz/ |
Single Number | https://leetcode.com/problems/single-number/ |
Defanging an IP Address | https://leetcode.com/problems/defanging-an-ip-address/ |
Number of Good Pairs | https://leetcode.com/problems/number-of-good-pairs/ |
Select Python3
Nested loop
Basic Python
Extra homework 10/28
30mins delay 8:30 AM
Basic Python
HW 1-2-3
Basic Python
FINAL EXAM
Basic Python
On-site practice for Final Exam -> Extra homework.
Advanced Python Programming
FINAL EXAM
Schedule Advanced Python Programming
ID | Date | Content |
1 | 9 Dec 2024 11 Dec 2024 | Python library (Numpy + Matplotlib) Python library (Matplotlib) |
2 | 16 Dec 2024 18 Dec 2024 | Pre-Final Advanced Python Programming Review Pre-Final Advanced Python Programming |
3 | 23 Dec 2024 25 Dec 2024 | Final Advanced Python Programming (13:00 - 15:00) Final Exam Data Structure in the afternoon (14:00 - 16:00) |
4 | 30 Dec 2024 01 Jan 2025 | (Online class) Review Final Exam Advanced Python Programming No Class |
5 | 6 Jan 2025 8 Jan 2025 | (Online class) Python Library (Matplotlib + Numpy) No Class |
Python Libraries
Python Libraries
Python Libraries
Library in python
from random import randint
number = randint(5,100)
print(number)
Python library (random.py)
Your file
Library in python
from random import randint
number = randint(5,100)
print(number)
Python library (random.py)
Your file
Python Libraries
Python Libraries
For math functions
Python Libraries
For random generator
Third-party Python Libraries
Third-party Python Libraries
GCI_Auto_Judge made by Tkinter
Third-party Python Libraries
pip install numpy
pip install matplotlib
pip install
Python Libraries
Third-party Python Libraries
Numpy
Third-party Python Libraries
Matplotlib
Built-in Python Libraries
Random
Random library
import random
number = random.randint(5,100)
print(number)
Need to call the library_name.function()
Random library
import random as rd
number = rd.randint(5,100)
print(number)
Need to call the shorter_name.function()
as shorter_name
import numpy as np
import matplotlib as plt
import tensorflow as tf
Random library
from random import randint,randchoice
number = randint(5,100)
print(number)
Only import the needed function
Don’t need to call with the library name
Random library
from random import *
number = randint(5,100)
print(number)
Import all functions in the library
Don’t need to call with the library name
Random library
from random import *
number = randint(5,100)
print(number)
import random
number = random.randint(5,100)
print(number)
from random import randint,randchoice
number = randint(5,100)
print(number)
import random as rd
number = rd.randint(5,100)
print(number)
Import the library
Import the library as shorter name
Import specific functions
Import all functions
Random library
Random library
Adding noise for generalization purpose in training data
import random
number = random.random()
print("random number",number )
Random library
import random
lucky_number = random.randint(0,100)
print("The winner is",lucky_number )
import random
lucky_number = random.randint(1,40)
print("The winner is U116270"+str(lucky_number).zfill(2))
Random library
import random
arr = ["McDonald","Burger King", "Mos Burger","KFC"]
result = random.choice(arr)
print("Today, you should eat",result)
Random library
import random
arr = ["A","B", "C","D"]
result = random.choice(arr)
print("Random answer, you should choice",result)
Random library
Shuffle data for training
import random
arr = [1,2,3,4,5,6,7,8,9]
print("before shuffle:")
print(arr)
random.shuffle(arr)
print("After shuffle:")
print(arr)
Random library
import random
def get_computer_choice():
return random.choice(['rock', 'paper', 'scissors'])
computer_choice = get_computer_choice()
print("computer_choice:", computer_choice)
Random library
import random
def get_computer_choice():
return random.choice(['rock', 'paper', 'scissors'])
def get_user_choice():
print("1. rock")
print("2. paper")
print("3. scissors")
int_choice = input("your choice:")
int_choice = int (int_choice)
if (int_choice ==1) :
return "rock"
elif (int_choice ==2) :
return "paper"
elif (int_choice ==3) :
return "scissors"
computer_choice = get_computer_choice()
your_choice = get_user_choice()
print("")
print("computer_choice:", computer_choice)
print("your_choice:", your_choice)
Random library
def who_win(your_choice,computer_choice):
if (your_choice == "rock"):
if (computer_choice=="paper"):
return "You Lose!"
elif(computer_choice=="scissors"):
return "You Win!"
elif(computer_choice=="rock"):
return "Draw"
if (your_choice == "paper"):
if (computer_choice=="paper"):
return "Draw"
elif(computer_choice=="scissors"):
return "You Lose!"
elif(computer_choice=="rock"):
return "You Win!"
if (your_choice == "scissors"):
if (computer_choice=="paper"):
return "You Win"
elif(computer_choice=="scissors"):
return "Draw!"
elif(computer_choice=="rock"):
return "You Loss!"
Random library
def who_win(your_choice,computer_choice):
if your_choice == computer_choice:
return "Draw!"
elif (
(your_choice == 'rock' and computer_choice == 'scissors') or
(your_choice == 'paper' and computer_choice == 'rock') or
(your_choice == 'scissors' and computer_choice == 'paper')
):
return "You win!"
else:
return "Computer wins!"
Optimize the code
Check it by yourself!
Random library
import random
def get_computer_choice():
arr = ["rock","paper","scissors"]
return random.choice(arr)
def get_user_choice():
print("1. rock")
print("2. paper")
print("3. scissors")
user_input = input("Your choice is:")
user_input = int (user_input)
if user_input == 1:
return "rock"
elif user_input == 2:
return "paper"
elif user_input == 3:
return "scissors"
def who_win (your_choice, computer_choice):
if (your_choice == computer_choice):
return "Draw"
if (your_choice == "rock"):
if (computer_choice== "paper"):
return "Computer Win!"
elif(computer_choice== "scissors"):
return "You Win!"
if (your_choice == "paper"):
if (computer_choice== "rock"):
return "You Win!"
elif(computer_choice== "scissors"):
return "Computer Win!"
if (your_choice == "scissors"):
if (computer_choice== "rock"):
return "Computer Win!"
elif(computer_choice== "paper"):
return "You Win!"
you_win = 0
computer_win = 0
while (you_win != 3 and computer_win != 3):
computer_choice = get_computer_choice()
your_choice = get_user_choice()
print("computer_choice:",computer_choice)
print("your_choice:",your_choice)
print("")
result = who_win(your_choice,computer_choice)
print(result)
if (result=="You Win!"):
you_win +=1
if (result=="Computer Win!"):
computer_win +=1
print(f"You {you_win} : {computer_win} Computer")
print("")
if (you_win == 3):
print("You are the winner")
elif(computer_win == 3):
print("Computer is the winner")
Built-in Python Libraries
Math
Math library
Math library
import math
num = -10
abs_value = math.fabs(num)
print(f"|{num}| = {abs_value}")
import math as m
num = -10
abs_value = m.fabs(num)
print(f"|{num}| = {abs_value}")
from math import *
num = -10
abs_value = fabs(num)
print(f"|{num}| = {abs_value}")
from math import fabs
num = -10
abs_value = fabs(num)
print(f"|{num}| = {abs_value}")
Math library
import math
num = 25
result = math.sqrt(num)
print(result)
Math library
import math
x = 2
y = 3
result_exp = math.pow(x,y)
print (f"{x}^{y}={result_exp}")
result_exp_2= x**y
print (f"{x}^{y}={result_exp_2 }")
Math library
import math
num = 4.5
ceil_result = math.ceil(num)
print(f"Ceil of {num} = {ceil_result}")
num = 4.9
ceil_result = math.ceil(num)
print(f"Ceil of {num} = {ceil_result}")
num = 4.1
ceil_result = math.ceil(num)
print(f"Ceil of {num} = {ceil_result}")
Ceil of 4.5 = 5
Ceil of 4.9 = 5
Ceil of 4.1 = 5
Math library
import math
num = 4.5
ceil_result = math.floor(num)
print(f"Floor of {num} = {ceil_result}")
num = 4.9
ceil_result = math.floor(num)
print(f"Floor of {num} = {ceil_result}")
num = 4.1
ceil_result = math.floor(num)
print(f"Floor of {num} = {ceil_result}")
Floor of 4.5 = 4
Floor of 4.9 = 4
Floor of 4.1 = 4
num = 4.5
ceil_result = round(num)
print(f"Round of {num} = {ceil_result}")
num = 4.51
ceil_result = round(num)
print(f"Round of {num} = {ceil_result}")
num = 4.49
ceil_result = round(num)
print(f"Round of {num} = {ceil_result}")
num = 4.9
ceil_result = round(num)
print(f"Round of {num} = {ceil_result}")
num = 4.1
ceil_result = round(num)
print(f"Round of {num} = {ceil_result}")
round of 4.5 = 4
round of 4.51 = 5
round of 4.49 = 4
round of 4.9 = 5
round of 4.1 = 4
num = 4.5
ceil_result = round(num)
print(f"round of {num} = {ceil_result}")
num = 4.5524123
precision = 2
ceil_result = round(num,precision)
print(f"round of {num} with {precision} decimal = {ceil_result}")
num = 4.5524123
precision = 4
ceil_result = round(num,precision)
print(f"round of {num} with {precision} decimal = {ceil_result}")
round of 4.5 = 4
round of 4.5524123 with 2 decimal = 4.55
round of 4.5524123 with 4 decimal = 4.5524
Math library
import math
num = -10
abs_value = math.fabs(num)
print(f"|{num}| = {abs_value}")
|-10| = 10.0
Math library
import math
rad = math.pi
degree = math.degrees(rad)
print(rad,"rad","=", degree,"degree")
degree = 180
rad = math.radians (degree)
print(degree,"degree","=", rad,"rad")
Math library
import math
rad = math.pi/2
sin_value = math.sin(rad)
cos_value = math.cos(rad)
print(f"sin({rad }) = {sin_value}")
print(f"cos({rad }) = {cos_value}")
degree = 60
sin_value = math.sin(math.radians(degree))
cos_value = math.cos(math.radians(degree))
print(f"sin({degree}) = {sin_value}")
print(f"cos({degree}) = {cos_value}")
Advanced Python Programming
FINAL EXAM
Advanced Python Programming
Good luck to you with this course!
Any questions?