Source: https://docs.google.com/presentation/d/14EF42i3A8RrRnhmocf0F5s40H7aht5Pl7UVHh3PEPzs/edit

Working with files

with open('DSC 20 Lecture 6.md', 'r') as f:  
	read_data = f.read()  
f.closed # True  

Writing to a file

  1. Open file in writing mode (‘w’ or ‘a’)
  2. Use .write method and pass a string that you want to write to a file
    Note: must cast to string when writing to file

List Comprehension, Strings

List Comprehension Came From Math

It is Python’s way of implementing a well-known notation for sets as used by mathematicians.

An elegant way to create lists in Python
Faster than a regular loop with append

Convert C to F

ctemps = [24,20,56,32,10]  
# For loop  
ftemps = []  
for c in ctemps:  
	f = C_to_F(c)  
	ftemps.append(f)  
# List comp  
[C_to_F(c) for c in ctemps]  
[str(i) + str(j) for i in range(3) for j in range(2)]  
sentence = "What did the fish say when it swam into the wall? Dam!"  
words = sentence.split()  
 
print([len(word) for word in words if word != "the"])  
[[1 if i_idx == row_idx else 0 for i_idx in range(0,3)] for row_idx in range(0,3)] # 3x3 diagonal matrix