Introduction to Python Programming Basics

Sep 3, 2024

Python Programming Tutorial Notes

Introduction

  • Instructor: Mosh Hamadani
  • Objective: Teach Python programming from scratch for various applications (data science, machine learning, web development).
  • No prior knowledge required.

What Can You Do With Python?

  • Multi-purpose programming language.
  • Machine Learning and AI: Python is the leading language for these fields.
  • Web Development: Popular frameworks like Django.
    • Examples of sites powered by Python: YouTube, Instagram, Spotify, Dropbox, Pinterest.
  • Automation: Automate repetitive tasks to save time and increase productivity.

Setting Up Python

  1. Visit python.org to download the latest version.
    • Check the box to "Add Python to PATH" during installation.
  2. Download a code editor.
    • Recommended: PyCharm.
    • Choose Community Edition (free).
  3. Create a new Python project in PyCharm.
    • Name the project (e.g., HelloWorld).

Writing Your First Code

  • Creating a Python File: Right-click on the project name, create a new Python file (e.g., app.py).
  • Printing Output: Use print("Hello, World!") to display output.
    • Strings: Textual data enclosed in quotes.
  • Running Code: Use the Run menu or shortcuts.

Variables

  • Definition: Temporarily store data.
  • Declaration: age = 20
  • Printing Variables: Use print(age) to display the value.
  • Changing Variables: Reassign values (e.g., age = 30).
  • Variable Naming: Use underscores for multiple words (e.g., first_name).
  • Boolean Values: Examples include is_online = True or is_online = False.

User Input

  • Use input() function to receive user input.
    • Example: name = input("What is your name? ")
  • Concatenate strings using + (e.g., print("Hello, " + name)).

Data Types

  1. Numbers: Integers and floats.
  2. Strings: Text data.
  3. Booleans: True or False.
  4. Type Conversion: Convert data types using int(), float(), str(), etc.

Arithmetic Operations

  • Basic operations: +, -, *, /, // (integer division), % (modulus), ** (exponentiation).
  • Augmented Assignment Operators: Simplify assignments (e.g., x += 3).

Comparison Operators

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

Logical Operators

  • Used for complex conditions: and, or, not.

If Statements

  • Structure:
    if condition:
        # code to execute if condition is true
    elif another_condition:
        # another code block
    else:
        # code if no conditions are true
    

Loops

  • While Loops: Repeat code while a condition is true.

    i = 1
    while i <= 5:
        print(i)
        i += 1
    
  • For Loops: Iterate over a sequence (like a list or range).

    for item in sequence:
        print(item)
    

Lists

  • Definition: Ordered collection of objects.
  • Creating a List: names = ["John", "Bob", "Marsh"]
  • Accessing Elements: Use indexing (e.g., names[0]).
  • List Methods: append(), insert(), remove(), clear(), etc.

Tuples

  • Definition: Similar to lists but immutable (cannot be changed).
  • Use parentheses to define: numbers = (1, 2, 3).

Functions

  • Built-in Functions: len(), range(), etc.
  • Creating Functions: def function_name(): to define a custom function.

Conclusion

  • Python is versatile and used for various applications.
  • Practice is key to mastering programming concepts.