Python Tutorial by Mosh Hamedani

Jul 11, 2024

Python Tutorial by Mosh Hamedani

Introduction

  • Aimed at beginners with no prior knowledge
  • Focus on data science, machine learning, web development
  • Mosh Hamedani, experienced instructor

Why Learn Python?

  • Multi-purpose language
  • Machine Learning and AI: No. 1 language
  • Web Development: Framework - Django
    • Examples: YouTube, Instagram, Spotify, Dropbox, Pinterest
  • Automation: Increase productivity by automating tasks

Setting Up Python

  1. Download from Python.org
  2. Install Python
  • Important checkbox: Add Python to PATH
  1. Install a code editor (e.g., PyCharm)
  • Community Edition (free & open-source)

Writing First Python Program

  1. Create a new project in PyCharm
  2. Write print("Hello World")
  3. Explanation:
  • print is a function
  • "Hello World" is a string (textual data)
  • Strings can use single or double quotes
  1. Running the code
  • Use Run menu or shortcut

Variables in Python

  • Store data using variables
  • Example: age = 20
  • Printing variables: print(age)
  • Changing values: Update variable and re-run
  • Types of variables: integers, floats, strings, booleans
  • Naming conventions: use underscores for readability (first_name)

Receiving Input

  • input function: name = input("What is your name?")
  • String concatenation: print("Hello " + name)
  • Type conversion: int, float, bool, str
  • Example: convert user input for age calculation

String Methods

  • Strings are objects with methods
    • Examples: upper(), lower(), find(), replace()
  • Immutable nature of strings
  • Checking substring: in keyword

Arithmetic Operations

  • Basic operators: +, -, *, /, // (integer division), % (modulus), ** (exponentiation)
  • Order of operations (operator precedence)
  • Augmented assignment operators: +=, -=, *=, etc.

Comparison & Logical Operators

  • Comparison: >, <, >=, <=, ==, !=
  • Logical: and, or, not

If Statements

  • Basic syntax: if, elif, else
  • Indentation for blocks
  • Example: temperature check program

While Loops

  • Repeat code block with condition
  • Structure: initialize variable, while condition, update variable
  • Example: print numbers from 1 to 5

For Loops

  • Iterate over sequences (lists, ranges)
  • Example: List of numbers for item in numbers

The range Function

  • Generate sequence of numbers
  • Syntax variations: range(5), range(5, 10), range(5, 10, 2)
  • Often used in for loops

Lists

  • Ordered collection of items
  • Creating lists: names = ["John", "Bob", "Mosh"]
  • Accessing elements: indexes (positive and negative)
  • Slicing lists: names[0:3]
  • List methods: append(), insert(), remove(), clear()
  • Checking membership: in operator
  • Getting length: len()

Tuples

  • Similar to lists but immutable
  • Defined using parentheses: numbers = (1, 2, 3)
  • Methods: count(), index()
  • Use cases: Ensuring data integrity

Additional Resources

  • Comprehensive Python course available at CodeWithMosh.com
  • Includes advanced topics, 30-day money-back guarantee, certificate of completion