Python Programming - Class 3

Jul 9, 2024

Python Programming - Class 3

Introduction

  • Third class session covering Python basics.
  • Encouraged student participation for feedback and questions about the previous class.
  • Recap of the last session's content.

Recap of Last Class

Python Overview

  • Python is an interpreted language, not a compiled language.
    • Code is executed line by line.
  • Example:
    print("Hello, World!")
    print("End of code")
    

Jupyter Notebook Tips

  • Use Shift + Enter to run code and move to the next cell.
  • Indentation matters in Python for the code to run correctly.
  • Example of a function with indentation:
    def my_function():
        print("Hello, World!")
    

Variables

  • Variables are essential in Python for storing objects.
  • Variables can be thought of as labels for containers.
  • Example:
    name = "John"
    age = 30
    
  • Use id() to check the memory location of a variable.
  • Example:
    a = "name"
    print(id(a))
    b = "name"
    print(id(b)) # Same as id(a) because they contain the same object.
    

Data Types

  • Integers (whole numbers)
    number = 2
    print(type(number)) # <class 'int'>
    
  • Float (decimal numbers)
    variable = 2.5
    print(type(variable)) # <class 'float'>
    
  • String (text)
    name = "John"
    print(type(name)) # <class 'str'>
    
  • None (null value)
    variable = None
    print(type(variable)) # <class 'NoneType'>
    

Data Structures

Lists

  • Used to store multiple items in a single variable.
  • Created using square brackets [].
  • Example:
    names = ["Ala", "Max", "Felix"]
    
  • Indexing:
    print(names[0]) # Ala
    print(names[-1]) # Felix
    
  • Slicing:
    print(names[0:2]) # ['Ala', 'Max']
    

Dictionaries

  • Key-value pairs, created using curly braces {}.
  • Example:
    student = {
        "name": "John",
        "email": "john@example.com",
        "age": 21
    }
    
  • Accessing dictionary values via keys:
    print(student["name"]) # John
    
  • Dictionaries can also contain lists:
    students = {
        "names": ["John", "Jane"],
        "ages": [21, 22]
    }
    

Sets

  • Unordered collection of unique elements.
  • Define using curly braces {}.
  • Removes duplicate elements:
    my_set = {1, 2, 2, 3}
    print(my_set) # {1, 2, 3}
    

Operators

  • Arithmetic operations (addition, subtraction, multiplication, division).
  • Floor division: //
    print(10 // 3) # 3
    
  • Modulus (remainder): %
    print(10 % 3) # 1
    
  • Exponentiation: **
    print(2 ** 3) # 8
    

Today's Topic: Control Flow

If-Else Statements

  • Executes a block of code only if a specified condition is true.
  • Syntax:
    if condition:
        # execute this code
    elif another_condition:
        # execute this code
    else:
        # execute this code
    

Loops

For Loops

  • Used to iterate over a sequence (like a list, tuple, or string).
  • Example:
    for i in range(5):
        print(i)
    
  • Loops over iterable elements.

While Loops

  • Runs as long as a certain condition is true.
  • Example:
    i = 0
    while i < 10:
        print(i)
        i += 1
    
  • Be cautious to avoid infinite loops; use break to stop the loop if necessary.

Break and Continue

  • break to exit the loop.
  • continue to skip the current iteration and continue with the next one.
  • Example:
    for i in range(10):
        if i == 5:
            break
        print(i)
    

Next Class Topic

  • Introduction to Object-Oriented Programming (OOP).
  • Further topics: importing modules, NumPy, Pandas for data science.