What is Python?

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

YearMilestone
1980sGuido van Rossum started developing Python as a hobby project.
1991Python 0.9.0 released (first public version).
2000Python 2.0 released โ€” introduced list comprehensions and garbage collection.
2008Python 3.0 released โ€” not backward-compatible, introduced many modern features.
2020Python 2 officially discontinued.
PresentPython is one of the most popular programming languages in the world (Top 3 in most rankings).

๐Ÿ”น 3. Key Features of Python

FeatureDescription
Simple and Easy to LearnPython has a clean and readable syntax, close to English.
Interpreted LanguageCode is executed line by line, no need to compile before running.
Dynamically TypedNo need to declare variable types โ€” Python figures it out automatically.
Object-OrientedSupports classes and object-oriented programming.
Cross-platformRuns on Windows, macOS, Linux, and more.
Extensive LibrariesHuge collection of standard and third-party libraries (NumPy, Pandas, Django, etc.).
Community SupportMassive global community, tons of free learning resources and forums.
VersatileUsed in web development, data science, automation, AI/ML, scripting, and more.

๐Ÿ”น 4. Installing Python (Step-by-Step)

โœ… For Windows:

  1. Download the installer
    Go to the official website: https://www.python.org/downloads
  2. Run the installer
    • Check the box: โœ… “Add Python to PATH”
    • Click Install Now
  3. 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)

  1. Open IDLE (comes with Python).
  2. Type: print("Hello, World!")
  3. Press Enter โ€“ Youโ€™ll see the output: Hello, World!

โœ… Using a text editor (VS Code, Notepad++, etc.)

  1. Save the code in a file named hello.py.
  2. Open terminal or command prompt.
  3. 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

OperatorDescriptionExample
+Addition5 + 3 = 8
-Subtraction5 - 3 = 2
*Multiplication5 * 3 = 15
/Division5 / 2 = 2.5
//Floor Division5 // 2 = 2
%Modulus (remainder)5 % 2 = 1
**Exponentiation2 ** 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:

OperatorDescriptionExample
andTrue if both are trueTrue and False โ†’ False
orTrue if at least one is trueTrue or False โ†’ True
notReverses boolean valuenot 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.

OperatorDescriptionExample
==Equal to5 == 5 โ†’ True
!=Not equal to5 != 3 โ†’ True
>Greater than5 > 3 โ†’ True
<Less than5 < 3 โ†’ False
>=Greater than or equal5 >= 5 โ†’ True
<=Less than or equal5 <= 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 BA or Bnot A
TrueTrueTrueTrueFalse
TrueFalseFalseTrueFalse
FalseTrueFalseTrueTrue
FalseFalseFalseFalseTrue

๐Ÿงฎ 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

KeywordDescription
ifChecks a condition and runs the code if True
elifAdditional conditions to test if if is False
elseDefault 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: Stop
  • yellow: Get Ready
  • green: Go
  • Others: Invalid color

Leave a Comment