🐍

Beginner's Guide to Python Programming

Nov 23, 2024

Python Tutorial Introduction

  • Instructor: Mosh Hamadani
  • Objective: Learn Python from scratch; suited for data science, machine learning, web development.
  • No prior programming knowledge required.

What You Can Do with Python

  • Multi-purpose language: Machine learning, data science, web development, automation.
  • Popular frameworks & platforms: Django (web), used by YouTube, Instagram, etc.

Setting Up Python Environment

  1. Download Python:

    • Visit python.org to download the latest version.
    • Important: Check the box to add Python to PATH (Windows users).
  2. Install Code Editor:

    • Pycharm from jetbrains.com/pycharm
    • Opt for the free Community edition.
    • Follow installation instructions for respective OS (Windows/Mac).

Writing Your First Python Program

  1. Create a new Python file (app.py):
    • Use print function to output text: print("Hello, World")
  2. Variables:
    • Used to store data: age = 20
    • Changeable values, e.g., age = 30
  3. Data Types:
    • Integers, floats, strings, booleans.
    • Example: price = 19.95, first_name = 'Mosh', is_online = True

User Input

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

Type Conversion

  • Convert input to different types using int(), float(), bool(), str().
  • Example: Calculate age from birth year with type conversion.

String Methods

  • Strings are objects with methods like upper(), lower(), replace(), find().

Arithmetic & Comparison Operators

  • Arithmetic: Addition (+), Subtraction (-), Multiplication (*), Division (/), etc.
  • Comparison: Equal (==), Not equal (!=), Greater than (>), etc.*

Logical Operators

  • Logical AND, OR, NOT to build complex conditions.

Conditional Statements

  • Use if, elif, and else for decision making.
  • Example: Checking temperature and printing appropriate messages.

Loops

  • While Loop: Repeat a block of code while a condition is true.
    • Example: Printing numbers 1 to 5.
  • For Loop: Iterate over a sequence (e.g., list, range).
    • Example: Printing each number in a list on a new line.

Lists

  • Define a list: names = ['John', 'Bob', 'Mosh']
  • Access elements by index: names[0], negative indices supported.
  • List methods: append(), insert(), remove(), clear().

Tuples

  • Similar to lists but immutable.
  • Example: numbers = (1, 2, 3)

Range Function

  • Generates a sequence of numbers.
  • Example: range(5) generates 0 to 4.

Final Note

  • Additional Resources: Mosh's online coding school offers more in-depth Python courses with money-back guarantee.