Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Basics of Python

Uploaded by

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

Basics of Python

Uploaded by

chankakadahin3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ព្រះរាជាណាចក្រកម្ពុជា

ជាតិ សាសនា ព្រះមហាក្សត្រ

សាកលវិទ្យាល័យ បៀលប្រាយ
សាខា ខេត្តតាកែវ

ASSIIGNMENT
Data Structures & Algorithms Using Python

Topic:
Basics of Python

Professor: THOUK LYHEANG

Research by:
1. ហាវ អនដារិន
2. នេត លក្ខិណា
3. ហ៊ិន ច័ន្ទកក្កដា

1|Page

ជំនាន់ទី ១៩ ក្រុម SS1IT បន្ទប់ Lab1


I. Basics of Python

 Variables
name = "Alice" # String
age = 25 # Integer
height = 5.7 # Float
is_student = True # Boolean

 Comments
""" This is
a multi-line
comment """

Single-line: # This is a comment

 Input and Output


 input
name = input("Enter your name: ")
 output
print("Hello, " + name + "!")

II. Control Flow

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)

EX: Iterating over a list


fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
for fruit in fruits:
print(fruit,end=" ")

EX: Iterating over a string


for char in "hello":
print(char)

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}")

EX:Using else with a for loop


for i in range(3):
print(i)
else:
print("Loop completed!")

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)

IV. Data Structures


1. Lists
 A collection of items:
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Access first item

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

You might also like