Basics
Points about Python:
In python, everything is an object
A case sensitive language
Comment character in Python: hash key
Basic modes:
Script
Interactive
Indentation (no curly braces)
Interactive Mode
To access the interactive shell:
$ python -i
To quit, type:
o Quit(), or
o Exit(), or
o Ctrl + D
Virtual Environments and Packages
(5, “12. Virtual environments …”)
Applications need specific Python packages and modules. A standard Python installation won't satisfy all
requirements. Virtual environment is for this purpose. For ex: application1 uses virtual environment 1
(python version 2, ...), application2 uses virtual environment 2 (python version 3, ...)
Creating a venv:
python3 -m venv "path_of_directory" (-m: module)
o $ python3 -m venv my_virtual_env
To activate the environment
o $ source my_virtual_env/bin/activate
To deactivate, use
o $ deactivate
Coding Style
https://docs.python.org/2/tutorial/controlflow.html#more-on-defining-functions
PEP 8: https://www.python.org/dev/peps/pep-0008/
Variable naming convention
Function naming convention
Variable
Points:
No need to declare a variable (undefined var) before using it (ex: x = 7). However, to print a
variable (ex: print(x)), this var has to be defined.
A var can point to different data type at different times
Variable assignment:
o Multiple assignments on the same line
Local & Global Scope (3,67)
o Global statement (3,70): read the note on (3,75) cause very important
Checking the data type of an object:
o Print(type(x))
Data types: …
Number
o Float
Define a float number?
Boolean (3, 32)
String
List
Tuple (coursin of List) (3,96)
o https://docs.python.org/2/tutorial/datastructures.html
Set
o https://docs.python.org/2/tutorial/datastructures.html
o Set comprehensions (similar to “list comprehensions)
Dictionary (similar to an “associative array”)
o https://docs.python.org/2/tutorial/datastructures.html
Sequence Types: str, unicode, list, tuple, bytearray, buffer, xrange
o https://docs.python.org/2/library/stdtypes.html#typesseq
Scope
module scope
Variables created outside of any function or class
local scope
Variables created inside of a function or class
Data Types
String
Quotes
Use any of the below to open and close a string
single quotes
double quotes
triple single quotes
triple double quotes
To include double quotes inside a string, enclose them with single quotes and vice versa. Or we can also
use escape character ( \ )
triple quotes: create multiline strings or docstrings
Concatenation
+ (glue) and * (repeat: associated with loop) operators
“one” + “two”
Others
About string:
Immutable
Quotes
Escape characters (3,124)
Raw strings (3,125)
Special characters
Indexing: positive vs negative index
Slicing (substring)
Length
Formatting
Unicode strings
List
About a list:
Ordered list of items
Mutable (can be changed after created)
Items can be different data types
Can contain duplicate items
Create a list
>>> mylist = ['a', 1, 18.2]
Display element(s) of a list:
>>> mylist[0] (display the 1st element using index 0)
>>> mylist (display the whole list)
o Output: ['a', 1, 18.2]
Adding items to a list:
“+” operator
append() method
Remove item(s):
Comparison: del – remove – pop
o https://stackoverflow.com/questions/11520492/difference-between-del-remove-and-
pop-on-lists
List methods:
Append: add items to a list
o >>> mylist.append(‘new’)
Functions to use on a list:
list([iterable]): returns a mutable sequence list of elements
o iterable is optional. If there is no argument, an empty list is returned
o iterable can be a string, list, tuple, set, dictionary, ...
Process the following infor (re-arrange the information)
Slicing
o Slices assignment
Length
Nesting lists
Can be used as a stack or a queue
o https://docs.python.org/2/tutorial/datastructures.html
Methods of a list objects:
o https://docs.python.org/2/tutorial/datastructures.html
3 useful built-in functions to use with list:
o Filter()
o Map()
o Reduce()
Concise ways to create a list (term: list comprehensions)
o https://docs.python.org/2/tutorial/datastructures.html
o Tags: bracket, expression, for clause
o Nested list comprehensions
Tuple
About tuple:
Just like a list; except:
Immutable (cannot be changed)
Create a tuple:
>>> mytup = ('a', 1, 18.2)
Display element(s) of a tuple:
>>> mytup[0]
>>> mytup (display the whole tuple)
o Output: ('a', 1, 18.2)
[Unordered] Dictionary
About dictionary:
Unordered key-value pairs
Keys don’t have to be same data type
Values don’t have to be same data type
A key is unique, has to be immutable and hash-able. For ex, a key can be a tuple (but not a list),
int, float, bool, str, bytes, ...
Create a dictionary
>>> mydic = {"apples": 5, "pears": 2, "oranges": 9}
Display element(s) of a dictionary:
>>> mydic['apples']
Dictionary methods:
items(): yield the key-value pairs as a "list" of tuples
How to iterate through a dictionary
How to:
Iterate through the keys
o >>> fruit_inventory = {"apples": 5, "pears": 2, "oranges": 9}
o >>> for fruit in fruit_inventory:
o ... print(fruit)
o ...
o oranges
o apples
o pears
Iterate through (key, value) pairs:
o >>> fruit_inventory = {"apples": 5, "pears": 2, "oranges": 9}
o >>> for fruit in fruit_inventory.items():
o ... print(fruit)
o ...
o ('oranges', 9)
o ('apples', 5)
o ('pears', 2)
Iterate through (key, value) pairs with the "key" and "value" being assigned to different
variables (feature: unpacking)
o >>> fruit_inventory = {"apples": 5, "pears": 2, "oranges": 9}
o >>> for fruit, quantity in fruit_inventory.items():
o ... print("You have {} {}.".format(quantity, fruit))
o ...
o You have 5 apples.
o You have 2 pears.
o You have 9 oranges.
Ordered Dictionary
About Ordered Dictionary
OrderedDict must be imported first
o >>> from collections import OrderedDict
o >>> od = OrderedDict()
o >>> od['apple'] = 5
o >>> od['orange'] = 6
Data type conversion
Operators
Comparison operators
(3, 33)
Comparing sequences and other types
https://docs.python.org/2/tutorial/datastructures.html
Boolean operators
(3,35)
Modules
https://docs.python.org/2/tutorial/modules.html
Tags & Associations
import, reuse
private & global symbol table
Execute modules as scripts
Modules search path
Compiled Python file
Tags & Associations
o *.pyc & *.pyo file formats
o Python interpreter, byte-compiled, platform independent (sharing)
o *.pyo file format, optimized code, flag (interpreter)
Standard modules
Tags & Associations
o “Python Library Reference”
dir() function
Tags & Associations
o Names’ definitions
Packages vs Modules
A package is a collection (directory) of Python modules, while a module is a single Python file. It contains
an additional __init__.py file to distinguish a package from a directory that happens to contain a bunch
of Python scripts
Lib/urllib is a package. Inside this contains modules
Importing modules
(3, 57)
2 different syntaxes:
import os
o Use dot-syntax to access its functionality
os.path.abspath()
from os.path import abspath
o Pull in only the functionality we need
o Can call the function directly (no need the prefix)
abspath()
o Using the full name is recommended for a more readable code
Input/Output
Input
Ex:
name = input("give me your name: ")
o include the answer within quotes (singles or doubles)
print("Your name is " + name)
Output
Displaying output
https://docs.python.org/2/tutorial/inputoutput.html
3 ways of writing values:
expression statements
print statement
write() method of file objects
Print command:
Print 3
Print “hello”
Print (“hello”)
print ("hello" + "\n") * 3
o print “hello\n” 3 times on the output (no need to use control flow statements like while,
for, or …)
Concatenation:
o A string and a number
Print(“your age is: “), age
Print “your age is: “, age
Print “your age is: “, str(age)
Print(“your age is:”, age)
o The function “print” joins parameters together with a space separator between them
Keyword arguments (3,65): end, sep
Print out multiple lines:
Use triple-quotes (either doubles or singles): """...""" or '''...'''. End of lines are automatically
included in the string, but it’s possible to prevent this by adding a \ at the end of the line.
A trailing comma avoids the newline after the output. Compare the followings:
First:
o ... a, b = 0, 1
o >>> while b < 10:
o ... print b
o ... a, b = b, a+b
Second:
o >>> a, b = 0, 1
o >>> while b < 1000:
o ... print b,
o ... a, b = b, a+b
Formatting output
2 ways:
Manual string manipulation
str.format() method
o
% operator (old way of doing)
Reading & Writing files
File Objects
Saving structured data with Jason
Flow Control
Tools:
While
For
If
Others:
Break statement
Continue statement
Else clause
Pass statement
Range() function
Looping techniques
https://docs.python.org/2/tutorial/datastructures.html
Some useful functions:
o Enumerate()
o Zip()
o Reversed()
o Sorted()
o Iteritems()
Conditions
Used in while or if or …
https://docs.python.org/2/tutorial/datastructures.html
Ending a program early with sys.exit() (3,58)
If syntax (conditional statement)
if ...:
elif ...:
else:
For (iterative loop)
For syntax:
for "individual_item" in "iterator":
"statements"
individual_item: this is just a variable
iterator: can be lists, tuples, dictionaries, and range sequences
For illustration on how to use "for" for each data type, see the corresponding data type section
while (conditional loop)
while loop syntax
while "expression":
"statements"
Functions
(3, 61)
Some points:
A func name can be re-assigned another name
With or without return statement (None value (3, 65))
Function’s arguments
def add(num1, num2):
result = num1 + num2
return result
Classes
https://docs.python.org/2/tutorial/classes.html
Check the file “class.py”
Main points:
__init__():
o Called automatically every time the class is being used
Properties
o Can be modified
o Can be deleted (“del” keyword)
Methods:
o Are functions that belong to the object
The word “self”
o Is a reference to the current instance of the class.
o Can be named anything (doesn’t have to be named “self”), but it has to be the first
parameter of any function
An object can be deleted with the “del” keyword
Create an empty class with the pass statement
Rest API
Also check the title “Lib/urllib”
Library
Lib/json
Conversion table:
Json.loads
Loads stands for “load string”
Translate a string containing JSON data into a Python dictionary
Json.dumps
Dumps stands for “dump string”
Translate a Python value into a string of JSON-formatted data
Lib/xml/dom/minidom
Many Python approaches to parse an XML payload:
MiniDom
ElementTree
Python/cpython/Lib/urllib
(also check the title “Rest API”)
Check the example “HTTP Rest API.py”
Error “urllib.error.HTTPError: HTTP Error 403: Forbidden”
This is probably because of mod_security or some similar server security feature which blocks known
spider/bot user agents (urllib uses something like python urllib/3.3.0, it's easily detected). Try setting a
known browser user agent with:
req = Request(url)
req.add_header('User-Agent', 'Mozilla/5.0')
or combine into one statement:
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
urllib3/urllib3/src/urllib3
psf/requests
https://github.com/psf/requests
check the example file “psf-requests.py”
requests is built on top of urllib3
Errors and Exceptions
(3,72)
https://docs.python.org/2/tutorial/errors.html
Regular Expression
(3,150)
Passing raw string to re.compile() (3,151)
“r” stands for “raw string”
Also check “string” under “variable” section
Grouping with parentheses (3,152)
How to match parenthesis (escape the character)
Difference between
o x.group() – x.groups()
o x.group(0) – x.group()
Matching multiple groups (3,153) (this one or that one)
Optional matching with question mark
Matching zero or more with the star
Matching one or more with the plus
Greedy and non-greedy matching
The findall() method
Debugging
When an error occurs, it will be displayed to the user in the form of a "stack trace"
If your script called a main() function, which in turn called a create_fortune_cookie_message() function,
which raised or generated an error... Your "call stack" would be:
Debugging your code
use print statements
o print("DEBUG: a =", a)
o print("==> Starting my_function()")
o “DEBUG” or “==>” are visual prefixes to visually highlight debugging statements
Use Python interactive shell
o $ python -i <script_name.py>
REFERENCES
1. https://www.tutorialspoint.com/python/
2. https://docs.python.org/2/tutorial/
3. Automate the boring stuff with Python
4. Python Programming for Beginners
5. https://docs.python.org/3/contents.html