0% found this document useful (0 votes)
0 views

Chap2 Programming Python

This document provides an overview of programming with Python, covering key topics such as variables, data types, built-in data structures, conditional statements, loops, functions, classes, and exception handling. It highlights Python's dynamic typing, indentation-based syntax, and various data structures like lists, tuples, sets, and dictionaries. The conclusion emphasizes Python's ease of use and versatility for programming.

Uploaded by

Phan Tiến Huy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Chap2 Programming Python

This document provides an overview of programming with Python, covering key topics such as variables, data types, built-in data structures, conditional statements, loops, functions, classes, and exception handling. It highlights Python's dynamic typing, indentation-based syntax, and various data structures like lists, tuples, sets, and dictionaries. The conclusion emphasizes Python's ease of use and versatility for programming.

Uploaded by

Phan Tiến Huy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

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
TP.HCM, 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) {
System.out.println("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"]
fruits.append("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):
self.name = name
self.age = age

student1 = Student("Alice", 20)


print(student1.name) # 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!
https://www.cse.hcmut.edu.vn

ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH


TRƯỜNG ĐẠI HỌC BÁCH KHOA

You might also like