1000 Python Examples

84
1000 Python Examples
English | 2020 | ISBN: n/a | 997 Pages | True (PDF, EPUB, MOBI) | 7.01 MB


First steps

What is Python?
What is needed to write a program?
The source (code) of Python
Python 2 vs. Python 3
Installation
Installation on Linux
Installation on Apple Mac OSX
Installation on MS Windows
Editors, IDEs
Documentation
Program types
Python on the command line
First script - hello world
Examples
Comments
Variables
Exercise: Hello world
What is programming?
What are the programming languages
A written human language
A programming language
Words and punctuation matter!
Literals, Value Types in Python
Floating point limitation
Value Types in Numpy
Rectangular (numerical operations)
Multiply string
Add numbers
Add strings
Exercise: Calculations
Solution: Calculations

Second steps

Modules
A main function
The main function - called
Indentation
Conditional main
Input - Output I/O
print in Python 2
print in Python 3
print in Python 2 as if it was Python 3
Exception: SyntaxError: Missing parentheses in call
Prompting for user input in Python 2
Prompting for user input in Python 3
Python2 input or raw_input?
Prompting both Python 2 and Python 3
Add numbers entered by the user (oups)
Add numbers entered by the user (fixed)
How can I check if a string can be converted to a number?
Converting string to int
Converting float to int
Conditionals: if
Conditionals: if - else
Conditionals: if - else (other example)
Conditionals: else if
Conditionals: elif
Ternary operator
Case or Switch in Python
Exercise: Rectangular
Exercise: Calculator
Exercise: Standard Input
Solution: Area of rectangular
Solution: Calculator
Command line arguments
Command line arguments - len
Command line arguments - exit
Exercise: Rectangular (argv)
Exercise: Calculator (argv)
Solution: Area of rectangular (argv)
Solution: Calculator eval
Solution: Calculator (argv)
Compilation vs. Interpretation
Is Python compiled or interpreted?
Flake8 checking

Numbers

Numbers
Operators for Numbers
Integer division and the future
Pseudo Random Number
Fixed random numbers
Rolling dice - randrange
Random choice
built-in method
Exception: TypeError: ‘module’ object is not callable
Fixing the previous code
Exception: AttributeError: module ‘random’ has no attribute
Exercise: Number guessing game - level 0
Exercise: Fruit salad
Solution: Number guessing game - level 0
Solution: Fruit salad

Boolean

if statement again
True and False
Boolean
True and False values in Python
Comparision operators
Do NOT Compare different types
Boolean operators
Boolean truth tables
Short circuit
Short circuit fixed
Incorrect use of conditions
Exercise: compare numbers
Exercise: compare strings
Solution: compare numbers
Solution: compare strings

Strings

Single quoted and double quoted strings
Long lines
Triple quoted strings (multiline)
String length (len)
String repetition and concatenation
A character in a string
String slice (instead of substr)
Change a string
How to change a string
String copy
String functions and methods (len, upper, lower)
index in string
index in string with range
rindex in string with range
find in string
Find all in the string
in string
index if in string
Encodings: ASCII, Windows-1255, Unicode
raw strings
ord
ord in a file
chr - number to character
Exercise: one string in another string
Exercise: to ASCII CLI
Exercise: from ASCII CLI
Solution: one string in another string
Solution: compare strings
Solution: to ASCII CLI
Solution: from ASCII CLI

Loops

Loops: for-in and while
for-in loop on strings
for-in loop on list
for-in loop on range
Iterable, iterator
for in loop with early end using break
for in loop skipping parts using continue
for in loop with break and continue
while loop
Infinite while loop
While with complex expression
While with break
While True
Duplicate input call
Eliminate duplicate input call
do while loop
while with many continue calls
Break out from multi-level loops
Exit vs return vs break and continue
Exercise: Print all the locations in a string
Exercise: Number guessing game
Exercise: MasterMind
Exercise: Count unique characters
Solution: Print all the locations in a string
Solution 1 for Number Guessing
Solution for Number Guessing (debug)
Solution for Number Guessing (move)
Solution for Number Guessing (multi-game)
Solution: MasterMind
Solution: Count unique characters
MasterMind to debug

PyCharm

PyCharm Intro
PyCharm Project
PyCharm Files
PyCharm - run code
PyCharm Python console at the bottom left
Refactoring example (with and without pycharm)

Formatted printing

