🐍

Comprehensive Python Programming Guide

Sep 16, 2024

Python Programming Tutorial by Mosh Hamadani

Introduction

  • Multi-purpose programming language suitable for:
    • Machine learning and AI
    • Web development
    • Automation
  • Popular frameworks and uses:
    • Django for web development (e.g., YouTube, Instagram)
    • Automate repetitive tasks

Setting Up Python Environment

  1. Download Python

    • Visit python.org and download the latest version.
    • Ensure to add Python to PATH during installation (important for Windows users).
  2. Install Code Editor

Basics of Python Programming

Writing Your First Python Code

  • Use print() function:
    • Example: print("Hello World")
  • Understanding strings in Python:
    • Enclose text in single or double quotes.

Variables

  • Store data temporarily in memory.
  • Examples:
    • age = 20 (integer)
    • price = 19.95 (floating-point number)
    • first_name = "Mosh" (string)
    • is_online = True (boolean value)
  • Note Python is case-sensitive.

User Input

  • Use input() function to receive user input:
    • Example: name = input("What is your name?")
  • String concatenation using + operator.

Type Conversion

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

Arithmetic and Logical Operations

Arithmetic Operators

  • Addition +, Subtraction -, Multiplication *, Division /
  • Integer Division //, Modulus %, Exponentiation **
  • Augmented assignment (e.g., x += 3)***

Operator Precedence

  • Multiplication and division before addition and subtraction.
  • Use parentheses () to change evaluation order.

Comparison Operators

  • Greater than >, Less than <, Equals ==, Not equals !=

Logical Operators

  • and, or, not for combining boolean expressions.

Control Flow

If Statements

  • Use if, elif, 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 cold")

Loops

  • While Loop: Repeat a block of code as long as a condition is true.
  • For Loop: Iterate over a sequence (e.g., list, range).

Range Function

  • Generate sequences of numbers for iteration.
  • Example: range(5) generates numbers 0 to 4.

Data Structures

Lists

  • Ordered, mutable collection of items:
    • Example: names = ["John", "Bob", "Mosh"]
  • Access and modify items using index.
  • Methods: append(), insert(), remove(), clear()

Tuples

  • Similar to lists but immutable.
  • Use parentheses () to define.
  • Suitable when data should not change.

Conclusion

  • Tuples and lists are foundational data structures for managing collections in Python.
  • Practice exercises provided to reinforce learning (e.g., weight converter program).

Further Learning