Basics of Python
Basics of Python
សាកលវិទ្យាល័យ បៀលប្រាយ
សាខា ខេត្តតាកែវ
ASSIIGNMENT
Data Structures & Algorithms Using Python
Topic:
Basics of Python
Research by:
1. ហាវ អនដារិន
2. នេត លក្ខិណា
3. ហ៊ិន ច័ន្ទកក្កដា
1|Page
Variables
name = "Alice" # String
age = 25 # Integer
height = 5.7 # Float
is_student = True # Boolean
Comments
""" This is
a multi-line
comment """
1. Conditional Statements
if, elif, else:
age = int(input("Enter your age: "))
if age < 18:
print("You are a minor.")
elif age == 18:
print("You just became an adult!")
else:
print("You are an adult.")
2|Page
2. Loops
For Loop:
EX: Using range() in a for loop
for i in range(5):
print("Iteration:", i)
3|Page
EX: Using break and continue
break: Exit the loop prematurely.
continue: Skip the current iteration and move to the next.
for i in range(5):
if i == 3:
break # Exit the loop when i is 3
print(i)
for i in range(5):
if i == 3:
continue # Skip the iteration when i is 3
print(i)
EX: Nested for loops: You can use a for loop inside another for loop.
for i in range(3):
for j in range(2):
print(f"i={i}, j={j}")
4|Page
While Loop:
count = 0
while count < 5:
print("Count:", count)
count += 1
III. Functions
1. Defining Functions
Basic function:
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
greet("darin")
2. Return Values
Example:
def add(a, b):
return a + b
result = add(5, 3)
print(result)
5|Page
fruits.append("orange") # Add item
2. Dictionaries
Key-value pairs:
person = {"name": "Alice", "age": 25}
print(person["name"], person["age"])
3. Tuples
Immutable sequences:
coordinates = (0:10, 1:20, 2:30)
print(coordinates[0])
6|Page