1/79
A comprehensive set of flashcards covering key Python programming concepts, syntax, and error handling.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
What data type does the variable 'name' represent in Python?
String (str)
How do you convert a string '42' to an integer in Python?
Use int(x) where x is the string.
What is the result of bool(0) in Python?
False
How do you add an element to a list in Python?
Use the append method: list.append(element)
What is the order of operations precedence for the exponentiation operator?
It has the highest precedence.
What does the statement 'nums.sort()' do?
It sorts the list 'nums' in place.
What will the expression 2 ** 3 return?
8 (exponentiation result)
What operator is used for floor division in Python?
// (double slashes)
What is the last character of the string 'Python'?
The last character is 'n'.
In Python, what does the keyword 'None' represent?
It represents the absence of a value.
What is the default return value of a function in Python if no return statement is provided?
None
What does the 'break' statement do in a loop?
It exits the loop immediately.
How do you read an entire file as a single string in Python?
Use f.read() after opening the file.
What is the purpose of the 'with' statement when handling files in Python?
It ensures that the file is properly closed after its suite finishes.
What does the method f.write() do?
It writes a string to the opened file.
What is the output of print(f'The value of x is {x}')?
It interpolates the value of x within the f-string.
What error is raised when trying to access an out-of-range index of a list?
IndexError
What is the difference between '==' and 'is' in Python?
'==' checks value equality, while 'is' checks identity (same object).
What keyword is used to define a function in Python?
def
How can you skip the current iteration of a loop?
Use the 'continue' statement.
What module is used for unit testing in Python?
unittest
How do you handle exceptions in Python?
Use try and except blocks.
What type of error is raised when you try to divide by zero?
ZeroDivisionError
Which Python statement allows you to handle multiple exceptions?
You can use multiple except blocks after a single try block.
How do you check if a file exists in the filesystem?
Use os.path.exists('filename').
What does the 'import math' statement do?
It imports the math module, which provides mathematical functions.
Which operator is used for modulo operation to find the remainder?
% (percent sign).
What does the function math.sqrt(x) return?
The square root of x.
How would you format a string with replacement fields in Python?
Use str.format() or f-strings.
What function would you use to generate a random integer between 1 and 10?
random.randint(1, 10)
How can you append to a file using Python?
Use 'open(filename, 'a')' to open a file in append mode.
What is the use of the 'pass' statement?
It acts as a placeholder for future code.
How do you start a for loop that iterates over a list in Python?
Use for item in list: syntax.
How do you open a file for reading?
Use open('filename', 'r').
What method is used to strip newline characters from strings?
strip() method.
What does 'math.factorial(n)' compute?
The factorial of n.
What is the difference between a tuple and a list in Python?
Tuples are immutable; lists are mutable.
How do you define a class in Python?
Use the class keyword, e.g., class MyClass:.
What is the purpose of docstrings in Python?
To document code by describing functions or classes.
What happens if you miss a colon at the end of a function definition?
A SyntaxError will occur.
How do you check if a variable is an instance of a particular type?
Use isinstance(variable, type).
What is the output of print('Hello, World!')?
It prints 'Hello, World!' to the console.
What does the math.ceil(x) function return?
The smallest integer greater than or equal to x.
How can you create a random float in the range 0.0 to 1.0?
Use random.random().
What does the os.listdir('.') function do?
Lists all files in the current directory.
How do you specify a default value for a function parameter?
By assigning a value in the function definition: def func(param=default).
What function would you use to generate a random sample of elements from a list?
random.sample() method.
What does the term 'mutable' mean in Python?
An object whose content can be changed.
How do you create a loop that continues indefinitely?
Use a while True: loop.
What is the output of the expression 10 % 3?
1 (the remainder of the division).
How do you comment a single line in Python?
Use the '#' symbol.
What does the write() method do when used on an opened file object?
It writes the specified string to the file.
How do you check if a variable is None?
Use the 'is' operator: variable is None.
What does 'sys.argv' contain?
A list of command-line arguments passed to a Python script.
What is the purpose of the 'finally' clause in try-except blocks?
It is executed regardless of whether an exception occurred.
What is an AssertionError in Python?
It is raised when an assert statement fails.
How do you generate a random number from a given range?
Use random.randrange(start, stop, step).
What is a dictionary in Python?
A collection of key-value pairs.
How do you define a method within a class?
By defining it with 'def' inside the class.
What does math.frexp(x) return?
It returns the mantissa and exponent of x.
How are parameters specified in a function definition?
In parentheses following the function name.
What is a lambda function?
An anonymous function defined with the 'lambda' keyword.
What exception does accessing an undefined variable raise?
NameError.
What function is used to read a file line by line?
Read in a loop or use f.readlines() for all lines.
What is 'nested loops'?
A loop inside another loop.
What is the output of print(0 == False)?
True, since 0 is considered falsy.
What is the result of 5 * '2' in Python?
TypeError, cannot multiply a string by an integer.
What does 'math.isnan()' check for?
It checks if a value is NaN (Not a Number).
How do you remove an item from a list?
Use list.remove(item).
What is the symbol for exponentiation in Python?
** (double asterisks).
How can you check if an exception is raised?
Use a try-except block.
What results in a LogicError?
When the code runs but produces incorrect results.
How do you use loops to repeat code a certain number of times?
Use for or while loops.
How do you raise an exception in Python?
Use the 'raise' keyword.
What does a method in Python return if no return statement is present?
None.
What does 'with open(filename, 'r')' achieve?
It opens a file for reading in a context manager.
What is the function of the 'continue' statement?
It skips the current iteration and moves to the next.
What is the effect of a SyntaxError?
It prevents code from running due to incorrect syntax.
How do you get the length of a list in Python?
Use len(list).
What will 'name = input()' always return?
A string, regardless of input.