About this note
The notes are from Zero to Mastery’s complete Python course. I didn’t take the whole thing as I already know Python, but it was a good refresher! Hence, the notes below are only to refresh my personal memory and not as comprehensive as other notes.
Introduction
What is a programming language?
flowchart LR
id1(code script) ---> id2(interpreter)
id1 ---> id3(compiler)
id2 ---> id4(computer)
id3 ---> id4
- Interpreter: Goes line by line and executes the code (python usually uses this)
- Compiler: Takes the entire file and turns it into executable machine code called binary
Python interpreter
- When most people talk about Python, they’re talking about
cpython
that translate your code script into machine executable code — ie:cpython
is a interpreter
Fundamental Data Types
int
: integerfloat
: decimal numberscomplex
: complex numbers (barely used)str
: strings (ordered sequence of characters)bool
: true (1) or false (0)list
: ordered sequence of objects (likearrays
in other languages)None
: absence of values (likeNULL
in other languages)dict
: unordered key value pairtuple
: immutable listsset
: unordered collection of unique objects
Math function examples
**
: power of//
: integer division%
: modulo (remainder)round(number)
: rounds the number up or downabs(number)
: absolute value of the number
Operator precedence
()
**
*
and/
+
and-
Type conversion
int('string')
str(int)
Escape sequence
\
allows you to use special characters without breaking format\n
line break
Formatted strings
print(f'hi {name}! you are {age} years old.')
String indexing
[start:stop:stepover]
- Start: where you want the string to start (default is 0)
- Stop: where you want to stop (default is the end)
- Step-over: the size of the step (default is 1)
Negative index means that you start from the end
Immutability
- Strings are immutable
- Lists are mutable
Built in functions and methods
len()
.upper()
- Methods vs functions
- Method: owned by something
.method()
- Method: owned by something
List slicing
- Slicing a list creates a new list
new = original
will just point to the originalnew = original[:]
will copy the original list
Matrix
- Basically nested lists
List methods
list.append(object)
adds object to the endlist.insert(index, object)
adds the object at the specified indexlist.extend(list)
extends the list by adding the listlist.pop(index)
removes the object at index (default is at the end) and returns the object you removedlist.remove(object)
removes the first instance of the object and doesn’t return anythinglist.clear()
clears the list completelylist.index(object, start, end)
returns the index of the object if it is within the range that’s specified (default range is the entire list if no start and end is specified)i in 'hi my name is meg
will return True becausein
is a special word
list.count(object)
count how many instances of object is in the listlist.sort()
sorts in place ‘alphabetically’sorted(list)
sorts the list and returns a new listlist.reverse
reverses the list in place (note that it doesn’t sort it)
List tricks
List unpacking
Dictionary
Dictionary keys
- Dictionary key has to be immutable and unique
- Usually a string
- If there’s a repeat, you’re replacing the old key with the new one
dictionary.get(key)
checks if the key even exists and if it exists then it returns its value
Sets
- Basically can do the mathematical operations
set1.difference(set2)
tells you the difference between set1 and set2 areset1.discard(value)
discards the value from set1set1.difference_update(set2)
removes the difference from set1set1.intersection(set2)
what’s the intersectionset1.isdisjoint(set2)
checks if sets are disjointset1.issubset(set2)
checks if set1 is a subset of set2set1.issuperset(set2)
checks superset (superset is the opposite of subset)set1.union(set2)
combines the two sets
Vocabulary
- Variables: store data in different variables
- Rules:
- snake_case
- start with lowercase or underscore
- letters, numbers, underscores
- case sensitive
- can’t overwrite key words
- Constants are usually written in all caps
__
variables should be left alone
- Rules:
- Expression: Piece of code that produces a value
- Statement: Entire line of code that performs some kind of action
- Augmented assignment operator: instead of writing out
x = x + 2
you can write in short hand asx += 2
Conditional logic
Truthy vs Falsey
- Python evaluates everything to be booleans when comparing them so even though strings aren’t booleans, it is converted to be booleans behind the scenes
- Falsey would be
0
or''
- Truthy would be like
'hello'
- Falsey would be
Ternary operator
- AKA Conditional expression
- Shorthand:
condition_if_true if condition else condition_if_false
Short circuiting
- when using
or
it only evaluates the first condition if it’s true
Loops
Random Notes
- Tuple vs Lists
- Tuples are immutable while lists are mutable
Exercises
Age guessing game
- Note:
input()
is always a string
Password checker
Logical operators
is vs ==
is
checks the location of the item whereas equality checks just the values