python fundamentals
python fundamentals
1. Introduction to Python
2. Basic Syntax
# This is a comment
print("Hello, World!") # Prints output
x = 5 # int
y = 3.14 # float
name = "Isaac" # str
is_active = True # bool
4. Control Flow
# If-else example
age = 18
if age >= 18:
print("Adult")
else:
print("Minor")
# Loop example
for i in range(5):
print(i)
5. Functions
def greet(name):
return f"Hello, {name}!"
print(greet("Isaac"))
6. Data Structures
# List
fruits = ["apple", "banana", "cherry"]
# Dictionary
person = {"name": "Isaac", "age": 28}
# Set
unique_numbers = {1, 2, 3}
import math
print(math.sqrt(16)) # 4.0
8. File Handling
class Person:
def __init__(self, name):
self.name = name
def greet(self):
return f"Hi, I’m {self.name}!"
p = Person("Isaac")
print(p.greet())