Python Notes
Python Notes
Data Types
- Python is a dynamically typed language meaning that data types do not need
to be specified when declaring a variable
Ex:
Lists
Useful Functions:
.append() - adds an element to the end of the list (+ can be used instead)
..remove() - removes specified element from the list
..pop() - removes the last element and returns that value
.clear()
.insert()
Nested Lists
- Lists can be nested within each other and treated as elements
Ex: items = ["Ball", "Bat", ["Gloves", 7, "Hat"]]
- ["Gloves", 7, "Hat"] , is a list nested within items and can be referred to as
any other element
- 7 can be accessed using the following syntax
items[2][1]
- 2 is the index of the list, and 1 is the index of 7 within that list
Negative Indexes
- In python lists also have negative indexes that start at -1 at the last element
and decrement by 1 moving backwards in the list
Copying Lists
- In python, when you set a list to equal another list, you are simply creating an
alias for that list.
- Ex:
- items = ["Ball", "Bat", "Gloves", 7, "Hat"]
- copy = items
- In this case copy is simply an alias for items and any changes to copy will also
affect items
A deep copy can be made to avoid this using the following syntax:
import copy
items = ["Ball", "Bat", ["Gloves", 7, "Hat"]]
c = copy.deepcopy(items)
If Statements
If statement syntax:
if 1 + 1 == 2:
print("Hello")
elif 1 + 1 == 1:
print ("Bye")
else:
print("Something")
&& and || operators in other languages are represented by and & or respectively
Not Operator vs !=
The != operator compares only the The “is not” operator compares if the
value of the objects being objects are pointing to the same memory
It returns True if the value of both It returns true if the objects are not
the objects are different and False pointing to the same memory location
- == and != check for equivalency, while is and is not check if the objects being
compared are identical or not.
Flow Charts
Recursion
Base Cases: Cases that require no “work” to compute
Recursive Cases: Simplify - > Recursive Call - > Do Additional Work
Dictionaries
#initializing dictionary
phonetic_dictionary = {}
Multidimensional Lists:
- A list of lists
- Can be accessed using two square brackets
- First square bracket is the index of the row
- Second square bracket is the index of the column
Graph Theory:
- Tuple of two collections
- FIrst element is the thing of interest (Vertices)
- Second element is the relationships with the Vertice (Edges)