Beginner's Guide to Python Programming

Aug 1, 2024

Python Programming Tutorial Notes

Introduction

  • Instructor: Mosh Hamadani
  • Focus: Python programming from scratch for various applications:
    • Data Science
    • Machine Learning
    • Web Development
  • Target Audience: No prior knowledge required.

Why Learn Python?

  • Multi-purpose Language: Used for various fields including:
    • Machine Learning and AI (most popular language)
    • Web Development (e.g., using Django)
    • Automation (saving time on repetitive tasks)
  • Examples of Websites Using Python:
    • YouTube
    • Instagram
    • Spotify
    • Dropbox
    • Pinterest

Setting Up Python

  1. Download Python:
    • Go to python.org
    • Download the latest version, check "Add Python to PATH" during installation.
  2. Install Code Editor:
    • Recommended: PyCharm (community edition is free)
    • Install instructions for Windows and Mac provided.

First Project & Code Execution

  1. Creating a Project in PyCharm:
    • Create a new project, name it (e.g., Hello World).
  2. Writing Your First Code:
    • Use print("Hello World") to print to the terminal.
    • Run the Code: Via menu or using shortcuts.

Variables in Python

  • Definition: Used to store data temporarily in memory.
  • Example:
    • age = 20
    • Print: print(age)
  • Changing Variable Value:
    • Example:
      age = 30
      print(age)
      

Variable Types

  1. String: Textual data, use quotes (single or double).
    • Example: name = "Mosh"
  2. Boolean: True/False values.
    • Example: is_online = True
  3. Integer/Float: Whole numbers or decimal numbers.
    • Example: price = 19.95

User Input

  • Use input() to get user input from the terminal.
  • Example:
    name = input("What is your name?")
    print("Hello " + name)
    

Type Conversion

  • Use functions to convert types:
    • int(): Convert to integer
    • float(): Convert to float
    • str(): Convert to string

Arithmetic Operations

  • Common operators: +, -, *, /, % (modulus), ** (exponentiation)
  • Example:
    x = 10 + 3 * 2  # Operator precedence applies
    
  • Augmented Assignment Operators:
    • +=, -=, *=, /=

Comparison Operators

  • Used to compare values: >, <, >=, <=, ==, !=

Logical Operators

  • Combine conditions:
    • and, or, not
    • Example: if price > 10 and price < 30:

Control Flow with If Statements

  • If Statement Syntax:
    if condition:
        # code block
    else:
        # alternative code block
    
  • Example:
    if temperature > 30:
        print("It's a hot day.")
    

While Loops

  • Repeat a block of code while a condition is true.
  • Example:
    i = 1
    while i <= 5:
        print(i)
        i += 1
    

Lists

  • Definition: Store sequences of objects, defined in [ ].
  • Examples:
    names = ["John", "Bob", "Mosh"]
    print(names[0])  # Outputs: John
    
  • List Methods: append(), insert(), remove(), clear(), len()

Tuples

  • Definition: Similar to lists but immutable.
  • Example:
    numbers = (1, 2, 3)
    

Summary

  • This tutorial covers foundational Python concepts for beginners.
  • Further learning resources available through Mosh's online coding school.