📝

Python Programming Basics

Jul 10, 2024

Python Programming Basics

Introduction

  • Instructor: Beau Carnes, FreeCodeCamp.org
  • Purpose: For beginners to learn Python programming from scratch.
  • Requirements: No prior programming experience needed. A web browser is enough to start coding in Python.
  • Course Outline: Learn core aspects of Python with simplified complex topics. Focus areas include shell scripting, task automation, web development, data analysis, machine learning, games, and embedded devices.

Getting Started

Setting Up An IDE

  1. Replit: An online IDE to code in Python directly from the web browser.
  2. Local Setup: Instructions given to set up Python on local operating systems via Python.org.

Create First Python Program

  1. Replit.com: Log in and create a new Replit for Python development.
  2. Variable Creation: Example to create variables: player_choice = "rock".

Python Basics

Creating Variables

  • player_choice: Stores the player's choice, e.g., rock, paper, or scissors.
  • computer_choice: Stores the computer's choice, e.g., paper.
  • Assigning Values: Use the assign operator (=) and quotation marks for strings.

Functions

  • Definition: Set of code which runs when called, defined using def.
  • Example: def get_choices(): assigns player and computer choices.
  • Calling a Function: get_choices() to execute the function.
  • Return Statement: Used to return a value from a function.

Dictionaries

  • Definition: Stores data in key-value pairs; created using {}.
  • Example: choices = {"player": player_choice, "computer": computer_choice}.
  • Accessing Values: Use keys to access values, e.g., choices["player"].

Getting User Input

  • Example: input("Enter a choice (rock, paper, scissors): ").
  • Data Conversion: Convert input strings to appropriate types using constructors.

Libraries and Importing

  • Random Library: Used for generating random values, e.g., import random.
  • Function for Random Choice: random.choice(options) where options is a list.

Control Statements

  • If Statements: Conditions to execute code blocks based on condition, e.g., if player == computer: print("It's a tie").
  • Elif and Else: Used for multiple conditions.
  • Logical Operators: and, or, not to combine conditions.

Loops

  • For Loops: Iterate over a list of values.
  • While Loops: Repeat as long as a condition is true.
  • Break and Continue: Control loop execution.

Data Types

  • Strings: Text data, surrounded by quotes.
  • Integers and Floats: Whole and decimal numbers.
  • Booleans: True or False values.
  • Lists: Ordered collections of items, e.g., [1, 2, 3].
  • Tuples: Immutable ordered collections, e.g., (1, 2, 3).
  • Sets: Unordered collections of unique items, e.g., {1, 2, 3}.

Advanced Python Concepts

Object-Oriented Programming (OOP)

  • Classes and Objects: Create templates (classes), and instances (objects).
  • Methods: Functions inside classes, e.g., def bark(self): print("Woof").
  • Inheritance: Create a new class from an existing class, inheriting attributes and methods.

Error Handling

  • Try and Except Blocks: Handle exceptions to prevent crashes, e.g., try: ... except:.

Modules and Packages

  • Importing Modules: Use built-in and third-party libraries.
  • Creating and Using Custom Modules: Structure code over multiple files and directories.

Functional Programming Tools

  • Lambda Functions: Small anonymous functions, e.g., lambda x: x * 2.
  • Map, Filter, Reduce: Functions for processing collections.*

File Handling

  • Reading and Writing Files: Use open, read, and write methods.
  • Context Managers: with open(...) as file: to manage file operations safely.

Final Project: Blackjack Game

Game Classes

  • Deck Class: Represents a deck of cards, methods to shuffle and deal cards.
  • Card Class: Represents an individual card, with attributes for suit and rank.
  • Hand Class: Represents a player or dealer's hand, methods to calculate the hand value.

Game Flow

  1. Starting the Game: Ask user for input on how many games to play.
  2. Shuffling and Dealing: Create and shuffle the deck, deal to player and dealer's hands.
  3. Player Actions: Allow player to 'hit' or 'stand' and handle accordingly.
  4. Dealer Actions: Dealer logic to draw cards until a threshold is met.
  5. Determine Winner: Compare hand values to declare the winner.
  6. Display Results: Show final results of the game.