Podcast
Questions and Answers
What is the primary purpose of the try
and except
blocks in Python when handling errors?
What is the primary purpose of the try
and except
blocks in Python when handling errors?
- To automatically fix any errors that occur during program execution.
- To force the program to terminate immediately upon encountering any error.
- To handle anticipated errors gracefully without crashing the program. (correct)
- To prevent errors from occurring in the first place.
In Python, what is the correct way to open a file named data.txt
in read mode and assign it to a file object?
In Python, what is the correct way to open a file named data.txt
in read mode and assign it to a file object?
- `with open("data.txt", "r") as file:` (correct)
- `open("data.txt", "read") as file`
- `file = open("data.txt", mode='read')`
- `file = open("data.txt", "w+")`
Given the list fruits = ["apple", "banana", "cherry"]
, how would you access the first element ("apple"
) of the list?
Given the list fruits = ["apple", "banana", "cherry"]
, how would you access the first element ("apple"
) of the list?
- `fruits.first()`
- `fruits["first"]`
- `fruits[0]` (correct)
- `fruits[1]`
What is the result of the following Python code?
x = 7
if x > 10:
print("x is greater than 10")
elif x == 7:
print("x is exactly 7")
else:
print("x is less than 10")
What is the result of the following Python code?
x = 7
if x > 10:
print("x is greater than 10")
elif x == 7:
print("x is exactly 7")
else:
print("x is less than 10")
Which of the following best describes the use of a for
loop with range()
in Python?
Which of the following best describes the use of a for
loop with range()
in Python?
Given the following code snippet, what will be the output?
count = 0
while count < 3:
print("Count:", count)
count += 1
Given the following code snippet, what will be the output?
count = 0
while count < 3:
print("Count:", count)
count += 1
In the context of file handling in Python, what does the w
mode signify when opening a file?
In the context of file handling in Python, what does the w
mode signify when opening a file?
What is the purpose of the def
keyword in Python?
What is the purpose of the def
keyword in Python?
Consider the following Python dictionary: student = {"name": "Caren", "age": 22, "major": "MIS"}
. How would you correctly access the value associated with the key "age"
?
Consider the following Python dictionary: student = {"name": "Caren", "age": 22, "major": "MIS"}
. How would you correctly access the value associated with the key "age"
?
What is the output of this code?
def greet(name):
print("Hello, " + name + "!")
greet("World")
What is the output of this code?
def greet(name):
print("Hello, " + name + "!")
greet("World")
Flashcards
What is a variable in Python?
What is a variable in Python?
A named storage location that can hold a value (e.g., name = "Caren").
What are If-Else Statements?
What are If-Else Statements?
Statements that execute different code blocks based on whether a condition is true or false.
What is a 'while' loop?
What is a 'while' loop?
A control flow statement that repeatedly executes a block of code as long as a condition is true.
What is a Function?
What is a Function?
Signup and view all the flashcards
What is a List?
What is a List?
Signup and view all the flashcards
What is a Dictionary?
What is a Dictionary?
Signup and view all the flashcards
What is File Handling?
What is File Handling?
Signup and view all the flashcards
What is Error Handling (Try-Except)?
What is Error Handling (Try-Except)?
Signup and view all the flashcards
Study Notes
- Python displays text using the
print()
function, such asPrint(“Hello, World!”)
. - Variables are used to store information; for example,
name = "Caren"
stores the name "Caren". - Numerical data can be stored as integers, like
age = 22
, or as decimal numbers, likeprice = 19.99
. - User input can be obtained using the
input()
function, as inuser_input = input("Enter something: ")
.
If-Else Statements
- Python uses
if
,elif
(else if), andelse
to make decisions based on conditions. - Example:
x = 10
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is exactly 5")
else:
print("x is less than 5")
Loops
- Loops are used to repeat actions.
- The
for
loop can iterate over a range of numbers:
for i in range(1, 6):
print(i)
- The
while
loop continues as long as a condition is true:
count = 0
while count < 5:
print("Count:", count)
count += 1
Functions
- Functions are blocks of code that can be called to perform specific tasks.
- Defined using the
def
keyword:
def greet(name):
print("Hello, " + name + "!")
- Functions are then called by name; for example,
greet("Caren")
.
Lists and Dictionaries
- Lists store ordered collections of items:
fruits = ["apple", "banana", "cherry"]
. - Accessing list elements, for example,
print(fruits[0])
outputs "apple". - Dictionaries store key-value pairs:
student = {"name": "Caren", "age": 22, "major": "MIS"}
. - Accessing dictionary values, for example,
print(student["name"])
outputs "Caren".
File Handling
- Files can be opened in write mode ("w") to save data:
with open("test.txt", "w") as file:
file.write("Hello, this is a test file!")
- Files can be opened in read mode ("r") to retrieve data:
with open("test.txt", "r") as file:
content = file.read()
print(content)
Handling Errors
try
andexcept
blocks catch errors without crashing the program:
try:
num = int(input("Enter a number: "))
print(10 / num)
except ZeroDivisionError:
print("You can't divide by zero!")
except ValueError:
print("Invalid input! Please enter a number.")
Overtime Pay Program
- The program calculates weekly pay based on hours worked:
def payFunction(hours):
payrate = 20.756
if hours > 40:
extraHours = hours - 40
pay = payrate * 40 + extraHours * payrate * 1.5
else:
pay = payrate * hours
print("Your weekly pay is", round(pay, 2))
hours = float(input("Please enter total hours worked: "))
if hours < 0 or hours > 80:
print("Invalid input. Hours should be between 0-80")
else:
payFunction(hours)
- It asks for hours worked, adds extra pay for more than 40 hours (1.5x rate), and calculates regular pay if less than 40 hours.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.