Python Course Notes

1. Introduction to Python

  • What is Python?
  • High-level, interpreted, and general-purpose programming language.
  • Emphasizes code readability and simplicity.
  • Popular for web development, data science, automation, etc.
  • Key Features:
  • to learn and write.
  • Extensive libraries (e.g., NumPy, pandas).
  • Cross-platform compatibility.
  • Community support.
  • Installation:
  • Download from python.org.
  • Use an IDE (e.g., PyCharm, VSCode) or Jupyter Notebook for coding.
  • 2. Python Basics

    Syntax and Structure:

    python
    print("Hello, World!") # Output: Hello, World!

    python
    x = 10
    name = "Alice"
    is_active = True
  • Data Types:
  • Numeric: int, float, complex
  • Text: str
  • Boolean: bool
  • Collection: list, tuple, set, dict
  • Type Conversion:
  • python
    int("5") # Converts string to integer float(10) # Converts integer to float

    3. Control Structures

  • Conditional Statements:
  • python
    if age >= 18:
    print("Adult")
    elif age >= 13:
    print("Teen")
    else:
    print("Child")
  • Loops:
  • for loop:

    python
    for i in range(5):
    print(i)

    while loop:

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

    4. Functions

    Defining and Calling Functions:

    python
    greet(name):
    return f"Hello, {name}!"
    print(greet("Alice"))

    Default Arguments:

    python
    def greet(name="World"):
    return f"Hello, {name}!"

    5. Collections

    Lists:

    python
    fruits = ["apple", "banana", "cherry"]
    fruits.append("date")
    print(fruits)

    Dictionaries:

    python
    person = {"name": "Alice", "age": 25}
    print(person["name"])

    Sets:

    python
    nums = {1, 2, 3}
    nums.add(4)

    Tuples:

    python
    colors = ("red", "green", "blue")

    6. Object-Oriented Programming (OOP)
    Classes and Objects:

    python
    class Person:
    def __init__(self, name, age):
    self.name = name
    self.age = age
    def greet(self):
    return f"Hi, I'm {self.name}."
    person = Person("Alice", 25)
    print(person.greet())

    7. File Handling

    Reading Files:

    python
    with open("file.txt", "r") as file:
    content = file.read()

    Writing Files:

    python
    with open("file.txt", "w") as file:
    file.write("Hello, World!")

    8. Exception Handling

    Try-Except Blocks:

    python
    try:
    result = 10 / 0
    except ZeroDivisionError:
    print("Cannot divide by zero!")

    9. Libraries and Modules

    Importing:

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

    Popular Libraries:

  • NumPy: Numerical computations.
  • pandas: Data analysis.
  • matplotlib: Plotting and visualization.
  • 10. Additional Topics

    List Comprehensions:

    python
    squares = [x**2 for x in range(10)]

    Lambda Functions:

    python
    add = lambda x, y: x + y print(add(3, 5))

    Working with APIs:

    python
    import requests
    response = requests.get
    ("https://api.example.com/data")
    print(response.json())