- 1. Python Programming
- 2. First steps
- 2.1. What is Python?
- 2.2. What is needed to write a program?
- 2.3. The source (code) of Python
- 2.4. Python 2 vs. Python 3
- 2.5. Installation
- 2.6. Installation on Linux
- 2.7. Installation on Apple Mac OSX
- 2.8. Installation on MS Windows
- 2.9. Installation of Anaconda
- 2.10. Editors, IDEs
- 2.11. Documentation
- 2.12. Program types
- 2.13. Python on the command line
- 2.14. First script - hello world
- 2.15. Examples
- 2.16. Comments
- 2.17. Variables
- 2.18. Exercise: Hello world
- 2.19. What is programming?
- 2.20. What are the programming languages
- 2.21. A written human language
- 2.22. A programming language
- 2.23. Words and punctuation matter!
- 2.24. Types matter to Python (a bit)
- 2.25. String vs int
- 2.26. String vs float
- 2.27. int and float
- 2.28. Literals, Value Types in Python
- 2.29. Floating point limitation
- 2.30. Floating point -compare using round
- 2.31. round
- 2.32. Value Types in Numpy
- 2.33. Rectangle (numerical operations)
- 2.34. Multiply string
- 2.35. Add numbers
- 2.36. Add strings
- 2.37. Exercise: Calculations
- 2.38. Solution: Calculations
- 3. Second steps
- 3.1. Modules
- 3.2. A main function
- 3.3. The main function - called
- 3.4. Indentation
- 3.5. Conditional main
- 3.6. Input - Output I/O
- 3.7. print in Python 2
- 3.8. print in Python 3
- 3.9. print in Python 2 as if it was Python 3
- 3.10. Exception: SyntaxError: Missing parentheses in call
- 3.11. Prompting for user input in Python 2
- 3.12. Prompting for user input in Python 3
- 3.13. Python2 input or raw_input?
- 3.14. Prompting both Python 2 and Python 3
- 3.15. Add numbers entered by the user (oups)
- 3.16. Add numbers entered by the user (fixed)
- 3.17. Can we convert a string to int or float?
- 3.18. How can I check if a string can be converted to a number?
- 3.19. Converting string to int
- 3.20. Converting float to int
- 3.21. How can I check if a string can be converted to a number?
- 3.22. Conditionals: if
- 3.23. Conditionals: if - else
- 3.24. Divide by 0
- 3.25. Conditionals: if - else (other example)
- 3.26. Conditionals: else if
- 3.27. Conditionals: elif
- 3.28. Ternary operator (Conditional Operator)
- 3.29. Case or Switch in Python: match pattern matching
- 3.30. Exercise: Rectangle
- 3.31. Exercise: Calculator
- 3.32. Exercise: Age limit
- 3.33. Exercise: What is this language?
- 3.34. Exercise: Standard Input
- 3.35. Solution: Area of rectangle
- 3.36. Solution: Calculator
- 3.37. Solution: Calculator eval
- 3.38. Solution: Age limit
- 3.39. Solution: What is this language?
- 3.40. STDIN vs Command line arguments
- 3.41. Command line arguments
- 3.42. Command line arguments - len
- 3.43. Command line arguments - exit
- 3.44. Exercise: Rectangle (argv)
- 3.45. Exercise: Calculator (argv)
- 3.46. Solution: Area of rectangle (argv)
- 3.47. Solution: Calculator (argv)
- 3.48. Solution: Calculator eval
- 3.49. Compilation vs. Interpretation
- 3.50. Is Python compiled or interpreted?
- 3.51. Flake8 checking
- 3.52. Pylint checking
- 4. Numbers
- 4.1. Numbers
- 4.2. Operators for Numbers
- 4.3. Integer division and the future
- 4.4. Pseudo Random Number (uniform distribution)
- 4.5. Fixed random numbers
- 4.6. Rolling dice - randrange
- 4.7. Random choice
- 4.8. built-in method
- 4.9. Exception: TypeError: 'module' object is not callable
- 4.10. Fixing the previous code
- 4.11. Exception: AttributeError: module 'random' has no attribute
- 4.12. Exercise: Number guessing game - level 0
- 4.13. Exercise: Fruit salad
- 4.14. Solution: Number guessing game - level 0
- 4.15. Solution: Fruit salad
- 5. Comparison and Boolean
- 5.1. if statement again
- 5.2. Comparison operators
- 5.3. Compare numbers, compare strings
- 5.4. Do NOT Compare different types!
- 5.5. Complex if statement with boolean operators
- 5.6. Chained expressions
- 5.7. Boolean operators
- 5.8. Boolean truth tables
- 5.9. Boolean values: True and False
- 5.10. Using True and False in variables
- 5.11. Comparison returns True or False
- 5.12. Assign comparisons to variables
- 5.13. Flag
- 5.14. Use flag to skip first few lines
- 5.15. Toggle
- 5.16. Short circuit
- 5.17. Short circuit fixed
- 5.18. Does this value count as True or False?
- 5.19. True and False values in Python
- 5.20. Incorrect use of conditions
- 5.21. Exercise: compare numbers
- 5.22. Exercise: compare strings
- 5.23. Solution: compare numbers
- 5.24. Solution: compare strings
- 6. Strings
- 6.1. Single quoted and double quoted strings
- 6.2. Long lines
- 6.3. Multiline strings
- 6.4. Triple quoted strings (multiline)
- 6.5. Triple quoted comments - documentation
- 6.6. String length (len)
- 6.7. String repetition and concatenation
- 6.8. A character in a string
- 6.9. String slice (instead of substr)
- 6.10. Change a string
- 6.11. How to change a string
- 6.12. String copy
- 6.13. String functions and methods (len, upper, lower)
- 6.14. index in string
- 6.15. index in string with range
- 6.16. Find all in the string
- 6.17. rindex in string with range
- 6.18. find in string
- 6.19. in string
- 6.20. index if in string
- 6.21. Encodings: ASCII, Windows-1255, Unicode
- 6.22. raw strings
- 6.23. ord
- 6.24. chr - number to character
- 6.25. Exercise: one string in another string
- 6.26. Exercise: Character to Unicode-8 - CLI
- 6.27. Exercise: from Unicode to character - CLI
- 6.28. Exercise: ROT13
- 6.29. Solution: one string in another string
- 6.30. Solution: compare strings
- 6.31. Solution: to Unicode CLI
- 6.32. Solution: from Unicode CLI
- 6.33. Solution: Show characters based on Unicode code-points
- 6.34. Solution: ROT13
7. Other
- 7.1. Infinite loop
- 7.2. break
- 7.3. continue
- 7.4. While with many conditions
- 7.5. while loop with many conditions
- 7.6. ord in a file
- 7.7. Strings as Comments
- 8. Loops
- 8.1. Loops: for-in and while
- 8.2. for-in loop on strings
- 8.3. for-in loop on list
- 8.4. for-in loop on range
- 8.5. Iterable, iterator
- 8.6. for in loop with early end using break
- 8.7. for in loop skipping parts using continue
- 8.8. for in loop with break and continue
- 8.9. while loop
- 8.10. Infinite while loop
- 8.11. While with complex expression
- 8.12. While with break
- 8.13. While True
- 8.14. Testing the refactoring of the while loop
- 8.15. Duplicate input call
- 8.16. Duplicate input call with loop
- 8.17. Eliminate duplicate input call
- 8.18. do while loop
- 8.19. while with many continue calls
- 8.20. Break out from multi-level loops
- 8.21. For-else
- 8.22. Exercise: Print all the locations in a string
- 8.23. Exercise: Number guessing game
- 8.24. Exercise: Count unique characters
- 8.25. Exercise: Convert for-loop to while-loop
- 8.26. Solution: Print all the locations in a string
- 8.27. Solution 1 for Number Guessing
- 8.28. Solution 2 for Number Guessing (x)
- 8.29. Solution 3 for Number Guessing (s)
- 8.30. Solution for Number Guessing (debug)
- 8.31. Solution for Number Guessing (move)
- 8.32. Solution for Number Guessing (multi-game)
- 8.33. Solution: Count unique characters
- 8.34. Solution: Convert for-loop to while-loop
- 9. Formatted strings
- 9.1. format - sprintf
- 9.2. printf using old %-syntax
- 9.3. Examples using format with names
- 9.4. Format columns
- 9.5. Examples using format - alignment
- 9.6. Format - string
- 9.7. Format characters and types (binary, octal, hexa)
- 9.8. Format floating point number
- 9.9. Examples using format - indexing
- 9.10. Format characters and types using f-format
- 9.11. f-format (formatted string literals)
- 9.12. Format floating point numbers using f-format
- 9.13. Format braces, bracket, and parentheses
- 9.14. parameterized formatter
- 9.15. format binary, octal, hexa numbers
- 9.16. Examples using format with attributes of objects
- 9.17. raw f-format
- 9.18. Format with conversion (stringifiation with str or repr)
- 10. Lists
- 10.1. Anything can be a list
- 10.2. Any layout
- 10.3. Access elements of a list
- 10.4. List slice with steps
- 10.5. Change a List
- 10.6. Change sublist vs change element of a list
- 10.7. Change with steps
- 10.8. List assignment and list copy
- 10.9. Shallow vs. Deep copy of lists
- 10.10. join
- 10.11. join list of numbers
- 10.12. split
- 10.13. for loop on lists
- 10.14. in list
- 10.15. Where is the element in the list
- 10.16. Index improved
- 10.17. [].insert
- 10.18. [].append
- 10.19. [].remove
- 10.20. Remove element by index [].pop
- 10.21. Remove first element of list
- 10.22. Remove several elements of list by index
- 10.23. Use list as a queue - FIFO
- 10.24. Queue using deque from collections
- 10.25. Fixed size queue
- 10.26. List as a stack - LIFO
- 10.27. stack with deque
- 10.28. Exercies: Queue
- 10.29. Exercise: Stack
- 10.30. Exercise: MasterMind
- 10.31. Solution: Queue with list
- 10.32. Solution: Queue with deque
- 10.33. Solution: Reverse Polish calculator (stack) with lists
- 10.34. Solution: Reverse Polish calculator (stack) with deque
- 10.35. Solution: MasterMind
- 10.36. MasterMind to debug
- 10.37. Debugging Queue
- 10.38. sort
- 10.39. sort numbers
- 10.40. key sort of strings
- 10.41. sort mixed values
- 10.42. sort mixed values fixed with str
- 10.43. sorting with sorted
- 10.44. sort vs. sorted
- 10.45. Sorted and change - shallow copy
- 10.46. Sorting characters of a string
- 10.47. range
- 10.48. Looping over index
- 10.49. Enumerate lists
- 10.50. List operators
- 10.51. List of lists
- 10.52. List assignment
- 10.53. List documentation
- 10.54. Exercise: color selector menu
- 10.55. Exercise: count digits
- 10.56. Exercise: Create list
- 10.57. Exercise: Count words
- 10.58. Exercise: Check if number is prime
- 10.59. Exercise: DNA sequencing
- 10.60. Solution: menu
- 10.61. Solution: count digits
- 10.62. Solution: Create list
- 10.63. Solution: Count words
- 10.64. Solution: Check if number is prime
- 10.65. Solution: DNA sequencing
- 10.66. Solution: DNA sequencing other
- 10.67. Solution: DNA sequencing using replace
- 10.68. Solution: DNA sequencing using regex
- 10.69. Solution: DNA sequencing with filter
- 10.70. Solution: DNA sequencing with filter and lambda
- 10.71. [].extend
- 10.72. append vs. extend
- 10.73. split and extend
- 11. Tuples
- 11.1. Create tuple
- 11.2. Convert list to tuple and tuple to list
- 11.3. Enumerate returns tuples
- 11.4. Change a tuple
- 11.5. Sort tuples
- 11.6. Sort tuples by specific elements
- 11.7. Sort and secondary sort
- 12. Files
- 12.1. File types: Text vs Binary
- 12.2. Open vs. Read vs. Load
- 12.3. Binary files: Images
- 12.4. Reading an Excel file
- 12.5. Reading a YAML file
- 12.6. Read and analyze a text file
- 12.7. Open and read file (easy but not recommended)
- 12.8. Open and read file using with (recommended)
- 12.9. Read file remove newlines
- 12.10. Filename on the command line
- 12.11. Filehandle with return
- 12.12. Read all the lines into a list
- 12.13. Read all the characters into a string (slurp)
- 12.14. Not existing file
- 12.15. Open file exception handling
- 12.16. Open many files - exception handling
- 12.17. Writing to file
- 12.18. Print to file
- 12.19. Append to file
- 12.20. Binary mode
- 12.21. Does file exist? Is it a file?
- 12.22. Direct access of a line in a file
- 12.23. Exercise: count digits
- 12.24. Exercise: remove newlines
- 12.25. Exercise: print lines with Report
- 12.26. Exercise: color selector
- 12.27. Exercise: ROT13
- 12.28. Exercise: Combine lists
- 12.29. Exercise: Number guessing game - save to file
- 12.30. Solution: count numbers
- 12.31. Solution: remove newlines
- 12.32. Solution: print lines with Report
- 12.33. Solution: color selector
- 12.34. Solution: ROT13
- 12.35. Solution: Combine lists
- 12.36. Solution: Combine lists with tuple
- 12.37. Filehandle using with and not using it
- 13. Dictionary (hash)
- 13.1. What is a dictionary
- 13.2. When to use dictionaries
- 13.3. Various dictionary examples
- 13.4. Dictionary
- 13.5. Create dictionary
- 13.6. keys
- 13.7. Loop over keys
- 13.8. Loop over dictionary keys
- 13.9. Loop using items
- 13.10. values
- 13.11. Not existing key
- 13.12. Get key
- 13.13. Does the key exist?
- 13.14. Does the value exist?
- 13.15. Delete key
- 13.16. List of dictionaries
- 13.17. Shared dictionary
- 13.18. immutable collection: tuple as dictionary key
- 13.19. immutable numbers: numbers as dictionary key
- 13.20. Sort a dictionary
- 13.21. Sort dictionary values
- 13.22. Sort dictionary by value
- 13.23. Sort dictionary keys by value (another example)
- 13.24. Insertion Order is kept
- 13.25. Change order of keys in dictionary - OrderedDict
- 13.26. Set order of keys in dictionary - OrderedDict
- 13.27. Setdefault
- 13.28. Exercise: count characters
- 13.29. Exercise: count words
- 13.30. Exercise: count words from a file
- 13.31. Exercise: Apache log
- 13.32. Exercise: Combine lists again
- 13.33. Exercise: counting DNA bases
- 13.34. Exercise: Count Amino Acids
- 13.35. Exercise: List of dictionaries
- 13.36. Exercise: Dictionary of dictionaries
- 13.37. Exercise: Age limit with dictionaries
- 13.38. Exercise: Merge files with timestamps
- 13.39. Solution: count characters
- 13.40. Default Dict
- 13.41. Solution: count characters with default dict
- 13.42. Solution: count words (plain)
- 13.43. Solution: count words (defaultdict)
- 13.44. Solution: count words (Counter)
- 13.45. Solution: count words in file
- 13.46. Solution: Apache log
- 13.47. Solution: Apache log using split
- 13.48. Solution: Combine files
- 13.49. Solution: Combine files-improved
- 13.50. Solution: counting DNA bases
- 13.51. Solution: Count Amino Acids
- 13.52. Solution: List of dictionaries
- 13.53. Solution: Dictionary of dictionaries
- 13.54. Solution: Age limit with dictionaries
- 13.55. Solution: Merge files with timestamps
- 13.56. Do not change dictionary in loop
- 13.57. Named tuple (sort of immutable dictionary)
- 13.58. Create dictionary from List
- 13.59. Sort Hungarian letters (lookup table)
- 14. Sets
- 14.1. sets
- 14.2. set operations
- 14.3. Creating a set
- 14.4. Creating a set from a list
- 14.5. Converting set to list
- 14.6. Creating an empty set
- 14.7. Adding an element to a set (add)
- 14.8. Merging one set into another set (update)
- 14.9. set intersection
- 14.10. set subset
- 14.11. set symmetric difference
- 14.12. set union
- 14.13. set relative complement (difference)
- 14.14. Set of numbers
- 14.15. Set of lists
- 14.16. Set of tuples
- 14.17. Create set from List
- 15. Code Reuse
- 15.1. Permutations
- 15.2. k-Permutations
- 15.3. Binomial coefficient
- 15.4. Binomial coefficient - factorial function
- 15.5. k-Permutations - factorial function
- 15.6. Permutations - factorial funcion
- 15.7. mymath module
- 15.8. Permutations - module
- 15.9. k-Permutations - module
- 15.10. Binomial coefficient - module
- 16. Functions (subroutines)
- 16.1. Why use functions?
- 16.2. Defining simple function
- 16.3. Passing positional parameters to a function
- 16.4. Function parameters can be named
- 16.5. Mixing positional and named parameters
- 16.6. Mixing positional and named parameters - order
- 16.7. Default values, optional parameters, optional parameters
- 16.8. Default value in first param
- 16.9. Several defaults, using names
- 16.10. Default list
- 16.11. Arbitrary number of arguments *
- 16.12. Arbitrary number of arguments passing a lists
- 16.13. Arbitrary number of arguments passing a tuple
- 16.14. Fixed parmeters before the others
- 16.15. Pass arbitrary number of functions
- 16.16. Arbitrary key-value pairs in parameters **
- 16.17. Pass a real dictionary
- 16.18. The dictionary contains copy
- 16.19. The dictionary contains copy but NOT deep copy!
- 16.20. Extra key-value pairs in parameters
- 16.21. Extra key-value pairs in parameters for email
- 16.22. Every parameter option
- 16.23. Duplicate declaration of functions (multiple signatures)
- 16.24. Pylint duplicate declaration
- 16.25. Return more than one value
- 16.26. Recursive factorial
- 16.27. Recursive Fibonacci
- 16.28. Non-recursive Fibonacci
- 16.29. Unbound recursion
- 16.30. Set recurions limit
- 16.31. Variable assignment and change - Immutable
- 16.32. Variable assignment and change - Mutable list
- 16.33. Variable assignment and change - Mutabled dict
- 16.34. Parameter passing of functions
- 16.35. Passing references
- 16.36. Function documentation
- 16.37. Sum ARGV
- 16.38. Copy-paste code
- 16.39. Copy-paste code fixed
- 16.40. Copy-paste code further improvement
- 16.41. Palindrome
- 16.42. Exit vs return vs break and continue
- 16.43. Exercise: statistics
- 16.44. Exercise: Pascal's triangle
- 16.45. Exercise: Pascal's triangle functions
- 16.46. Exercise: recursive dependency tree
- 16.47. Exercise: dependency tree
- 16.48. Exercise: Tower of Hanoi
- 16.49. Exercise: Merge and Bubble sort
- 16.50. Exercise: Refactor previous solutions to use functions
- 16.51. Exercise: Number guessing - functions
- 16.52. Solution: statistics
- 16.53. Solution: Pascal triangle
- 16.54. Solution: Pascal triangle functions
- 16.55. Solution: recursive
- 16.56. Solution: Tower of Hanoi
- 16.57. Solution: Merge and Bubble sort
- 17. Modules
- 17.1. Goal of having modules
- 17.2. Before modules
- 17.3. Create modules
- 17.4. path to load modules from - The module search path
- 17.5. sys.path - the module search path
- 17.6. Project directory layouts
- 17.7. Flat project directory structure
- 17.8. Absolute path
- 17.9. Relative path
- 17.10. Relative path explained
- 17.11. Submodules
- 17.12. Python modules are compiled
- 17.13. How "import" and "from" work?
- 17.14. Execute at import time
- 17.15. Runtime loading of modules
- 17.16. Conditional loading of modules
- 17.17. What is in our namespace?
- 17.18. Runtime import
- 17.19. Duplicate importing of functions
- 17.20. Duplicate importing of functions - solved
- 17.21. Script or library
- 17.22. Script or library - import
- 17.23. Script or library - from import
- 17.24. Scope of import
- 17.25. Import multiple times
- 17.26. Do not import *
- 17.27. Exercise: Number guessing
- 17.28. Exercies: Scripts and modules
- 17.29. Exercise: Module my_sum
- 17.30. Exercise: Convert your script to module
- 17.31. Exercise: Add doctests to your own code
- 17.32. Solution: Module my_sum
- 17.33. Loaded modules and their path
- 17.34. Built-in modules
18. Other Modules
- 18.1. assert to verify values
- 18.2. mycalc as a self testing module
- 18.3. doctest
- 18.4. Export import
- 18.5. Export import with all
- 18.6. import module
- 18.7. deep copy list
- 18.8. deep copy dictionary
- 19. Python standard modules (standard packages)
- 19.1. Some Standard packages
- 20. math
- 20.1. math examples
- 21. sys
- 21.1. sys module
- 21.2. Writing to standard error (stderr)
- 21.3. exit prints to STDERR
- 22. os
- 22.1. python which OS are we running on (os, platform)
- 22.2. Current directory (getcwd, pwd, chdir)
- 22.3. OS path
- 22.4. os.path.join
- 22.5. Directory listing
- 22.6. Directory listing using glob
- 22.7. Traverse directory tree - list directories recursively
- 22.8. OS dir (mkdir, makedirs, remove, rmdir)
- 22.9. expanduser - handle tilde ~ the home directory of the user
- 22.10. Get process ID
- 22.11. External command with system
- 22.12. Accessing the system environment variables from Python
- 22.13. Set environment variables on the fly
- 22.14. Reading the .env environment file in Python
- 22.15. Set env and run command
- 23. Pathlib
- 23.1. Pathlib example
- 23.2. Pathlib cwd
- 23.3. Pathlib get extension (suffix)
- 23.4. Pathlib current file
- 23.5. Pathlib parents (dirname)
- 23.6. Pathlib parts (basename)
- 23.7. Pathlib exists
- 23.8. Pathlib iterdir (flat)
- 23.9. Pathlib mkdir (makedir)
- 24. shutil
- 24.1. shutil module
- 25. time
- 25.1. time module
- 25.2. sleep in Python
- 25.3. timer
- 25.4. Current date and time datetime now
- 25.5. Converting string to datetime (parsing date and time string)
- 25.6. Parse datetime string with and without timezone information
- 25.7. datetime fromisoformat
- 25.8. date fromisoformat (only date, no time)
- 25.9. datetime arithmeticis (subtract)
- 25.10. Timezone aware datetime
- 25.11. datetime arithmeticis (compare, sort)
- 25.12. datetime arithmeticis (addition)
- 25.13. Rounding datetime object to nearest second (removing microseconds)
- 25.14. Rounding datetime object to date (removing hours, minutes, seconds)
- 25.15. Convert datetime object to date object
- 25.16. Convert datetime object to time object
- 25.17. Today (date)
- 26. subprocess
- 26.1. External CLI tool to demo subprocess
- 26.2. Run with os.system
- 26.3. Run external process let STDOUT and STDERR through
- 26.4. Run external process and capture STDOUT and STDERR separately
- 26.5. Run external process and capture STDOUT and STDERR merged together
- 26.6. subprocess in the background
- 26.7. subprocess collect output while external program is running
- 26.8. Exercise: Processes
- 26.9. Subprocess TBD
- 27. Command line arguments with argparse
- 27.1. Command line arguments
- 27.2. Modules to handle the command line
- 27.3. argparse
- 27.4. Basic usage of argparse
- 27.5. Positional argument
- 27.6. Many positional argument
- 27.7. Convert to integers
- 27.8. Convert to integer
- 27.9. Named arguments
- 27.10. Boolean Flags
- 27.11. Short names
- 27.12. argparse print help explicitely
- 27.13. Argparse xor - mutual exlucise - only one - exactly one
- 27.14. Argparse argument with default and optional value
- 27.15. Conditional required parameter with argparse
- 27.16. Exercise: Command line parameters
- 27.17. Exercise: argparse positional and named
- 27.18. Other
- 28. JSON
- 28.1. JSON - JavaScript Object Notation
- 28.2. JSON dumps
- 28.3. JSON loads
- 28.4. dump
- 28.5. load
- 28.6. Round trip
- 28.7. Pretty print JSON
- 28.8. Serialize Datetime objects in JSON
- 28.9. Sort keys in JSON
- 28.10. Set order of keys in JSON - OrderedDict
- 28.11. Exercise: Counter in JSON
- 28.12. Exercise: Phone book in JSON
- 28.13. Solution: Counter in JSON
- 28.14. Solution: Phone book
- 29. YAML
- 29.1. YAML - YAML Ain't Markup Language
- 29.2. Read YAML
- 29.3. Write YAML
- 29.4. Exercise: Counter in YAML
- 29.5. Exercise: Phone book in YAML
- 29.6. Solution: Counter in YAML
- 30. Exception handling