Comprehensive Guide to Learning Python

Aug 7, 2024

Python Programming Tutorial

Introduction

  • Learn Python programming for data science, machine learning, or web development.
  • No prior knowledge needed; everything taught from scratch.
  • Instructor: Mosh Hamadani.
  • Subscribe for new weekly videos.

Uses of Python

  • Multi-purpose programming language.
  • Machine Learning and AI: Python is the top choice for projects.
  • Web Development: Popular frameworks include Django.
    • Examples of websites built with Python/Django: YouTube, Instagram, Spotify, Dropbox, Pinterest.
  • Automation: Automate repetitive tasks to save time and increase productivity.

Setting Up Python

  1. Download Python from python.org:
    • Go to Downloads and select the latest version.
    • Check "Add Python to PATH" during installation (Windows).
  2. Install a code editor:
  3. Create a new project in PyCharm:
    • Specify location and name (e.g., hello_world).
    • Create a new Python file (e.g., app.py).

Writing Your First Python Code

  • Print Function: print("Hello World")
    • Strings are sequences of characters, surrounded by quotes.
  • Run the code via the Run menu or shortcut (Ctrl+Shift+R on Mac).

Variables in Python

  • Variables store temporary data: e.g., age = 20
  • To print variable values, avoid quotes: print(age).
  • Variables can be updated: age = 30

Types of Variables

  • Integer: Whole numbers (e.g., age = 20).
  • Float: Numbers with decimals (e.g., price = 19.95).
  • String: Textual data (e.g., first_name = "Mosh").
  • Boolean: True/False values (e.g., is_online = True).
  • Python is case-sensitive.

User Input

  • Use the input() function for user input:
    • Example: name = input("What is your name? ")
  • String concatenation: print("Hello " + name).

Type Conversion

  • Convert strings to integers with int(), floats with float(), and vice versa with str().
  • Example of calculating age from birth year:
    birth_year = int(input("Enter your birth year: "))
    age = 2020 - birth_year
    print(age)
    

Arithmetic Operators

  • Basic operations: + (addition), - (subtraction), * (multiplication), / (division).
  • Division Types:
    • Single Slash / for float results.
    • Double Slash // for integer results.
  • Modulus %: gives remainder.
  • Exponent **: raises to power.

Comparison Operators

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

Logical Operators

  • Combine conditions: and, or, not.

If Statements

  • Make decisions based on conditions:
    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 code until condition fails.
    i = 1
    while i <= 5:
        print(i)
        i += 1
    
  • For Loops: Iterate over lists or ranges.
    for number in range(1, 6):
        print(number)
    

Lists

  • Create lists with square brackets:
    names = ["John", "Bob", "Mosh"]
    
  • Access elements by index (zero-based).
  • Modify lists with methods like append(), insert(), remove(), and clear().

Tuples

  • Similar to lists, but immutable (cannot change once created).
  • Defined with parentheses.

Conclusion

  • Python is versatile for automation, data analysis, web development, etc.
  • For more comprehensive learning, consider a dedicated Python course.