Python Programming Essentials: Introductory Notes

Jul 24, 2024

Python Tutorial Notes

Introduction

  • Learn Python programming from scratch.
  • No prior knowledge needed.
  • Mosh Hamadani as the instructor.
  • Subscribe for weekly updates.

What Can You Do with Python?

  • Python is a multi-purpose programming language.
  • Uses include:
    • Machine Learning & AI.
    • Data Science projects.
    • Web Development using Django.
  • Examples of websites using Python & Django:
    • YouTube
    • Instagram
    • Spotify
    • Dropbox
    • Pinterest
  • Automation of repetitive tasks to increase productivity.

Setting Up Python

Downloading Python

  1. Visit python.org.
  2. Navigate to Downloads and select the latest version.
  3. Install the Python installer:
    • On Windows: Check "Add Python to PATH".
    • Agree to terms and install.

Installing a Code Editor

  • Use PyCharm for writing and executing code.
  1. Visit jetbrains.com/pycharm.
  2. Download the Community Edition (free).
  3. Install PyCharm:
    • On Windows: Follow the installation wizard.
    • On Mac: Drag to Applications folder.
  4. Configure settings on first run and create a new project.

Writing Your First Python Code

Hello World

  • Create a new Python file and write:
    print("Hello World")
    
  • Run the code to see output in the terminal.

Variables

  • Used to store data temporarily.
  • Syntax to declare variables:
    age = 20
    
  • Print the value of a variable:
    print(age)
    

Types of Values

  • Integer: Whole numbers.
  • Float: Numbers with a decimal point.
  • String: Textual data (enclosed in quotes).
  • Boolean: true or false.
    • Case-sensitive (True, False).

User Input

  • Use input() function to receive input:
    name = input("What is your name? ")
    print("Hello " + name)
    

Data Type Conversion

  • To convert string to integer or float:
    age = int(input("Enter your age: "))
    

Arithmetic Operations

  • Common operators include +, -, *, /, // (integer division), % (modulus), ** (exponent).
  • Operator precedence matters—use parentheses to control order.

Comparison Operators

  • Used to compare values:
    • , >=, <, <=, == (equal), != (not equal).

Logical Operators

  • Combine comparisons:
    • and, or, not.

Control Flow

If Statements

  • Used 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 a cold day")
    

Loops

While Loops

  • Repeats a block of code:
    i = 1
    while i <= 5:
        print(i)
        i += 1
    

For Loops

  • Iterates over a sequence:
    for num in range(1, 6):
        print(num)
    

Lists

  • Used to store collections of items:
    names = ["John", "Bob", "Marsh"]
    
  • Access elements by index (0-based), modify elements, append/remove items.

Using Built-in Functions

  • len() for length of the list.
  • Using in to check for existence in list.

Tuples

  • Similar to lists but immutable:
    numbers = (1, 2, 3)
    
  • Fewer methods available than lists.

Conclusion

  • Recap of Python basics covered in this tutorial.
  • Encourages continued learning through structured courses available on the instructor’s platform.