21  Python Programming

21.1 Python Basics

  • Entering expressions into the interactive shell
  • The integer, floating-point, and string data types
  • String concatenation and replication
  • Storing values in variables
    • Assignment statements
    • Variable names
  • Your first program
  • Dissecting your program
    • Comments
    • The print() function
    • The input() function
    • Printing the user’s name
    • The len() function
    • The str(), int(), and float() functions

21.2 Manipulating Strings

  • Working with Strings
    • String literals
      • Double quotes
      • Escape characters
      • Raw strings
      • Multiline strings with triple quotes
      • Multiline comments
    • Indexing and slicing strings
    • The in and not in operators with Strings
  • Putting strings inside other strings
    • String interpolation
    • f-strings
  • Useful String methods
    • upper(), lower(), isupper(), and islower() methods
    • The isX() methods: isalpha(), isalnum(), isdecimal(), isspace(), istitle()
    • The startswith() and endswith() methods
    • The join() and split() methods
    • Splitting strings with the partition() method
    • Justifying text with the rjust(), ljust(), and center() methods
    • Removing whitespace with the strip(), rstrip(), and lstrip() methods
    • Numeric values of characters with the ord() and chr() functions

21.3 Functions

  • def statements with parameters
  • Define, call, pass, argument, parameter
  • Return values and return statements
  • The None value
  • Keyword arguments and the print() function
  • The call stack
  • Local and global scope
    • Local variables cannot be used in the global scope
    • Local scopes cannot use variables in other local scopes
    • Global variables can be read from a local scope
    • Local and global variables with the same name
  • The global statement

21.4 Flow Control

21.5 Data Structures

21.5.1 Lists and Tuples

  • The list data type
    • Getting individual values in a list with indexes
    • Negative indexes
    • Getting a list from another list with slices
    • Getting a list’s length with the len() function
    • Changing values in a list with indexes
    • List concatenation and list replication
    • Removing values from lists with del statements
  • Working with lists
    • Using for loops with lists
    • The in and not in operators
    • The multiple assignment trick
    • Using the enumerate() function with lists
    • Using the random.choice() and random.shuffle() functions with lists
  • Augmented assignment operators
  • Methods
    • Finding a value in a list with the index() method
    • Adding values to lists with theappend() and insert() methods
    • Removing values from lists with the remove() method
    • Sorting the values in a list with the sort() method
    • Reversing the values in a list with the reverse() method
  • Sequence data types
    • Mutable and immutable data types
    • The tuple data type
    • Converting types with the list() and tuple() functions
  • References
    • Identity and the id() function
    • Passing references
    • The copy module’s copy() and deepcopy() functions

21.5.2 Dictionaries and Structuring Data

  • The dict data type
    • Dictionaries vs. lists
    • The keys(), values(), and items() methods
    • Checking whether a key or value exists in a dictionary
    • The get() method
    • The setdefault() method
  • Pretty printing (pprint module)
  • Using data structures to model real-world things

21.5.3 Sets, Named Tuples, Default Dictionaries, Counter, Deque

  • The set data structure
    • Creating a set
    • Set operations
  • The collections module
    • Named tuple
    • Default dictionary
    • Counter
    • The deque

21.6 File Handling

  • Reading and Writing Files
    • Files and file paths
    • Backslash (\) on Windows and forward slash (/) on macOs and Linux
    • Using the / operator to join paths
    • The current working directory
    • The home directory
    • Absolute vs. relative paths
    • Creating new folders using the os.makedirs() function
    • Handling absolute and relative paths
    • Getting the parts of a file path
    • Finding file sizes and folder contents
    • Modifying a list of files using glob patterns (Interested? See pattern matching with Regex)
    • Checking path validity
  • The file reading/writing process
    • Opening files with the open() function
    • Reading the contents of files
    • Writing to files
  • Organizing Files
    • The shutil module
      • Copying files and folders
      • Moving and renaming files and folders
      • Permanently deleting files and folders
      • Walking a directory tree
  • Error handling.

21.7 Debugging

  • Raising exceptions
  • Getting the traceback as a string
  • Assertions
  • Logging
    • Using the logging module
    • Don’t debug with the print() function
    • Logging levels
    • Disabling logging
    • Logging to a file
  • Debugging in VSCode

21.8 Introduction to Object Oriented Programming (OOP)

  • Classes and objects
  • Accessing object variables
  • Accessing object functions
  • init() function