Python is a high-level, easy-to-read programming language created by Guido van Rossum in 1991.
It is used for:
Python is popular because:
Installing Python
To start coding:
After installing, open Command Prompt (Windows) or Terminal (Mac/Linux) and type:
python --version
If installed correctly, it will show something like:
Python 3.12.1
Writing Your First Python Program
The traditional first program is "Hello, World!"
print("Hello, World!")
Explanation:
print() โ Displays output on the screen"Hello, World!" โ Text (called a string)Python Syntax Basics
Python is indentation-based (spaces matter!).
Example:
if 5 > 2:
print("Five is greater than two")
Notice the space before print() โ that indentation is required.
Variables in Python
Variables store data.
name = "Alice" age = 25 height = 5.6 is_student = True
Variable Rules:
age โ Age)Data Types in Python
Here are the main data types:
| Data Type | Example | Description |
|-----------|----------|------------|
| int | 10 | Whole numbers |
| float | 3.14 | Decimal numbers |
| str | "Hello" | Text |
| bool | True / False | Boolean values |
| list | [1, 2, 3] | Collection (changeable) |
| tuple | (1, 2, 3) | Collection (unchangeable) |
| dict | {"name": "Alice"} | Key-value pairs |
Example:
x = 10 # int y = 3.14 # float name = "John" # string is_valid = False # boolean
Taking User Input
name = input("Enter your name: ")
print("Hello", name)
โ ๏ธ Important: input() always returns a string.
If you want a number:
age = int(input("Enter your age: "))
print("You are", age, "years old.")
Operators in Python
| Type | Operators | Example | |------|-----------|----------| | Arithmetic | + - * / % | 5 + 2 | | Comparison | == != > < >= <= | 5 > 3 | | Logical | and or not | x > 5 and x < 10 |
Example:
a = 10 b = 5 print(a + b) print(a > b)
Conditional Statements (if-else)
Used for decision making.
age = 18
if age >= 18:
print("You are an adult")
else:
print("You are a minor")
With elif:
score = 85
if score >= 90:
print("Grade A")
elif score >= 80:
print("Grade B")
else:
print("Grade C")
Loops in Python
1. For Loop
for i in range(5):
print(i)
Output:
0 1 2 3 4
2. While Loop
count = 0
while count < 5:
print(count)
count += 1
Functions in Python
Functions let you reuse code.
def greet(name):
print("Hello", name)
greet("Alice")
Function with return value:
def add(a, b):
return a + b
result = add(5, 3)
print(result)
Lists in Python
Lists store multiple values.
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Access first item
fruits.append("orange") # Add item
print(len(fruits)) # Length
Dictionaries in Python
Dictionaries store key-value pairs.
student = {
"name": "John",
"age": 20,
"course": "Computer Science"
}
print(student["name"])
Python Libraries
One of Pythonโs biggest strengths is its libraries.
Examples:
math โ Mathematicsrandom โ Random numbersdatetime โ Dates & timeExample:
import random print(random.randint(1, 10))
Comments in Python
Comments are ignored by Python.
# This is a comment """ This is a multi-line comment """
Why Learn Python?
Python is used by major companies like:
It is also heavily used in:
# Hello World
print("Hello, World!")
# If statement example
if 5 > 2:
print("Five is greater than two")
# Variables
name = "Alice"
age = 25
height = 5.6
is_student = True
# Data types examples
x = 10
y = 3.14
name = "John"
is_valid = False
# Input example
# name = input("Enter your name: ")
# print("Hello", name)
# age = int(input("Enter your age: "))
# print("You are", age, "years old.")
# Operators
a = 10
b = 5
print(a + b)
print(a > b)
# Conditional statements
age = 18
if age >= 18:
print("You are an adult")
else:
print("You are a minor")
score = 85
if score >= 90:
print("Grade A")
elif score >= 80:
print("Grade B")
else:
print("Grade C")
# For loop
for i in range(5):
print(i)
# While loop
count = 0
while count < 5:
print(count)
count += 1
# Functions
def greet(name):
print("Hello", name)
greet("Alice")
def add(a, b):
return a + b
result = add(5, 3)
print(result)
# Lists
fruits = ["apple", "banana", "cherry"]
print(fruits[0])
fruits.append("orange")
print(len(fruits))
# Dictionary
student = {
"name": "John",
"age": 20,
"course": "Computer Science"
}
print(student["name"])
# Library example
import random
print(random.randint(1, 10))
# Comments example
# This is a single-line comment
"""
This is a
multi-line comment
"""