Advanced Programming
CO2039
Chapter 2: Programming with Python
ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH
TRƯỜNG ĐẠI HỌC BÁCH KHOA
[Link], 03/01/2025
CONTENT
01 VARIABLES & DATA TYPES
02 DATA STRUCTURES
03 CONDITIONALS, LOOPS AND EXCEPTIONS
04 CONCLUSION
2
VARIABLES &
01 DATA TYPES
3
Python Syntax Overview
# Python (No semicolons, indentation-based)
print("Hello, Python!")
# Java (Curly braces, semicolons)
public class Main {
public static void main(String[] args) {
[Link]("Hello, Java!");
}
}
● Python uses indentation instead of {}
● No semicolons required
4
Variables & Data Types in Python
Python is dynamically typed → No need to declare data types explicitly
Data Type Example Description
Integer x = 10 Whole numbers
Float pi = 3.14 Decimal numbers
String name = "Alice" Text
Boolean is_valid = True True or False
NoneType x = None Represents “nothing”
5
02 DATA STRUCTURES
6
Python Built-in Data Structures
● Lists (Ordered, Mutable)
fruits = ["apple", "banana", "cherry"]
[Link]("orange") # Add element
print(fruits[1]) # Output: banana
⇒ Ordered, allows duplicates, mutable
● Tuples (Ordered, Immutable)
coordinates = (10, 20)
print(coordinates[0]) # Output: 10
⇒ Like lists but immutable
7
Python Built-in Data Structures
● Sets (Unordered, Unique Elements)
unique_numbers = {1, 2, 3, 3}
print(unique_numbers) # Output: {1, 2, 3}
⇒ No duplicates, unordered
● Dictionaries (Key-Value Pairs)
student = {"name": "Alice", "age": 20}
print(student["name"]) # Output: Alice
⇒ Stores data as key-value pairs
8
Type conversions
x = 10
y = str(x) # Convert int to string
print(y) # Output: "10"
Common conversions:
● int("100") → Converts string to integer
● float("3.14") → Converts string to float
● str(50) → Converts integer to string
9
CONDITIONALS,
03 LOOPS &
EXCEPTIONS
10
Conditional Statements
age = 18
if age >= 18:
print("Adult")
elif age >= 13:
print("Teenager")
else:
print("Child")
⇒ Indentation is required (No {} like Java or C++)
11
Loops
● for Loop (Iterate over a sequence)
for i in range(5):
print(i) # Output: 0 1 2 3 4
⇒ Loops through a range or collection
● while Loop (Repeats until condition is false)
x = 0
while x < 5:
print(x)
x += 1
⇒ Use break to stop the loop early
12
Functions
● Defining and calling a function
def greet(name):
return "Hello, " + name
print(greet("Alice")) # Output: Hello, Alice
● Supports default and keyword arguments
def greet(name="Guest"):
return "Hello, " + name
print(greet()) # Output: Hello, Guest
13
Classes
● Defining a simple class for data storage
class Student:
def __init__(self, name, age):
[Link] = name
[Link] = age
student1 = Student("Alice", 20)
print([Link]) # Output: Alice
● __init__() → Constructor that initializes object properties
● self → Refers to the instance of the class
14
Exception Handling
● Handling errors with try-except
try:
x = int("abc") # Error: Cannot convert string to int
except ValueError:
print("Invalid input")
⇒ Prevents program crashes due to runtime errors
15
03 CONCLUSION
16
Conclusion
● Python is dynamically typed and easy to use
● Supports various built-in data structures
● Uses indentation for code blocks
● Includes conditional statements, loops, functions, classes and exception
handling
17
Thank you for your
attention!
[Link]
ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH
TRƯỜNG ĐẠI HỌC BÁCH KHOA