Complete Python Beginner's Tutorial Notes

Jul 4, 2024

Python Tutorial

Introduction

  • Instructor: Mosh Hamedani
  • Goal: Learn to start programming in Python
  • No prior knowledge required
  • Purpose: Useful for data science, machine learning, web development, automation
  • Subscribe for more programming tutorials

Python Applications

  • Multi-purpose language:
    • Machine Learning & AI: No. 1 language for these projects
    • Web Development: Framework example: Django
    • Automation: Save time, increase productivity
  • Example Websites Using Python/Django:
    • YouTube, Instagram, Spotify, Dropbox, Pinterest

Getting Started

  1. Download Python: Go to python.org -> Downloads -> Latest Version

    • Installation: Check "Add Python to PATH" on Windows
  2. Install Code Editor: PyCharm (jetbrains.com/pycharm)

    • Choose Community Edition (Free)
    • Installation process differs slightly between Windows and Mac

Writing Your First Python Program

  1. Create a New Project: Hello, World!
  2. Write Code:
    print("Hello World")
    
    • print: Function to output text
    • String: Textual data within quotes

Variables

  • Usage: Store data in memory for later use
  • Declaration:
    age = 20 
    print(age)
    
    • Variables can store numbers, strings, and booleans
    • Case-sensitive: False vs false

User Input

  • Get input from user:
    name = input("What is your name?")
    print("Hello " + name)
    
    • Use input() function to read terminal input
    • String concatenation with +

Type Conversion

  • Convert string input to integers, floats, or booleans using int(), float(), bool(), and str()
    year = int(input("Birth year?"))
    age = 2020 - year
    print(age)
    

String Methods

  • Common methods:
    • .upper(), .lower(), .find(), .replace(), in operator

Arithmetic Operations

  • Operators: +, -, *, /, //, %, **
    • Modulus %: Returns remainder
    • Exponent **: Power calculation
  • Augmented assignment:
    • +=, -=, *=, /=, //=, %=, **=

Comparison & Logical Operators

  • Comparison: >, >=, <, <=, ==, !=
  • Logical: and, or, not

If Statements

  • Syntax: Conditional logic for decision-making
    if temperature > 30:
        print("It's a hot day")
    elif temperature > 20:
        print("It's a nice day")
    else:
        print("It's cold")
    

Loops

  • While Loops: Repeat block of code while condition is true
    i = 1
    while i <= 5:
        print(i)
        i += 1
    
  • For Loops: Iterate over a sequence (list, range, etc.)
    for number in range(5):
        print(number)
    

Working with Lists

  • List Creation and Access:
    names = ["John", "Bob", "Marsh"]
    print(names[0])  # Access first element
    ``
    
  • List Methods: .append(), .insert(), .remove(), .clear()
  • Slicing: names[0:2]
  • Iterating: for item in names

Tuples

  • Immutable Lists:
    numbers = (1, 2, 3)
    
  • Accessing Elements: numbers[0]
  • Methods: .index(), .count()

Resources

  • Advanced Learning: Comprehensive Python courses at codewithmosh.com
  • Online School: Courses on web/mobile development with certifications