format - sprintf
Examples using format - indexing
Examples using format with names
Format columns
Examples using format - alignment
Format - string
Format characters and types
Format floating point number
f-strings (formatted string literals)
printf using old %-syntax
Format braces, bracket, and parentheses
Examples using format with attributes of objects
raw f-strings

Lists

Anything can be a lists
Any layout
Lists
List slice with steps
Change a List
Change with steps
List assignment and list copy
join
join list of numbers
split
for loop on lists
in list
Where is the element in the list
Index improved
[].insert
[].append
[].remove
Remove element by index [].pop
Remove first element of list
Remove several elements of list by index
Use list as a queue
Queue using deque from collections
Fixed size queue
List as a stack
stack with deque
Exercies: Queue
Exercise: Stack
Solution: Queue with list
Solution: Queue with deque
Solution: Reverse Polish calculator (stack) with lists
Solution: Reverse Polish calculator (stack) with deque
Debugging Queue
sort
sort numbers
sort mixed
key sort
Sort tuples
sort with sorted
sort vs. sorted
key sort with sorted
Sorting characters of a string
range
Looping over index
Enumerate lists
List operators
List of lists
List assignment
List documentation
tuple
Exercise: color selector menu
Exercise: count digits
Exercise: Create list
Exercise: Count words
Exercise: Check if number is prime
Exercise: DNA sequencing
Solution: menu
Solution: count digits
Solution: Create list
Solution: Count words
Solution: Check if number is prime
Solution: DNA sequencing
Solution: DNA sequencing with filter
Solution: DNA sequencing with filter and lambda
[].extend
append vs. extend
split and extend

Files

Open and read file
Filename on the command line
Filehandle with and without
Filehandle with return
Read file remove newlines
Read all the lines into a list
Read all the characters into a string (slurp)
Not existing file
Open file exception handling
Open many files - exception handling
Writing to file
Append to file
Binary mode
Does file exist? Is it a file?
Exercise: count numbers
Exercise: strip newlines
Exercise: color selector
Exercise: ROT13
Exercise: Combine lists
Solution: count numbers
Solution: strip newlines
Solution: color selector
Solution: Combine lists
Read text file
Open and read file
Direct access of a line in a file
Example

Dictionary (hash)

What is a dictionary
When to use dictionaries
Dictionary
keys
Loop over keys
Loop using items
values
Not existing key
Get key
Does the key exist?
Does the value exist?
Delete key
List of dictionaries
Shared dictionary
immutable collection: tuple as dictionary key
immutable numbers: numbers as dictionary key
Sort dictionary by value
Sort dictionary keys by value
Insertion Order is kept
Change order of keys in dictionary - OrderedDict
Set order of keys in dictionary - OrderedDict
Exercise: count characters
Exercise: count words
Exercise: count words from a file
Exercise: Apache log
Exercise: Combine lists again
Exercise: counting DNA bases
Exercise: Count Amino Acids
Exercise: List of dictionaries
Exercise: Dictinoary of dictionaries
Solution: count characters
Solution: count characters with default dict
Solution: count words
Solution: count words in file
Solution: Apache log
Solution: Combine lists again
Solution: counting DNA bases
Solution: Count Amino Acids
Loop over dictionary keys
Do not change dictionary in loop
Default Dict

Sets

sets
set operations
set intersection
set subset
set symmetric difference
set union
set relative complement
set examples
defining an empty set
Adding an element to a set (add)
Merging one set into another set (update)

Functions (subroutines)

Defining simple function
Defining a function
Parameters can be named
Mixing positional and named parameters
Default values
Several defaults, using names
Arbitrary number of arguments *
Fixed parmeters before the others
Arbitrary key-value pairs in parameters **
Extra key-value pairs in parameters
Every parameter option
Duplicate declaration of functions (multiple signatures)
Recursive factorial
Recursive Fibonacci
Non-recursive Fibonacci
Unbound recursion
Variable assignment and change - Immutable
Variable assignment and change - Mutable
Parameter passing of functions
Passing references
Function documentation
Sum ARGV
Copy-paste code
Copy-paste code fixed
Copy-paste code further improvement
Palindrome
Exercise: statistics
Exercise: recursive
Exercise: Tower of Hanoi
Exercise: Merge and Bubble sort
Solution: statistics
Solution: recursive
Solution: Tower of Hanoi
Solution: Merge and Bubble sort

Modules

