Beginner's Guide to Python Programming

Aug 12, 2024

Python Programming Tutorial

Introduction

  • Learn Python from scratch.
  • Suitable for data science, machine learning, and web development.
  • No prior knowledge needed.
  • Instructor: Mosh Hamadani.
  • Subscribe for weekly updates.

What Can You Do with Python?

  • Multi-purpose Language:
    • Machine Learning & AI (Top language for ML & Data Science)
    • Web Development (using Django)
    • Automation (time-saving and productivity)
  • Examples of websites using Python/Django:
    • YouTube
    • Instagram
    • Spotify
    • Dropbox
    • Pinterest

Installation

  1. Go to python.org to download the latest version.
    • Select "Downloads" and choose the latest version.
    • Important: Check "Add Python to PATH" during installation (for Windows).
  2. Install a code editor: PyCharm.
    • Download from JetBrains (Community Edition is free).
    • Install following wizard instructions.
  3. Create a new Python project in PyCharm.
    • Name it "Hello World".

Writing Your First Code

  • Create a new Python file (app.py).
  • Write code:
    print("Hello, World!")  
    
  • Run code using the run menu or shortcut.
  • Output will show in the terminal window.

Variables

  • Used to store data temporarily.
  • Syntax: variable_name = value
  • Example:
    age = 20  
    print(age)  
    
  • Demonstrates variable declaration and output.

Data Types

  • Primitive Types:
    • Numbers (integers, floats)
    • Strings (use quotes)
    • Booleans (True/False)
  • Variable Naming: Use underscore for multiple words.

Input from Users

  • Use input() function to receive user input.
    name = input("What is your name? ")  
    print("Hello, " + name)  
    
  • String concatenation demonstrated.

Type Conversion

  • Convert data types using functions:
    • int(), float(), bool(), str()
  • Example of converting input to integer for calculations.

Arithmetic Operations

  • Basic operators:
    • Addition (+), Subtraction (-), Multiplication (*), Division (/, //, %, **)
  • Use parentheses for precedence in operations.

Comparison Operators

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

Logical Operators

  • Combine boolean expressions:
    • and, or, not
  • Used in conditional statements.

If Statements

  • Syntax:
    if condition:  
        # code block  
    elif condition:  
        # code block  
    else:  
        # code block  
    
  • Example for determining temperature conditions.

While Loops

  • Used for repeated execution:
    while condition:  
        # code block  
    
  • Example of printing numbers from 1 to 5.

Lists

  • To store multiple items:
    names = ["John", "Bob", "Mosh"]  
    
  • Access items using index (0-based).
  • Modify lists using methods: append(), insert(), remove(), clear().
  • Length of list: len(list).

Tuples

  • Immutable sequences similar to lists.
  • Defined using parentheses:
    numbers = (1, 2, 3)  
    
  • Access via indexing, but cannot modify.

Conclusion

  • Comprehensive overview of Python basics.
  • Call to action for further learning resources.