Python Programming Tutorial

Jul 26, 2024

Python Programming Tutorial Notes

Introduction

  • The tutorial is designed for beginners with no prior knowledge of Python or programming.
  • Focuses on programming for data science, machine learning, and web development.
  • Instructor: Mosh Hamadani.

What You Can Do with Python

  • Machine Learning and AI: Python is the leading language for these fields.
  • Web Development: Use frameworks like Django; notable websites include YouTube, Instagram, Spotify, Dropbox, Pinterest.
  • Automation: Simplify repetitive tasks to save time.

Setup Instructions

  1. Download Python

    • Visit python.org.
    • Choose the latest version from the downloads section.
    • Important: Check "Add Python to PATH" during installation.
  2. Install a Code Editor

    • Recommended: PyCharm (available at jetbrains.com).
    • Choose Community Edition (free).
    • Follow installation instructions based on OS.
  3. Create a New Project in PyCharm

    • Name your project and confirm its location.
    • Right-click to create a new Python file for coding.

Your First Python Program

  • Writing "Hello, World!" in Python:
    print("Hello, World!")
    
  • To run the code: Go to Run > Run or use a shortcut.

Variables

  • Definition: Used to store data temporarily in memory.
  • Example of declaring a variable:
    age = 20
    
  • To print a variable's value:
    print(age)
    
  • Types of Variables:
    • Integer: whole numbers (e.g., age)
    • Float: numbers with decimals (e.g., price = 19.95)
    • String: textual data (e.g., name = "Mosh")
    • Boolean: True or False (e.g., is_online = True)

User Input

  • Using the input() function:
    name = input("What is your name? ")
    print("Hello " + name)
    

Data Types

  1. Primitive Types:

    • Numbers (Integers and Floats)
    • Strings
    • Booleans
  2. Type Conversion:

    • Use functions like int(), float(), str() to convert between types.

Arithmetic Operations

  • Basic operators: +, -, *, /, // (integer division), % (modulus), ** (exponentiation).
  • Operator precedence: Multiplication and division before addition and subtraction.

Comparison Operators

  • Common operators: >, >=, <, <=, ==, !=

Logical Operators

  • Combine boolean expressions:
    • and: true if both are true.
    • or: true if at least one is true.
    • not: reverses the boolean value.

Conditionals (If Statements)

  • Basic structure to make decisions:
    if temperature > 30:
        print("It's a hot day")
    elif temperature > 20:
        print("It's a nice day")
    else:
        print("It's a cold day")
    

Loops

  1. While Loop: Repeat a block of code while a condition is true.

    while i <= 5:
        print(i)
        i += 1
    
  2. For Loop: Iterate over a sequence (such as a list):

    for item in list:
        print(item)
    

Lists

  • Declare a list using square brackets:
    names = ["John", "Bob", "Mary"]
    
  • Access elements using indexes.
  • Common methods: append(), insert(), remove(), clear(), len().

Range Function

  • Generates a sequence of numbers:
    for number in range(5):
        print(number)
    

Tuples

  • Similar to lists but immutable:
    numbers = (1, 2, 3)
    
  • Cannot change elements after creation.

Exercise Sections

  • Several hands-on exercises are included to practice concepts.

Additional Resources

  • Consider enrolling in Mosh's online Python course at codewoodmosh.com, which includes more comprehensive training and a certificate of completion.

Note: Review each section in detail for deeper understanding, especially coding examples and functions mentioned.
Ask questions if anything is unclear as you learn!