Table of Contents
1. Introduction to Python
Python is a high-level, interpreted programming language known for its simplicity and readability. It’s used in web development, data science, automation, AI, and more.
- Created by Guido van Rossum in 1991.
- Open-source and cross-platform.
- Extensive libraries for everything from math to machine learning.
Note: Python emphasizes code readability and simplicity — its philosophy is captured in “The Zen of Python” (
import this).2. Setting Up Python
- Download from python.org and install.
- Check installation:
python --version(orpython3 --version). - Use IDEs like VS Code, PyCharm, or Jupyter Notebook.
print("Hello, Python!")
3. Basic Syntax
- Python uses indentation (spaces or tabs) to define code blocks.
- No semicolons or braces are needed.
name = "Alice"
age = 25
if age > 18:
print(name, "is an adult")
4. Variables and Data Types
name = "John"
age = 30
price = 19.99
is_active = True
print(type(name))
print(type(age))
Tip: Python infers variable types automatically — no need for explicit declarations.
5. Operators
a = 10
b = 3
print(a + b) # 13
print(a % b) # 1
print(a ** b) # 1000 (power)
6. Conditionals and Loops
# If-Else
x = 5
if x > 0:
print("Positive")
else:
print("Non-positive")
# Loop
for i in range(1, 6):
print(i)
7. Functions
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
8. Lists, Tuples, and Dictionaries
# List
fruits = ["Apple", "Banana", "Cherry"]
print(fruits[0])
# Tuple
colors = ("Red", "Green", "Blue")
# Dictionary
person = {"name": "John", "age": 30}
print(person["name"])
9. Object-Oriented Programming (OOP)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name}")
p1 = Person("Alice", 25)
p1.greet()
10. Modules and Packages
# math module
import math
print(math.sqrt(16))
print(math.pi)
Note: You can install external packages with
pip install package-name.11. Best Practices
- Follow PEP 8 style guide for clean code.
- Use virtual environments for project isolation.
- Write readable variable names and add comments.
- Use exceptions instead of error codes.
12. Mini Project — Simple Calculator
def add(a, b): return a + b
def subtract(a, b): return a - b
def multiply(a, b): return a * b
def divide(a, b): return a / b
print("Simple Calculator")
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Choose operation (+, -, *, /): ")
if op == '+':
print("Result:", add(a, b))
elif op == '-':
print("Result:", subtract(a, b))
elif op == '*':
print("Result:", multiply(a, b))
elif op == '/':
print("Result:", divide(a, b))
else:
print("Invalid operator")
13. Conclusion
Python is one of the most versatile and beginner-friendly programming languages. With it, you can build websites, automate tasks, analyze data, and even train AI models.
Next, explore advanced concepts such as file handling, error handling, and libraries like NumPy, Pandas, and Flask.
Comments
Post a Comment