Before modules
Create modules
path to load modules from - The module search path
sys.path - the module search path
Flat project directory structure
Absolute path
Relative path
Python modules are compiled
How “import” and “from” work?
Runtime loading of modules
Conditional loading of modules
Duplicate importing of functions
Script or library
Script or library - import
Script or library - from import
assert to verify values
mycalc as a self testing module
doctest
Scope of import
Export import
Export import with all
import module
Execute at import time
Import multiple times
Exercise: Number guessing
Exercies: Scripts and modules
Exercise: Module my_sum
Exercise: Convert your script to module
Exercise: Add doctests to your own code
Solution: Module my_sum

Regular Expressions

What are Regular Expressions (aka. Regexes)?
What are Regular Expressions good for?
Examples
Where can I use it ?
grep
Regexes first match
Match numbers
Capture
Capture more
Capture even more
findall
findall with capture
findall with capture more than one
Any Character
Match dot
Character classes
Common characer classes
Negated character class
Optional character
Regex 0 or more quantifier
Quantifiers
Quantifiers limit
Quantifiers on character classes
Greedy quantifiers
Minimal quantifiers
Anchors
Anchors on both end
Match ISBN numbers
Matching a section
Matching a section - minimal
Matching a section negated character class
DOTALL S (single line)
MULTILINE M
Two regex with logical or
Alternatives
Grouping and Alternatives
Internal variables
More internal variables
Regex DNA
Regex IGNORECASE
Regex VERBOSE X
Substitution
findall capture
Fixing dates
Duplicate numbers
Remove spaces
Replace string in assembly code
Full example of previous
Split with regex
Exercises: Regexes part 1
Exercise: Regexes part 2
Exercise: Sort SNMP numbers
Exercise: parse hours log file and give report
Exercise: Parse ini file
Exercise: Replace Python
Exercise: Extract phone numbers
Solution: Sort SNMP numbers
Solution: parse hours log file and give report
Solution: Processing INI file manually
Solution: Processing config file
Solution: Extract phone numbers
Regular Expressions Cheat sheet
Fix bad JSON
Fix very bad JSON
Raw string or escape
Remove spaces regex
Regex Unicode
Anchors Other example

Python standard modules

Some Standard modules
sys
Writing to standard error (stderr)
Current directory (getcwd, pwd, chdir)
OS dir (mkdir, makedirs, remove, rmdir)
python which OS are we running on (os, platform)
Get process ID
OS path
Traverse directory tree - list directories recursively
os.path.join
Directory listing
expanduser - handle tilde ~
Listing specific files using glob
External command with system
subprocess
subprocess in the background
Accessing the system environment variables from Python
Set env and run command
shutil
time
sleep in Python
timer
Current date and time datetime now
Converting string to datetime
datetime arithmeticis
Rounding datetime object to nearest second
Signals and Python
Sending Signal
Catching Signal
Catching Ctrl-C on Unix
Catching Ctrl-C on Unix confirm
Alarm signal and timeouts
deep copy list
deep copy dictionary
Exercise: Catching Ctrl-C on Unix 2nd time
Exercise: Signals
Ctrl-z

JSON

JSON - JavaScript Object Notation
dumps
loads
dump
load
Round trip
Pretty print JSON
Sort keys in JSON
Set order of keys in JSON - OrderedDict
Exercise: Counter in JSON
Exercise: Phone book
Exercise: Processes
Solution: Counter in JSON
Solution: Phone book

Command line arguments with argparse

Modules to handle the command line
argparse
Basic usage of argparse
Positional argument
Many positional argument
Convert to integers
Convert to integer
Named arguments
Boolean Flags
Short names
Exercise: Command line parameters
Exercise: argparse positional and named

Exception handling

Hierarchy of calls
Handling errors as return values
Handling errors as exceptions
A simple exception
Working on a list
Catch ZeroDivisionError exception
Module to open files and calculate something
File for exception handling example
Open files - exception
Handle divide by zero exception
Handle files - exception
Catch all the exceptions and show their type
List exception types
Exceptions
How to raise an exception
Stack trace
Exercies: Exception int conversion
Exercies: Raise Exception
Solution: Exception int conversion (specific)
Solution: Exception int conversion (all other)
Solution: Raise Exception

Classes - OOP - Object Oriented Programming

