Beginner's Guide to Python Programming

Aug 1, 2024

Python Programming Tutorial Notes

Introduction

  • Learn Python from scratch
  • Applicable for data science, machine learning, web development
  • No prior knowledge required
  • Instructor: Mosh Hamadani
  • Subscribe for weekly updates

What Can You Do With Python?

  • Multi-purpose programming language
  • Common uses:
    • Machine Learning & AI
    • Web Development (popular frameworks: Django)
    • Automation of repetitive tasks
  • Examples of websites built with Python & Django:
    • YouTube
    • Instagram
    • Spotify
    • Dropbox
    • Pinterest

Installing Python

  1. Go to python.org
  2. Download the latest version
  3. Important: Check "Add Python to PATH" during installation

Installing a Code Editor

  • Recommended: PyCharm
  • Download from JetBrains
  • Community Edition is free
  • Configure settings on the first launch
  • Create a new project and name it (e.g., "Hello World")

Writing Your First Python Code

  • Create a new Python file
  • Code to print "Hello World":
    print("Hello World")  
    
  • Run the code via the run menu or shortcuts

Understanding Variables

  • Variables store data in memory
  • Example of variable declaration:
    age = 20  
    
  • Print variable value:
    print(age)  
    
  • Variable names: Use underscores for multiple words

Data Types

  1. Strings
    • Textual data enclosed in quotes
  2. Booleans
    • True or False
    • Case sensitive (True vs true)
  3. Numbers
    • Integers and floats (e.g., 19.95)

User Input

  • Use input() to read values from the terminal
  • Example:
    name = input("What is your name? ")  
    print("Hello " + name)  
    

Type Conversion

  • Use built-in functions:
    • int() to convert to integer
    • float() to convert to float
    • str() to convert to string

Arithmetic Operations

  • Basic operators: +, -, *, /
  • Integer division (//) and modulus (%)
  • Operator precedence (use parentheses to change order)

Conditional Statements

  • Use if, elif, and else for decision making
  • Example:
    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 loop example:
    i = 1  
    while i <= 5:  
        print(i)  
        i += 1  
    
  • for loop example to iterate over a list
    for item in names:  
        print(item)  
    

Lists

  • Store multiple items in a single variable
  • Example:
    names = ["John", "Bob", "Mosh"]  
    
  • Access items by index, use append(), insert(), remove(), clear(), and in operator
  • Length of a list: len(list)

Tuples

  • Similar to lists but immutable
  • Use parentheses to define:
    numbers = (1, 2, 3)  
    
  • Limited methods compared to lists

Conclusion

  • Python is versatile and beginner-friendly
  • Essential for various applications and use cases
  • Resources for further learning are available at codewoodmosh.com