Essential Python Programming Basics

Sep 20, 2024

Python Crash Course

Introduction

  • Learn as much Python as possible in one hour.
  • Ideal for those transitioning from other programming languages.
  • Additional full, free 12-hour course available.

Setup

Download Python Interpreter

  • Visit python.org
  • Go to Downloads and download the latest version.
  • On Windows, check the box to add Python to PATH.
  • Run the executable to complete installation.

Download IDE (Integrated Development Environment)

  • Two popular choices: PyCharm and VS Code.
    • PyCharm is more beginner-friendly.
    • VS Code can also be used, but requires Python extension.

Creating Your First Python Project

  • Open IDE (e.g., PyCharm) and create a new project.
  • Create a new Python file (e.g., Main.py).

Writing Code

  • Use the print() function:
    print("I like pizza")
    
  • Output: Displays "I like pizza" in console.
  • Add multiple print statements:
    print("It's really good")
    

Comments

  • Use # for comments:
    # This is my first Python program
    
  • Comments do not affect output.

Variables

Definition

  • Variables are reusable containers for values.
  • Four data types: Strings, Integers, Floats, Booleans.

Creating Variables

  • Example:
    full_name = "Your Name"
    
  • Use f-strings for formatted output:
    print(f"Hello, {full_name}")
    

Data Types Explained

  • Strings: Series of characters.
  • Integers: Whole numbers (e.g., 25).
  • Floats: Numbers with decimals (e.g., 3.2).
  • Booleans: True or False values.

Basic Arithmetic

  • Arithmetic Operators:
    • Addition (+)
    • Subtraction (-)
    • Multiplication (*)
    • Division (/)
    • Integer Division (//)
    • Modulus (%)

Example Usage

friends = 5
friends += 1  # Increment
print(friends)  # Output: 6

Typecasting

Definition

  • Converting a variable from one data type to another.

Functions for Typecasting:

  • str(), int(), float(), bool()
  • Example:
    age = int(input("Enter your age:"))
    

User Input

  • Use the input() function to accept user input:
    name = input("Enter your name:")
    
  • Input is always a string by default. Typecast if necessary.

Conditional Statements: If Statements

Basic Structure

if condition:
    # code to execute if condition is true
else:
    # code to execute if condition is false

Example:

if age >= 18:
    print("You are an adult")
else:
    print("You are a child")

Logical Operators

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

Loops

While Loops

  • Repeat code while a condition is true.
while condition:
    # code to repeat

For Loops

  • Iterate over a sequence or a range.
for i in range(10):
    print(i)

Data Structures

Lists

  • Mutable collections defined by square brackets.
  • Supports indexing, appending, and removing elements.

Tuples

  • Immutable collections defined by parentheses.
  • Faster access than lists but cannot change.

Sets

  • Unordered collections defined by curly braces.
  • Unique items only, no duplicates allowed.
  • Good for membership testing.

Summary

  • Python is versatile and beginner-friendly.
  • Fundamental concepts: Variables, Control Flow, Loops, Data Structures.