Python Programming Course Notes

Jun 7, 2024

Python Programming Course Notes

Introduction

  • Course Overview: Learning Python programming from scratch
  • Python Popularity: High demand for jobs, used for scripting and automation
  • Ease of Learning: Beginner-friendly, minimal syntax, fast learning curve

Installing Python and Text Editor

  • Python Installation: Download Python from python.org/downloads
    • Choose Python 3.x (future of Python) over Python 2.x (legacy, less supported)
  • Text Editor: Recommended to install PyCharm (Community version is free)
    • Go to jetbrains.com/pycharm to download
    • Configure settings like theme and interpreter (Python 3)
    • Create a new project and Python file

Writing and Running Basic Python Programs

  • Hello World Program: Basic print statement example
    print("Hello, World!")
    
  • Drawing Shapes: Using multiple print statements
    print("/\")
    print("/  \")
    print("/____\")
    

Variables in Python

  • Variable Creation: Storing data values for easy reuse and management
    character_name = "John"
    character_age = 35
    print(character_name, character_age)
    
  • Types of Data for Variables: Strings, numbers, and booleans
    is_male = True
    character_age = 50.5
    
  • Modification of Variables
    character_name = "Mike"
    

Working with Strings

  • String Basics: Creating and using strings
    phrase = "Draft Academy"
    print(phrase.upper())  # Uppercase
    print(phrase.isupper())  # Boolean check
    
  • String Functions: Common functions like .upper(), .lower(), .isupper(), .islower()
  • String Indexing: Accessing individual characters and string methods like .index()
    print(phrase[0])
    print(phrase.index("A"))
    
  • String Concatenation: Combining strings
    print(phrase + " is cool")
    

Working with Numbers

  • Basic Arithmetic: Addition, subtraction, multiplication, division
    print(3 + 4)
    print(3 * 4)
    
  • Advanced Functions: abs(), pow(), max(), min(), round(), math.sqrt()
    import math
    print(abs(-5))
    print(math.sqrt(36))
    

User Input

  • Getting Input from Users
    name = input("Enter your name: ")
    print("Hello " + name + "!")
    

Building a Basic Calculator

  • Calculator Program: Adding two numbers
    num1 = float(input("Enter first number: "))
    num2 = float(input("Enter second number: "))
    result = num1 + num2
    print(result)
    

Conditional Statements (If Statements)

  • Basic If Statements
    is_male = True
    if is_male:
        print("You are a male")
    else:
        print("You are not a male")
    
  • Comparison Operators: ==, !=, <, >, <=, >=
    if num1 == num2:
        print("Equal")
    

Dictionaries in Python

  • Creating Dictionaries
    monthConversions = {
        "Jan": "January",
        "Feb": "February"
    }
    print(monthConversions["Jan"])
    
  • Dictionary Functions: Using .get() and handling missing keys
    print(monthConversions.get("Luv", "Not a valid key"))
    

Loops in Python

  • While Loops: Repeating a block of code while a condition is true
    i = 1
    while i <= 10:
        print(i)
        i += 1
    
  • For Loops: Iterating over a sequence (list, string, etc.)
    friends = ["Jim", "Karen", "Kevin"]
    for friend in friends:
        print(friend)
    

Exponent Function

  • Exponent Function Implementation
    def raise_to_power(base_num, pow_num):
        result = 1
        for index in range(pow_num):
            result *= base_num
        return result
    print(raise_to_power(2, 3))  # Output: 8
    

2D Lists & Nested Loops

  • Creating and Accessing 2D Lists
    number_grid = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
    ]
    print(number_grid[0][0])  # Output: 1
    
  • Nested Loops for 2D Lists
    for row in number_grid:
        for col in row:
            print(col)
    

Functions in Python

  • Defining and Calling Functions
    def say_hi(name):
        print("Hello " + name)
    say_hi("Mike")
    
  • Return Statements in Functions
    def cube(num):
        return num * num * num
    print(cube(3))
    

Building a Basic Translator

  • Simple Translator Program
    def translate(phrase):
        translation = ""
        for letter in phrase:
            if letter in "AEIOUaeiou":
                translation += "g"
            else:
                translation += letter
        return translation
    print(translate("Hello"))  # Output: Hgllg
    

File Handling

  • Reading Files
    employee_file = open("employees.txt", "r")
    print(employee_file.read())
    employee_file.close()
    
  • Writing and Appending to Files
    employee_file = open("employees.txt", "a")
    employee_file.write("Toby - Human Resources")
    employee_file.close()
    

Using Modules

  • Importing Modules
    import math
    print(math.sqrt(36))
    
  • Installing External Modules Using Pip
    pip install [module-name]
    

Classes and Objects

  • Creating Classes and Objects
    class Student:
        def __init__(self, name, major, gpa):
            self.name = name
            self.major = major
            self.gpa = gpa
    student1 = Student("Jim", "Business", 3.1)
    print(student1.name)
    

Object Functions

  • Functions Inside Classes
    class Student:
        def on_honor_roll(self):
            if self.gpa >= 3.5:
                return True
            return False
    student1 = Student("Jim", "Business", 3.1)
    print(student1.on_honor_roll())  # Output: False
    

Inheritance in Python

  • Creating Inheritance: Base and Derived Classes
    class Chef:
        def make_chicken(self):
            print("The chef makes a chicken")
    class ChineseChef(Chef):
        def make_special_dish(self):
            print("The chef makes orange chicken")
    myChef = Chef()
    myChef.make_chicken()
    myChineseChef = ChineseChef()
    myChineseChef.make_special_dish()
    

Python Interpreter

  • Using the Python Interpreter: Quick testing and experiments
    python3
    >>> print("Hello, World!")