Files
- storage for data
- unique methods to access within code
Access modes:w
write: wipes the file and adds your dataa
append: appends your data to the end of the filer
read: opens the file just for reading
Text Processing
file.read()
file.readline()
file.readlines()
List Comprehension
- Fancy, shorthand method of writing for loops
- Syntax changes depending on use case
- Can be nested in each other, just like lists
- Can contain multiple for loops in one list comp
- Can also be a nested loop
Syntax[x for x in iterable]
[x for x in iterable if (condition)]
[x if (condition) else y for x in iterable]
[x if (condition) else y if (condition) else z for x in iterable]
Dictionary Comprehension
- Fancy, shorthand method of populating dicitonaries
- Syntax changes depending on use case
Syntax- basically the same as list comp, but now it expects key:value
- can include a list comp
Assert Statements
- Used to evaluate written code
- asserts → input validation (are the arguments the correct type?)
- Often combined with boolean functions (
any()
,all()
, etc.)
assert isinstance(lst, list)