Python is a high-level, interpreted, general-purpose programming language known for its simple, readable syntax and wide range of applications. Created by Guido van Rossum and released in 1991, Python supports multiple programming paradigms including procedural, object-oriented, and functional programming. It’s widely used in web development, data analysis, machine learning, automation, scientific computing, and more.
๐น 2. History of Python
Year | Milestone |
---|---|
1980s | Guido van Rossum started developing Python as a hobby project. |
1991 | Python 0.9.0 released (first public version). |
2000 | Python 2.0 released โ introduced list comprehensions and garbage collection. |
2008 | Python 3.0 released โ not backward-compatible, introduced many modern features. |
2020 | Python 2 officially discontinued. |
Present | Python is one of the most popular programming languages in the world (Top 3 in most rankings). |
๐น 3. Key Features of Python
Feature | Description |
---|---|
Simple and Easy to Learn | Python has a clean and readable syntax, close to English. |
Interpreted Language | Code is executed line by line, no need to compile before running. |
Dynamically Typed | No need to declare variable types โ Python figures it out automatically. |
Object-Oriented | Supports classes and object-oriented programming. |
Cross-platform | Runs on Windows, macOS, Linux, and more. |
Extensive Libraries | Huge collection of standard and third-party libraries (NumPy, Pandas, Django, etc.). |
Community Support | Massive global community, tons of free learning resources and forums. |
Versatile | Used in web development, data science, automation, AI/ML, scripting, and more. |
๐น 4. Installing Python (Step-by-Step)
โ For Windows:
- Download the installer
Go to the official website: https://www.python.org/downloads - Run the installer
- Check the box: โ “Add Python to PATH”
- Click Install Now
- Verify installation
Open Command Prompt and type:python --version
You should see something like:Python 3.12.0
โ For macOS:
- Use Homebrew (recommended):
brew install python
- Or download the
.pkg
installer from the official website.
โ For Linux (Ubuntu/Debian):
- Use the package manager:
sudo apt update sudo apt install python3
- Check installation:
python3 --version
๐น 5. Writing and Running Your First Python Program
โ Using IDLE (Python’s built-in editor)
- Open IDLE (comes with Python).
- Type:
print("Hello, World!")
- Press Enter โ Youโll see the output:
Hello, World!
โ Using a text editor (VS Code, Notepad++, etc.)
- Save the code in a file named
hello.py
. - Open terminal or command prompt.
- Run:
python hello.py
Python is an ideal language for beginners due to its readable syntax and active community. Learning Python opens doors to multiple fields like web development, data analysis, and AI. Mastering the basicsโlike variables, functions, and control structuresโsets the foundation for advanced Python development.
๐ง Variables, Data Types, and Operators in Python
In Python, variables are used to store data, and data types define what kind of data the variable holds. Operators perform operations on these variables and data.
๐น 1. Variables in Python
A variable is a name that refers to a value stored in memory.
โ Syntax:
variable_name = value
โ Example:
x = 10
name = "Alice"
pi = 3.14
- Variables are dynamically typed โ you donโt need to declare their type explicitly.
- Variable names must start with a letter or underscore (
_
) and cannot start with a digit.
๐น 2. Data Types in Python
Python has several built-in data types. The most commonly used basic data types are:
โ
a. Integer (int
)
Whole numbers (positive or negative) without decimals.
a = 5
b = -42
print(type(a)) # Output: <class 'int'>
โ
b. Float (float
)
Numbers that contain decimal points.
pi = 3.14159
g = -9.8
print(type(pi)) # Output: <class 'float'>
โ
c. String (str
)
A sequence of characters enclosed in single or double quotes.
name = "Python"
greeting = 'Hello'
print(type(name)) # Output: <class 'str'>
โ
d. Boolean (bool
)
Only two possible values: True
or False
.
is_active = True
is_admin = False
print(type(is_active)) # Output: <class 'bool'>
๐น 3. Type Conversion (Casting)
You can convert one data type into another using casting functions:
int("10") # โ 10 (int)
float("3.14") # โ 3.14 (float)
str(100) # โ "100" (str)
bool(0) # โ False
bool(1) # โ True
๐น 4. Arithmetic Operators
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 5 - 3 = 2 |
* | Multiplication | 5 * 3 = 15 |
/ | Division | 5 / 2 = 2.5 |
// | Floor Division | 5 // 2 = 2 |
% | Modulus (remainder) | 5 % 2 = 1 |
** | Exponentiation | 2 ** 3 = 8 |
โ Example:
a = 10
b = 3
print(a + b) # 13
print(a / b) # 3.333...
print(a // b) # 3
print(a % b) # 1
๐น 5. Logical (Boolean) Operators
Used to combine multiple boolean expressions:
Operator | Description | Example |
---|---|---|
and | True if both are true | True and False โ False |
or | True if at least one is true | True or False โ True |
not | Reverses boolean value | not True โ False |
โ Example:
a = True
b = False
print(a and b) # False
print(a or b) # True
print(not a) # False
๐น 6. Comparison Operators
Used to compare two values and return a Boolean result.
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 5 โ True |
!= | Not equal to | 5 != 3 โ True |
> | Greater than | 5 > 3 โ True |
< | Less than | 5 < 3 โ False |
>= | Greater than or equal | 5 >= 5 โ True |
<= | Less than or equal | 5 <= 3 โ False |
๐งช Practice Code Example
x = 10
y = 3
# Arithmetic
print("Addition:", x + y)
print("Exponentiation:", x ** y)
# Logical
is_sunny = True
has_umbrella = False
print("Can go out:", is_sunny or has_umbrella)
# Comparison
print("x greater than y?", x > y)
๐ง 1. Variables & Data Types
๐งพ Code:
x = 5 # Integer
y = 3.14 # Float
name = "Alice" # String
is_happy = True # Boolean
๐ผ๏ธ Visualization:
+------------+----------+----------------+
| Variable | Type | Value |
+------------+----------+----------------+
| x | int | 5 |
| y | float | 3.14 |
| name | str | "Alice" |
| is_happy | bool | True |
+------------+----------+----------------+
โ 2. Arithmetic Operators
๐งพ Code:
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333...
print(a % b) # 1
print(a ** b) # 1000
๐ผ๏ธ Visualization:
a = 10 b = 3
Addition โ 10 + 3 = 13
Subtraction โ 10 - 3 = 7
Multiplication โ 10 * 3 = 30
Division โ 10 / 3 = 3.33...
Modulus โ 10 % 3 = 1
Power โ 10 ** 3 = 1000
โ 3. Boolean and Logical Operators
๐งพ Code:
is_adult = True
has_ticket = False
print(is_adult and has_ticket) # False
print(is_adult or has_ticket) # True
print(not is_adult) # False
๐ผ๏ธ Truth Table:
A (is_adult) | B (has_ticket) | A and B | A or B | not A |
---|---|---|---|---|
True | True | True | True | False |
True | False | False | True | False |
False | True | False | True | True |
False | False | False | False | True |
๐งฎ 4. Comparison Operators
๐งพ Code:
a = 7
b = 10
print(a == b) # False
print(a != b) # True
print(a < b) # True
print(a >= b) # False
๐ผ๏ธ Visualization:
a = 7, b = 10
a == b โ False (7 is not equal to 10)
a != b โ True (7 is not equal to 10)
a < b โ True (7 is less than 10)
a >= b โ False (7 is not greater than or equal to 10)
๐ 5. Type Conversion (Casting)
๐งพ Code:
a = "10"
b = int(a) + 5
print(b) # Output: 15
๐ผ๏ธ Visualization:
Step 1: a = "10" (string)
Step 2: Convert "10" โ 10 (int)
Step 3: 10 + 5 = 15
๐ธ What Are Control Structures?
Control structures allow the flow of a program to change based on conditions. Instead of executing code sequentially line-by-line, a program can make decisions, perform actions conditionally, or repeat code (loops).
In this section, we focus on decision-making control structures using:
if
elif
(short for “else if”)else
๐ธ 1. The if
Statement
The if
statement tests a condition. If the condition is True
, the block of code under it is executed.
๐น Syntax:
if condition:
# Code block to execute if condition is true
๐น Example:
x = 10
if x > 5:
print("x is greater than 5")
Output:
x is greater than 5
๐ธ 2. The elif
Statement (Else If)
The elif
statement allows checking multiple conditions after an initial if
. If the if
condition is False, the program checks elif
. Only the first condition that evaluates to True
is executed.
๐น Syntax:
if condition1:
# Code block if condition1 is true
elif condition2:
# Code block if condition2 is true
๐น Example:
x = 15
if x < 10:
print("x is less than 10")
elif x < 20:
print("x is between 10 and 20")
Output:
x is between 10 and 20
๐ธ 3. The else
Statement
The else
statement is used when none of the if
or elif
conditions are True
. It’s the default block that runs when all other conditions fail.
๐น Syntax:
if condition1:
# Block 1
elif condition2:
# Block 2
else:
# Block 3 (default)
๐น Example:
x = 25
if x < 10:
print("x is less than 10")
elif x < 20:
print("x is between 10 and 20")
else:
print("x is 20 or greater")
Output:
x is 20 or greater
๐ธ Visual Flow Diagram
Hereโs a simplified version of the logic:
+-------------------+
| if condition True |-----> Execute block A
+-------------------+
|
False โ
+-------------------+
| elif condition |-----> Execute block B
+-------------------+
|
False โ
+-------------------+
| else |-----> Execute block C
+-------------------+
๐ธ Indentation in Python
Python uses indentation to define blocks of code. This is critical in if-elif-else
structures. Misaligned indentation will result in errors.
Example (Correct):
x = 5
if x > 3:
print("Yes")
Example (Incorrect โ will cause an error):
x = 5
if x > 3:
print("Yes") # IndentationError
๐ธ Combining Conditions
You can use logical operators like and
, or
, and not
for complex decisions.
Example:
age = 25
has_license = True
if age >= 18 and has_license:
print("You can drive.")
else:
print("You cannot drive.")
๐ธ Real-Life Use Case
Letโs say weโre creating a simple grading system:
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")
Output:
Grade: B
โ Summary
Keyword | Description |
---|---|
if | Checks a condition and runs the code if True |
elif | Additional conditions to test if if is False |
else | Default block if no conditions are met |
โ INTERACTIVE EXERCISES
๐น Exercise 1: Even or Odd
๐ Problem: Write a program that checks if a number is even or odd.
# Input a number
num = int(input("Enter a number: "))
# TODO: Check if number is even or odd
๐ง Hint: Use the modulus operator %
.
๐น Exercise 2: Positive, Negative, or Zero
๐ Problem: Write a program that classifies a number as positive, negative, or zero.
# Input a number
num = float(input("Enter a number: "))
# TODO: Print whether the number is positive, negative, or zero
๐น Exercise 3: Age-based Permissions
๐ Problem: Based on a person’s age, print what they are allowed to do.
# Rules:
# Age < 13: "You are a child."
# 13 <= Age < 18: "You are a teenager."
# 18 <= Age < 60: "You are an adult."
# Age >= 60: "You are a senior citizen."
age = int(input("Enter your age: "))
# TODO: Write if-elif-else logic
๐น Exercise 4: Grading System
๐ Problem: Write a program that assigns grades based on the score.
# Rules:
# 90 - 100: A
# 80 - 89 : B
# 70 - 79 : C
# 60 - 69 : D
# <60 : F
score = int(input("Enter your score (0-100): "))
# TODO: Use if-elif-else to print the grade
๐น Exercise 5: Leap Year Checker
๐ Problem: Write a program to check if a year is a leap year.
# Rule: A year is leap if:
# (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)
year = int(input("Enter a year: "))
# TODO: Check leap year logic and print result
๐ก BONUS CHALLENGES (For Practice)
๐ธ Challenge 1: Number Comparison
๐ Problem: Input two numbers and print which one is greater or if they are equal.
๐ธ Challenge 2: Password Strength Checker
๐ Problem: Input a password string. If it’s more than 8 characters and contains numbers, say “Strong Password”, else “Weak Password”.
๐ง Hint: Use len(password)
and any(char.isdigit() for char in password)
๐ธ Challenge 3: Traffic Light System
๐ Problem: Input a color (red
, yellow
, green
). Output the action:
red
: Stopyellow
: Get Readygreen
: Go- Others: Invalid color