Why Object Oriented Programming?
Generic Object Oriented Programming terms
OOP in Python
OOP in Python (numbers, strings, lists)
OOP in Python (argparse)
Create a class
Import module containing class
Import class from module
Initialize a class - constructor, attributes
Attributes are not special
Create Point class
Initialize a class - constructor, attributes
Methods
Stringify class
Inheritance
Inheritance - another level
Modes of method inheritance
Modes of method inheritance - implicit
Modes of method inheritance - override
Modes of method inheritance - extend
Modes of method inheritance - delegate - provide
Composition - Line
Some comments
Class in function
Serialization of instances with pickle
Quick Class definition and usage
Exercise: Add move_rad to based on radians
Exercise: Improve previous examples
Exercise: Polygon
Exercise: Number
Exercise: Library
Exercise: Bookexchange
Exercise: Represent turtle graphics
Solution - Polygon

PyPi - Python Package Index

What is PyPi?
Easy Install
pip
Upgrade pip
PYTHONPATH
Virtualenv
Virtualenv for Python 3

SQLite Database Access

SQLite
Connecting to SQLite database
Create TABLE in SQLite
INSERT data into SQLite database
SELECT data from SQLite database
A counter

MySQL

Install MySQL support
Create database user (manually)
Create database (manually)
Create table (manually)
Connect to MySQL
Connect to MySQL and Handle exception
Select data
Select more data
Select all data fetchall
Select some data fetchmany
Select some data WHERE clause
Select into dictionaries
Insert data
Update data
Delete data
Exercise MySQL
Exercise: MySQL Connection
Solution: MySQL Connection

PostgreSQL

PostgreSQL install
Python and Postgresql
PostgreSQL connect
INSERT
INSERT (from command line)
SELECT
DELETE

SQLAlchemy

SQLAlchemy hierarchy
SQLAlchemy engine
SQLAlchemy autocommit
SQLAlchemy engine CREATE TABLE
SQLAlchemy engine INSERT
SQLAlchemy engine SELECT
SQLAlchemy engine SELECT all
SQLAlchemy engine SELECT fetchall
SQLAlchemy engine SELECT aggregate
SQLAlchemy engine SELECT IN
SQLAlchemy engine SELECT IN with placeholders
SQLAlchemy engine connection
SQLAlchemy engine transaction
SQLAlchemy engine using context managers
Exercise: Create table
SQLAlchemy Metada
SQLAlchemy types
SQLAlchemy ORM - Object Relational Mapping
SQLAlchemy ORM create
SQLAlchemy ORM schema
SQLAlchemy ORM reflection
SQLAlchemy ORM INSERT after automap
SQLAlchemy ORM INSERT
SQLAlchemy ORM SELECT
SQLAlchemy ORM SELECT cross tables
SQLAlchemy ORM SELECT and INSERT
SQLAlchemy ORM UPDATE
SQLAlchemy ORM logging
Solution: Create table
Exercise: Inspector
SQLAlchemy CREATE and DROP
SQLAlchemy Notes
SQLAlchemy Meta SQLite CREATE
SQLAlchemy Meta Reflection
SQLAlchemy Meta INSERT
SQLAlchemy Meta SELECT

NoSQL

Types of NoSQL databases

MongoDB

MongoDB CRUD
Install MongoDB support
Python MongoDB insert
MongoDB CLI
Python MongoDB find
Python MongoDB find refine
Python MongoDB update
Python MongoDB remove (delete)

Redis

Redis CLI
Redis list keys
Redis set get
Redis incr
Redis incrby
Redis setex

Web client

urllib the web client
urllib2 the web client
httpbin.org
requests get
Download image using requests
Download image as a stream using requests
Download zip file
Extract zip file
Interactive Requests
requests get JSON
requests get JSON UserAgent
requests get JSON UserAgent
requests get header
requests change header
requests post
Tweet
API config file
bit.ly
Exercise: Combine web server and client

Python Web server

Hello world web
Dump web environment info
Web echo
Web form
Resources

Python Flask

Python Flask intro
Python Flask installation
Flask: Hello World
Flask hello world + test
Flask generated page - time
Flask: Echo GET
Flask: Echo POST
Flask: templates
Flask: templates
Flask: templates with parameters
Flask: runner
Exercise: Flask calculator
Static files
Flask Logging
Flask: Counter
Color selector without session
Session management
Flask custom 404 page
Flask Error page
Flask URL routing
Flask Path params
Flask Path params (int)
Flask Path params add (int)
Flask Path params add (path)
Jinja loop, conditional, include
Exercise: Flask persistent
Exercise: Flask persistent
Flask Exercises

No comments have been posted yet. Please feel free to comment first!

    Load more replies

    Join the conversation!

    Login or Register
    to post a comment.

    Advanced Search