Python Tutorial: From Beginner to Advanced

This comprehensive guide takes you through the fundamentals of Python programming, covering everything from basic syntax to advanced concepts like working with external libraries.

Table of Contents

  1. Introduction to Python
  2. Getting Started
  3. Basic Syntax and Data Types
  4. Control Flow: Conditional Statements and Loops
  5. Functions in Python
  6. Object-Oriented Programming (OOP)
  7. Advanced Data Structures
  8. Working with External Libraries: NumPy and Pandas
  9. Error Handling and Debugging
  10. File Operations in Python
  11. Advanced Topics: Regular Expressions, Networking, etc.
  12. Practice Exercises and Projects

Section 1: Introduction to Python

What is Python?

Python is a high-level programming language known for its simplicity and readability. It was created by Guido van Rossum in the late 1980s.

Why Learn Python?

  • Versatility: Used in web development, data analysis, artificial intelligence, automation, etc.
  • Readability: Clean syntax makes it easy to learn and use.
  • Community-driven: Large open-source community contributes to countless libraries and frameworks.

Section 2: Getting Started

Installing Python

  1. Download Python from the official website.
  2. Follow the installation guide for your operating system (Windows, macOS, Linux).
  3. Verify installation by running python --version in the terminal or command prompt.

First Program

print("Hello, World!")

Section 3: Basic Syntax and Data Types

Variables

  • Python is ** Dynamically typed**, meaning you don’t need to declare a variable’s type before using it.
  • Example:
    x = 5  # integer
    y = "hello"  # string
    z = True  # boolean (True/False)
    

Data Types

| Type | Example | Description | |————-|——————|————-| | int | x = 10 | Integer | | str | y = "Python" | String | | float | z = 3.5 | Float | | bool | a = True, b = False | Boolean |

Basic Operations

# Arithmetic operations:
print(2 + 3)   # Output: 5
print(4 * 6)   # Output: 24
print(10 / 2)  # Output: 5.0 (float division)

Section 4: Control Flow

Conditional Statements

x = 5
if x > 3:
    print("x is greater than 3")
else:
    print(f"{x} is not greater than or equal to 3")

Loops in Python

  • For Loop: Iterate over a sequence.
    for i in range(5):
        print(i)
    
  • While Loop: Repeat until the condition fails.
    n = 0
    while n < 3:
        print(n)
        n += 1
    

Section 5: Functions

Defining a Function

def greet(name):
    return f"Hello, {name}!"
  • Example usage:
    print(greet("Alice"))  # Output: "Hello, Alice!"
    

Built-in Functions

Python has many built-in functions like len(), print(), and sorted().


Section 6: Object-Oriented Programming (OOP)

Classes and Objects

class Car:
    def __init__(self, make, model):
        self.make = make
        self.model = model

car = Car("Toyota", "Camry")
print(car.make)  # Output: Toyota
  • Encapsulation: Data is bundled with methods.
  • Inheritance: You can inherit properties from existing classes.

Section 7: Advanced Data Structures

Lists vs. Tuples

# List:
a = [1, 2, 3]
print(a[0])   # Output: 1 (integer)
# Tuple:
b = ("apple", "banana")
print(b[0])   # Output: "apple" (string)

Dictionaries

d = {"key": "value"}
print(d["key"])  # Output: value

Section 8: Working with External Libraries

NumPy for Numerical Operations

Install and use:

pip install numpy

Example usage:

import numpy as np

arr = np.array([1, 2, 3])
print(arr)   # Output: [1 2 3]

Pandas for Data Analysis

Install and use:

pip install pandas

Example usage:

import pandas as pd

data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df)   # Output: DataFrame with columns Name and Age.

Section 9: Error Handling and Debugging

Try…Except

try:
    print(10 / 0)
except ZeroDivisionError:
    print("Cannot divide by zero.")

Section 10: File Operations in Python

Read from a file:

file = open("data.txt", "r")
content = file.read()
print(content)
file.close()

Write to a file:

with open("new_file.txt", "w") as f:
    f.write("Hello, World!")

Section 11: Advanced Topics

Regular Expressions (regex)

Use re module for pattern matching and manipulation. Example:

import re

pattern = r"hello"
print(re.search(pattern, "The quick brown fox jumps over the lazy dog"))

Networking in Python

Install and use:

pip install requests

Example request:

from urllib.request import urlopen

response = urlopen("https://www.example.com")
print(response.status)  # Output: HTTP status code.

Section 12: Practice Exercises and Projects

Exercise 1: Fibonacci Sequence

Write a function to generate the first n numbers in the Fibonacci sequence.

def fibonacci(n):
    if n == 0:
        return []
    elif n == 1:
        return [0]
    else:
        fib_sequence = [0, 1]
        for i in range(2, n):
            next_num = fib_sequence[i-1] + fib_sequence[i-2]
            fib_sequence.append(next_num)
        return fib_sequence

print(fibonacci(5))   # Output: [0, 1, 1, 2, 3]

Exercise 2: Data Analysis Project

Use Pandas to analyze a dataset (e.g., cars.csv):

import pandas as pd

data = pd.read_csv("cars.csv")
print(data.head())   # Output the first five rows.

This guide covers Python basics, OOP concepts, data structures, and more. For further learning:

  • Documentation: https://docs.python.org/3/
  • Community: https://python.org社区
  • Tutorials: RealPython (realpython.com)