Reading Day 3
- What is the purpose of the ‘with’ statement when opening a file in Python, and how does it help manage resources while reading and writing files?
- The with statement in Python ensures that resources are closed automatically, even if an exception occurs. This is important for files, as they can consume system resources.
with open("myfile.txt") as f:
contents = f.read()
- Explain the difference between the ‘read()’ and ‘readline()’ methods for file objects in Python. Provide examples of when to use each method.
- The read() method reads the entire contents of a file into a string, while the readline() method reads a single line from a file.
- The read() method is useful when you need to read the entire contents of a file into a variable. For example, you might use the read() method to read a configuration file or a log file.
- The readline() method is useful when you need to read the contents of a file line by line. For example, you might use the readline() method to read a text file or a CSV file.
- Briefly describe the concept of exception handling in Python. How can the ‘try’, ‘except’, and ‘finally’ blocks be used to handle exceptions and ensure proper execution of code? Provide a simple example.
- Exception handling in Python is a way to handle unexpected errors that may occur during the execution of a program. The try block is used to define the code that may raise an exception, the except block is used to handle the exception, and the finally block is used to execute code regardless of whether or not an exception occurs.
overAPI.com
Class Notes from lecture
Given a string, write a function that will return a new string with only 1 of each character in the new sting.
Commissioner > comisner
def remove_char(strng):
new_string = ‘’
for char in strng:
if char not in new_string:
new_string += char
print(char)
word = “commissioner”
print(word[3])
print(remove_char(word))
print(remove_char(‘agressiveness’))
for num in my_list:
print(num)
my_list[0] = 100
for num in my_list:
print(num)
tuple
my_tuple = (1, 2, 3, 4)
for num in my_tuple:
print(num)
reassign tuple like my_tuple = (100, 200, 3, 4)
Need to make a change to an immutable tuple?
my_tuple = list(my_tuple)
print(my_tuple)
my_tuple[0] = 100
print(my_tuple)
my_tuple = tuple(my_tuple)
my_new_tuple = (1)
print(my_new_tuple) wil print without the parens…YOU MUST ADD A TRAILING COMMA!!! my_new_tuple = (1, )
word = "comissioner"
my_set = set(word)
print(my_set)
solving todays problem: turn it into a set, then loop throug it and concatenate, or do a join on a set and a list
try:
print(“Divide by zero again”, 1/ “spam”)
except:
print(“Don’t divide by zero silly.”)
print(“Total Lie! The Problem was not dividing by zero. It was a type error”)
try:
spam = “nonsense” / 42
except ZeroDivisionError:
print(“Don’t divide by zero silly.”)
except Exception as e:
print(e)
print(“So sorry end user. Something broke!”)
print(“Attempting to create message”)
try:
message = “nothing “ + “wrong “ + “here”
except TypeError:
print(“Unable to create message”)
except ZeroDivisionError:
print(“Cannot divide by 0”)
finally:
print(“Message successfully created”)