🐍

Getting Started with Python Programming

Oct 9, 2024

Python Programming Basics

Introduction

  • Quick 1-hour introduction to Python.
  • Recommended for beginners or those transitioning from another language.
  • More in-depth 12-hour course available on YouTube.

Setup

  1. Python Interpreter

    • Download from python.org.
    • Install and add Python to the PATH (for Windows).
  2. IDE (Integrated Development Environment)

    • Options: PyCharm or VS Code.
    • PyCharm is more beginner-friendly.
    • Use the Community Edition (free).

First Python Program

  • Create a new Python project and file.
  • Use print() function to display output.
    • Example: print("I like pizza")
  • Comments are written using # (pound sign).

Variables

  • Reusable containers for values.

  • Data Types:

    • String: Series of characters, enclosed in quotes.
    • Integer: Whole numbers.
    • Float: Numbers with decimals.
    • Boolean: True or False.
  • Use f-string for string interpolation: f"Hello, {variable}".

Arithmetic Operations

  • Operators:
    • Addition: +
    • Subtraction: -
    • Multiplication: *
    • Division: /
    • Integer Division: //
    • Modulus: %
  • Augmented Assignment: +=, -=, *=, /=, //=, %=.

Type Casting

  • Converting variables from one type to another.
  • Functions: str(), int(), float(), bool().
  • Useful for handling user input.

User Input

  • Use input() to get user input.
  • By default, input is of string type.
  • Can type cast to other data types as needed.

Conditional Statements

  • If Statements: Basic decision-making structure.
  • Syntax: if, elif, else.
  • Use comparison operators: ==, !=, <, >, <=, >=.

Logical Operators

  • OR: At least one condition must be true.
  • AND: Both conditions must be true.
  • NOT: Inverts the Boolean value.

Loops

While Loops

  • Repeatedly execute code as long as a condition is true.
  • Check condition at the start of each iteration.

For Loops

  • Iterate over a sequence (string, list, tuple, set).
  • Can specify range and step.

Data Collections

Lists

  • Mutable, ordered collection.
  • Defined with square brackets [].
  • Methods: append(), remove(), pop(), clear().

Tuples

  • Immutable, ordered collection.
  • Defined with parentheses ().
  • Faster access than lists.

Sets

  • Mutable, unordered collection.
  • Defined with curly braces {}.
  • No duplicate elements.
  • Best for membership testing.