Python Programming: A Beginner-Friendly Guide


Python is one of the most popular programming languages, known for its simplicity and versatility. Used by companies like Google, Netflix, and NASA, it’s perfect for beginners and powerful enough for advanced projects.

Why Python?

  • Easy to learn - Readable syntax, less boilerplate
  • Versatile - Web, data science, AI, automation, games
  • Huge ecosystem - Libraries for everything
  • Great community - Tons of tutorials and help
  • High demand - Excellent career prospects

Installation

Windows/Mac: Download from python.org

Linux: ```bash sudo apt install python3 ```

Verify: ```bash python3 —version ```

Your First Program

```python print(“Hello, World!”) ```

Run it: ```bash python3 hello.py ```

Variables and Data Types

```python

Numbers

age = 25 price = 19.99 big_number = 1_000_000 # Underscores for readability

Strings

name = “Alice” message = ‘Hello’ multiline = """This is a multiline string"""

Booleans

is_active = True is_logged_in = False

None (like null)

result = None

Type checking

print(type(age)) # <class ‘int’> ```

String Operations

```python name = “Alice”

Concatenation

greeting = “Hello, ” + name

f-strings (modern way)

greeting = f”Hello, {name}” message = f”2 + 2 = {2 + 2}“

Methods

print(name.upper()) # ALICE print(name.lower()) # alice print(name.startswith(“A”)) # True print(len(name)) # 5

Slicing

text = “Python” print(text[0]) # P (first character) print(text[-1]) # n (last character) print(text[0:3]) # Pyt (substring) print(text[:3]) # Pyt print(text[3:]) # hon ```

Lists (Arrays)

```python

Creating lists

fruits = [“apple”, “banana”, “orange”] numbers = [1, 2, 3, 4, 5] mixed = [1, “two”, 3.0, True]

Accessing elements

first = fruits[0] # apple last = fruits[-1] # orange

Adding elements

fruits.append(“grape”) fruits.insert(1, “mango”) # Insert at index 1

Removing elements

fruits.remove(“banana”) # Remove by value popped = fruits.pop() # Remove last del fruits[0] # Remove by index

List operations

length = len(fruits) fruits.sort() fruits.reverse()

Checking membership

if “apple” in fruits: print(“Found apple!”)

List comprehension

squares = [x**2 for x in range(10)] evens = [x for x in range(20) if x % 2 == 0] ```

Dictionaries (Objects)

```python

Creating dictionaries

user = { “name”: “Alice”, “age”: 30, “city”: “New York” }

Accessing values

name = user[“name”] age = user.get(“age”) # Safer, returns None if missing

Adding/updating

user[“email”] = “alice@example.com” user[“age”] = 31

Removing

del user[“city”] removed = user.pop(“email”)

Checking keys

if “name” in user: print(user[“name”])

Iterating

for key, value in user.items(): print(f”{key}: {value}“)

Dictionary comprehension

squares = {x: x**2 for x in range(5)} ```

Control Flow

If Statements

```python age = 18

if age >= 18: print(“Adult”) elif age >= 13: print(“Teenager”) else: print(“Child”)

Ternary operator

status = “Adult” if age >= 18 else “Minor” ```

Loops

```python

For loop

for i in range(5): # 0 to 4 print(i)

for i in range(1, 6): # 1 to 5 print(i)

for fruit in fruits: print(fruit)

While loop

count = 0 while count < 5: print(count) count += 1

Break and continue

for i in range(10): if i == 5: break # Exit loop if i % 2 == 0: continue # Skip to next iteration print(i) ```

Functions

```python

Basic function

def greet(name): return f”Hello, {name}!”

message = greet(“Alice”)

Default parameters

def greet(name=“Guest”): return f”Hello, {name}!”

Multiple parameters

def add(a, b): return a + b

Multiple return values

def get_user(): return “Alice”, 30, “alice@example.com

name, age, email = get_user()

Variable arguments

def sum_all(*numbers): return sum(numbers)

total = sum_all(1, 2, 3, 4, 5)

Keyword arguments

def create_user(**kwargs): return kwargs

user = create_user(name=“Alice”, age=30) ```

Classes and Objects

```python class User: # Constructor def init(self, name, age): self.name = name self.age = age self.active = True

# Method
def greet(self):
    return f"Hi, I'm {self.name}"

# String representation
def __str__(self):
    return f"User({self.name}, {self.age})"

Creating instance

user = User(“Alice”, 30) print(user.greet()) print(user.name)

Inheritance

class Admin(User): def init(self, name, age, permissions): super().init(name, age) self.permissions = permissions

def greet(self):
    return f"Hi, I'm {self.name}, an admin"

```

File Operations

```python

Writing file

with open(“file.txt”, “w”) as f: f.write(“Hello, World!\n”) f.write(“Second line\n”)

Reading file

with open(“file.txt”, “r”) as f: content = f.read() print(content)

Reading line by line

with open(“file.txt”, “r”) as f: for line in f: print(line.strip())

Appending

with open(“file.txt”, “a”) as f: f.write(“New line\n”) ```

Exception Handling

```python try: number = int(input(“Enter number: ”)) result = 10 / number except ValueError: print(“Invalid number!”) except ZeroDivisionError: print(“Cannot divide by zero!”) except Exception as e: print(f”Error: {e}”) else: print(f”Result: {result}”) finally: print(“Cleanup code here”) ```

List of Useful Libraries

Built-in Modules

```python

Math

import math print(math.sqrt(16)) print(math.pi)

Random

import random print(random.randint(1, 10)) print(random.choice([“apple”, “banana”, “orange”]))

Datetime

from datetime import datetime, timedelta now = datetime.now() tomorrow = now + timedelta(days=1) print(now.strftime(“%Y-%m-%d”))

JSON

import json data = {“name”: “Alice”, “age”: 30} json_string = json.dumps(data) parsed = json.loads(json_string) ```

Common Patterns

List Filtering

```python numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Filter even numbers

evens = [n for n in numbers if n % 2 == 0]

Map: double each number

doubled = [n * 2 for n in numbers]

Filter and map

even_squared = [n**2 for n in numbers if n % 2 == 0] ```

Dictionary Operations

```python

Merge dictionaries

dict1 = {“a”: 1, “b”: 2} dict2 = {“c”: 3, “d”: 4} merged = {**dict1, **dict2}

Filter dictionary

users = {“alice”: 30, “bob”: 25, “charlie”: 35} adults = {k: v for k, v in users.items() if v >= 30} ```

Working with APIs

```python import requests

GET request

response = requests.get(“https://api.example.com/users”) data = response.json()

POST request

response = requests.post( “https://api.example.com/users”, json={“name”: “Alice”, “email”: “alice@example.com”} ) ```

Best Practices

  1. Use meaningful names - user_count not x
  2. Follow PEP 8 - Python style guide
  3. Use list comprehensions - More Pythonic
  4. Use context managers - with for files
  5. Write docstrings - Document functions
  6. Use virtual environments - Isolate dependencies

```python def calculate_total(items): """ Calculate total price of items.

Args:
    items (list): List of item prices

Returns:
    float: Total price
"""
return sum(items)

```

Next Steps

  1. Practice on LeetCode - Solve problems
  2. Build projects - Todo app, calculator, web scraper
  3. Learn frameworks - Django (web), pandas (data)
  4. Read documentation - docs.python.org

Python opens doors to endless possibilities. Start coding today!