Chapter 1: Advanced Data Structures and Algorithms
Python is a high-level, interpreted programming language known for its simplicity and readability. It is widely used in web development, data science, artificial intelligence, automation, and more.
Key Features:
Easy to read and write
Large standard library
Supports multiple programming paradigms (procedural, object-oriented, functional)
Open-source and free
Use print() to display output:
print("Hello, World!")
Comments are ignored by Python and are used to explain code:
# This is a single-line comment
"""
This is a
multi-line comment
or docstring
"""
Variables store data. You don’t need to declare their type explicitly.
name = "Alice" # string
age = 25 # integer
height = 5.7 # float
print(name, age, height)
Python has several built-in data types:
TypeExampleint10float3.14str"Hello"boolTrue / Falselist[1, 2, 3]tuple(1, 2, 3)dict{"a": 1}
a = 10
b = 3
print(a + b) # Addition: 13
print(a - b) # Subtraction: 7
print(a * b) # Multiplication: 30
print(a / b) # Division: 3.3333...
print(a // b) # Floor division: 3
print(a % b) # Modulus: 1
print(a ** b) # Exponentiation: 1000
print(a > b) # True
print(a < b) # False
print(a == b) # False
print(a != b) # True
x = True
y = False
print(x and y) # False
print(x or y) # True
print(not x) # False
age = 18
if age >= 18:
print("You are an adult")
else:
print("You are a minor")
for Loopfor i in range(5):
print(i) # Prints 0 to 4
while Loopcount = 0
while count < 5:
print(count)
count += 1
Functions help organize code into reusable blocks.
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
Lists are ordered, changeable collections.
fruits = ["apple", "banana", "cherry"]
fruits.append("orange") # Add an item
print(fruits[1]) # Access by index
fruits.remove("banana") # Remove an item
print(fruits)
Write a Python program to print your name and age.
Write a program that checks if a number is even or odd.
Write a function that takes two numbers and returns their sum.
Create a list of 5 of your favorite foods and print each item using a loop.
Write a program to print numbers from 10 down to 1 using a loop.
No Comments Yet