What is Python?
Python is a high-level, interpreted programming language known for its simplicity and readability. Created by Guido van Rossum and first released in 1991, Python emphasizes code readability and allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java.
Where Python is Used
⚙️
Automation
Scripting, task automation, system administration
📊
Data Science
Data analysis, machine learning, data visualization
🌐
Web Development
Backend development, APIs, web frameworks (Django, Flask)
🤖
AI & Machine Learning
TensorFlow, PyTorch, scikit-learn
Why Python is Beginner-Friendly
- Simple, readable syntax that resembles English
- No need to declare variable types
- Extensive standard library
- Large community and abundant learning resources
- Cross-platform compatibility
Variables and Data Types
Variables are containers for storing data values. In Python, you don't need to declare the type - Python automatically determines it.
# Variables - containers for data
name = "Alice"
age = 25
height = 5.6
is_student = True
# Display variable values
print("Name:", name)
print("Age:", age)
print("Height:", height)
print("Is Student:", is_student)
# Check data types
print("Type of name:", type(name))
print("Type of age:", type(age))
print("Type of height:", type(height))
print("Type of is_student:", type(is_student))
Numbers
Python supports integers (whole numbers) and floats (decimal numbers).
# Integers
num1 = 10
num2 = 20
sum_result = num1 + num2
print("Sum:", sum_result)
# Floats
price = 19.99
discount = 0.15
final_price = price * (1 - discount)
print("Final Price:", final_price)
# Different number operations
a = 15
b = 4
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b) # Returns integer
print("Modulus:", a % b) # Remainder
print("Exponentiation:", a ** b) # Power
Strings
Strings are sequences of characters enclosed in quotes (single or double).
# String basics
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print("Full Name:", full_name)
# String methods
text = " Hello, Python! "
print("Original:", text)
print("Upper:", text.upper())
print("Lower:", text.lower())
print("Strip:", text.strip()) # Remove whitespace
print("Replace:", text.replace("Python", "World"))
# String formatting
age = 25
message = f"My name is {first_name} and I am {age} years old."
print(message)
# String indexing
word = "Python"
print("First character:", word[0])
print("Last character:", word[-1])
print("Substring:", word[0:3]) # "Pyt"
Boolean
Boolean values are True or False, used for logical operations.
# Boolean values
is_sunny = True
is_raining = False
print("Is it sunny?", is_sunny)
print("Is it raining?", is_raining)
# Boolean operations
print("AND:", is_sunny and is_raining) # False
print("OR:", is_sunny or is_raining) # True
print("NOT:", not is_raining) # True
# Comparison results
age = 18
print("Age >= 18:", age >= 18) # True
print("Age == 20:", age == 20) # False
Basic Input and Output
# Getting input from user
name = input("Enter your name: ")
age = input("Enter your age: ")
# Convert string to integer
age = int(age)
# Display output
print(f"Hello, {name}! You are {age} years old.")
# Multiple outputs
print("Line 1", "Line 2", "Line 3", sep=" | ")
print("First", end=" ")
print("Second", end=" ")
print("Third")
Comments and Formatting
# This is a single-line comment
"""
This is a multi-line comment
or docstring
You can write multiple lines here
"""
# Good formatting practices
# Use meaningful variable names
student_name = "Alice"
student_age = 20
# Add comments to explain complex logic
# Calculate average of three test scores
test1 = 85
test2 = 90
test3 = 88
average = (test1 + test2 + test3) / 3
print(f"Average score: {average}")
Arithmetic Operators
a = 10
b = 3
print("a =", a, ", b =", b)
print("Addition (a + b):", a + b) # 13
print("Subtraction (a - b):", a - b) # 7
print("Multiplication (a * b):", a * b) # 30
print("Division (a / b):", a / b) # 3.333...
print("Floor Division (a // b):", a // b) # 3
print("Modulus (a % b):", a % b) # 1
print("Exponentiation (a ** b):", a ** b) # 1000
Comparison Operators
x = 10
y = 5
print("x =", x, ", y =", y)
print("x == y (equal):", x == y) # False
print("x != y (not equal):", x != y) # True
print("x > y (greater):", x > y) # True
print("x < y (less):", x < y) # False
print("x >= y (greater or equal):", x >= y) # True
print("x <= y (less or equal):", x <= y) # False
Logical Operators
# Logical operators
a = True
b = False
print("a =", a, ", b =", b)
print("a and b:", a and b) # False (both must be True)
print("a or b:", a or b) # True (at least one True)
print("not a:", not a) # False
print("not b:", not b) # True
# Practical example
age = 20
has_license = True
can_drive = age >= 18 and has_license
print("Can drive?", can_drive) # True