Python Lesson Notes
Introduction to Python
History of Python
Python was created by Guido van Rossum in the late 1980s and released in 1991. It was
designed to emphasize code readability and simplicity. The name 'Python' comes from the
British comedy group Monty Python.
What Can Python Do?
Python is a versatile, high-level programming language used in:
● Web development (Django, Flask)
● Data science (Pandas, NumPy, Matplotlib)
● Machine learning & AI (TensorFlow, Scikit-learn)
● Automation (scripting, web scraping, bots)
● Cybersecurity & ethical hacking
● Internet of Things (IoT)
● Cloud computing and DevOps (AWS Lambda, Docker automation)
● Game development (Pygame)
Why Python?
● Simple and easy to learn (English-like syntax)
● Open-source with a vast community
● Cross-platform compatibility (Windows, Linux, macOS)
● Extensive libraries and frameworks
● Supports object-oriented, procedural, and functional programming
Python Syntax vs. Other Languages
● No semicolons (;) or curly braces ({})
● Uses indentation instead of {}
● Dynamically typed (no need to declare variable types)
● Readable and concise
Installing Python
1. Download Python from python.org
2. Install and set up PATH environment variable
3. Verify installation with python --version
4. Use pip for package management
Basic Python Concepts
The print() Statement
print("Hello, Python!")
Output:
Hello, Python!
Comments in Python
# This is a single-line comment
"""
This is a multi-line comment
"""
Python Data Structures & Data Types
● Numbers (int, float, complex)
● Strings
● Lists, Tuples, Sets, Dictionaries
● Type Casting (int("10"), str(100), float("10.5"))
String Operations
name = "Python"
print(name.upper()) # Convert to uppercase
print(name.lower()) # Convert to lowercase
print(name[0]) # Access first character
print(name[::-1]) # Reverse the string
Output:
PYTHON
python
P
nohtyP
Simple Input & Output
user_input = input("Enter your name: ")
print("Hello,", user_input)
Example Input:
Alice
Example Output:
Hello, Alice
Operators in Python
a = 10
b=5
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Modulus:", a % b)
print("Exponentiation:", a ** b)
Output:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
Modulus: 0
Exponentiation: 100000
Python Program Flow
Indentation in Python
Indentation refers to the spaces or tabs used at the beginning of a line to indicate code
structure. Python enforces indentation to define blocks of code, making it readable and
error-free. Incorrect indentation results in an IndentationError.
if True:
print("This is correctly indented")
Output:
This is correctly indented
Conditional Statements
x = int(input("Enter a number: "))
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is 5")
else:
print("x is less than 5")
Loops in Python
while Loop
i=1
while i <= 5:
print(i)
i += 1
for Loop
for i in range(5):
print(i)
Break & Continue
for i in range(10):
if i == 5:
break # Stops the loop when i is 5
print(i)
Functions in Python
Defining a Function
A function is a reusable block of code that performs a specific task.
def greet(name):
print("Hello,", name)
greet("Alice")
Output:
Hello, Alice
Function with Return Value
def add(a, b):
return a + b
result = add(5, 3)
print("Sum:", result)
Output:
Sum: 8
Default Parameters in Functions
def greet(name="Guest"):
print("Hello,", name)
greet()
greet("John")
Output:
Hello, Guest
Hello, John
Lambda Functions in Python
Lambda functions are anonymous, single-line functions.
square = lambda x: x ** 2
print(square(5))
Output:
25
Lambda with Multiple Arguments
add = lambda x, y: x + y
print(add(3, 7))
Output:
10