Python Tutorial Summary

Jul 25, 2024

Python Tutorial Summary

Introduction

  • Instructor: Mosh Hamedani
  • Objective: Learn Python programming from scratch for data science, machine learning, or web development.
  • No prior knowledge required
  • Purpose: To equip learners with basic Python programming skills
  • Applications of Python:
    • Machine Learning and AI
    • Web Development (using Django)
    • Automation

Setting Up Python Environment

Download Python

  • Website: python.org
  • Steps:
    • Go to Downloads and select the latest version
    • Install the package
    • On Windows: Check the box to add Python to PATH
    • Follow the installation steps

Install Code Editor

  • Recommended Editor: PyCharm (from JetBrains)
  • Steps:
    • Download PyCharm (Community Edition)
    • Install the package
    • Configure initial settings (skip remaining and set defaults)
    • Create a new project (e.g., hello_world)

Writing and Running Python Code

Hello World Program

  • Create file: New Python file (app.py)
  • Code:
    print("Hello, World!")
    
  • Run: Execute the program to print the output in terminal

Variables in Python

  • Definition: Used to temporarily store data

  • Example:

    age = 20
    print(age)
    age = 30
    print(age)
    
  • Types of Variables:

    • Integer
    • Float
    • String
    • Boolean
  • Example for different types:

    price = 19.95  # float
    first_name = "Mosh"  # string
    is_online = True  # boolean
    
  • Exercise: Declare variables for hospital check-in

User Input

  • Function: input() to receive user input
  • Example:
    name = input("What is your name? ")
    print("Hello " + name)
    
  • Type Conversion Functions:
    • int(), float(), bool(), str()
  • Example: Calculate user’s age from birth year
    birth_year = int(input("Enter your birth year: "))
    age = 2020 - birth_year
    print(age)
    

Exercise

  • Write a basic calculator program to add two numbers
  • Solution:
    first = float(input("Enter first number: "))
    second = float(input("Enter second number: "))
    sum = first + second
    print("Sum is: " + str(sum))
    

Working with Strings

  • Common Methods:
    • .upper(), .lower(), .find(), .replace()
  • Examples:
    course = "Python for Beginners"
    print(course.upper())
    print(course.find("for"))
    print(course.replace("Beginners", "Experts"))
    print("Python" in course)
    

Arithmetic Operations

  • Operators: +, -, *, /, //, %, **
  • Example:
    print(10 + 3)
    print(10 - 3)
    print(10 * 3)
    print(10 / 3)
    print(10 // 3)
    print(10 % 3)
    print(10 ** 3)
    
  • Augmented Assignment:
    x = 10
    x += 3  # same as x = x + 3
    print(x)
    

Operator Precedence and Parentheses

  • Example:
    x = 10 + 3 * 2  # Result: 16
    x = (10 + 3) * 2  # Result: 26
    

Comparison and Logical Operators

  • Comparison: >, >=, <, <=, ==, !=
  • Logical: and, or, not
  • Examples:
    price = 25
    print(price > 10 and price < 30)
    print(price > 10 or price < 30)
    print(not price > 10)
    

Conditional Statements

  • Structure:

    • if, elif, else
  • Example:

    temperature = 35
    if temperature > 30:
        print("It's a hot day")
        print("Drink plenty of water")
    elif temperature > 20:
        print("It's a nice day")
    elif temperature > 10:
        print("It's a bit cold")
    else:
        print("It's cold")
    print("Done")
    
  • Exercise: Write a weight converter program

Loops

While Loop

  • Example:
    i = 1
    while i <= 5:
        print(i)
        i += 1
    
  • Example with string multiplication:
    i = 1
    while i <= 10:
        print(i * '*')
        i += 1
    

For Loop

  • Example:
    numbers = [1, 2, 3, 4, 5]
    for item in numbers:
        print(item)
    

Range Function

  • Usage: Generates a sequence of numbers
  • Examples:
    for number in range(5):
        print(number)  # 0 to 4
    
    for number in range(5, 10):
        print(number)  # 5 to 9
    
    for number in range(5, 10, 2):
        print(number)  # 5, 7, 9
    

Data Structures

Lists

  • Example:
    names = ["John", "Bob", "Mosh", "Sam", "Mary"]
    print(names)
    print(names[0])  # 'John'
    print(names[-1])  # 'Mary'
    names[0] = "Jonathan"
    print(names[0:3])  # ['Jonathan', 'Bob', 'Mosh']
    
  • List Methods:
    • append(), insert(), remove(), clear(), in, len()

Tuples

  • Example:
    numbers = (1, 2, 3)
    
  • Immutable: Cannot modify elements after creation