Run python3 to check if it's already on your machine
- Mac: Run this in terminal:
brew install python. - Windows: Download the installer from python.org.
- Linux: Run this in terminal
sudo apt-get install python3.
Create a file hello.py with the following content:
print("Hello, World!")Run it using the command: python3 hello.py.
You don't need to set data type for Python variables explicitly, you just set a value to it.
str_var = "Hello" # String
int_var = 42 # Integer
float_var = 2.3 # Float
bool_var = True # Boolean
list_var = [1, 2, 3] # List(similar to arrays)
tuple_var = ("Hello", 42, True, 5.9, 66) #Tuples (unchangable list)
set_var = {"Cairo", "London", "Paris"} # Set (unique values)
dict_var = {"key1": "value2", "key2": "value2"} # Dictionary
null_var = None # Null valueCasting variables
int_var = int(2.8) # 2
float_var = float("3.9") # 3.9- Arithmetic
x = 5 * 2 - ( 3 + 1 ) - Assignment
x += 1 - Logical
x = not(True and (False or True)) - Comparison
x >= 10
def greet(name):
return "Hello, " + name- If else
if x > 10:
print("x is greater than 10")
elif x == 10:
print("x is 10")
else:
print("x is less than 10")
# Short hand if
print("x is 10") if x == 10 else print("x is not 10")- match case
char = 'e'
match char:
case 'a':
print("This character is a")
case 'b':
print("This character is b")
case 'c' | 'd':
print("This character is c or d")
case _:
print("This character is not a , b , c nor d")- loop on range
for i in range(5): # For loop prints 0 to 4
print(i)- loop on array
cities = ["Cairo", "London", "Paris"]
for x in cities:
print(x)- loop on dictionary
dict = {
"key1": "value1",
"key2": "value2"
}
for k, v in dict.items():
print(k, "is", v)- using continue
for x in range(0, 6):
if x == 2:
continue
print(x)
# prints 0 1 3 4 5try:
doSomething()
except:
print("Error occurred")
finally:
print("Done")from datetime import datetime, timedelta
current_datetime = datetime.now()
date_str = "2025-02-23 16:30:00"
parsed_datetime = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
old_date = parsed_datetime - timedelta(days=7) # file course.py
class Course:
def __init__(self, name):
self.name = name
def __str__(self):
return self.name# file student.py
from course import Course
class Student:
def __init__(self, name):
self.name = name
self.courses = []
def enroll(self, course):
self.courses.append(course)
print("Student:", self.name, "enrolled in:", course)
def list_courses(self):
print("Student", self.name, "courses:", ' - '.join(course.name for course in self.courses))
# Example usage
if __name__ == "__main__":
math = Course("Math")
physics = Course("Physics")
adam = Student("Adam")
adam.enroll(math) # Student: Adam enrolled in: Math
adam.enroll(physics) # Student: Adam enrolled in: Physics
adam.list_courses() # Student Adam courses: Math - Physicsinput_val = input("Choose number between 1 and 20:")sample.txt
This is just
To test
How read file in python works
my_file = open("sample.txt")
print("just 4 characters =>", my_file.read(4))
print("just one line => ", my_file.readline())
print("the whole file => ", my_file.read())
my_file.close()Output
just 4 characters => This
just one line => is just
the whole file => To test
How read file in python works
print("Hello") # Hello
print("Hello", "world") # Hello worldmy_file = open("sample.txt", "a")
my_file.write("This text will be added to the end of the file")
my_file.close()my_file = open("sample.txt", "w")
my_file.write("This text will overwrite the contents of the file")
my_file.close()// todo add unit test example