Explore tens of thousands of sets crafted by our community.
Python Basics
50
Flashcards
0/50
How do you create a variable 'x' with the value 5?
x = 5
How do you handle exceptions in Python?
try: # code that might throw an exception except ExceptionType: # code to handle the exception
What is the correct syntax to import a module named 'math'?
import math
What syntax is used to specify default parameter values for a function?
def my_function(param1=default_value):
How do you get the length of a list or string 'my_list'?
len(my_list)
What is the output of the expression 'not False' in Python?
True
How would you read a file named 'myfile.txt' in Python?
with open('myfile.txt', 'r') as file: file_content = file.read()
How would you remove and return the last item from a list called 'my_list'?
my_list.pop()
How do you create an empty set in Python?
set()
What is the result of the 'in' operator in the expression '5 in [1, 2, 3]'?
False
How do you create a list containing the numbers 1, 2, 3?
[1, 2, 3]
How do you access the first element of a list 'my_list'?
my_list[0]
What does the 'pass' statement do in Python?
It does nothing; it's a null statement.
What is the output of '5 // 2' in Python?
2
How do you create an if statement in Python?
if condition: # code to execute
How would you append an element to a list named 'my_list'?
my_list.append(element)
How do you perform exponentiation, like raising 2 to the power of 3, in Python?
2 ** 3
How do you declare a global variable inside a function?
global variable_name
How do you check the type of a variable 'x' in Python?
type(x)
How do you capture both keys and values when iterating over a dictionary named 'my_dict'?
for key, value in my_dict.items():
How do you create a dictionary with keys 'a', 'b', and 'c'?
{'a': value1, 'b': value2, 'c': value3}
How would you concatenate two strings, 'Hello' and 'World'?
'Hello' + 'World'
How do you convert '101' (base 2) to an integer in Python?
int('101', 2)
How do you check if 'x' is not equal to 'y' in Python?
x != y
How do you slice the list 'my_list' to get only the first three elements?
my_list[0:3]
How do you check if 'my_dict' is a dictionary?
isinstance(my_dict, dict)
How do you group several statements into a block in Python?
Indentation
How do you print 'Hello World' in Python?
print('Hello World')
What is the syntax to raise an exception of type 'ValueError' in Python?
raise ValueError('An appropriate error message')
How do you get a list of keys from a dictionary named 'my_dict'?
list(my_dict.keys())
What is the correct syntax for a while loop in Python?
while condition: # code to execute
How do you convert the integer 5 to a string in Python?
str(5)
How do you create a tuple with the values 'a', 'b', and 'c'?
('a', 'b', 'c')
How do you make a multi-line string in Python?
"""This is a multi-line string"""
How do you write a comment in Python?
# This is a comment
What is the correct way to create a function in Python?
def function_name():
How do you iterate over a list [1, 2, 3] using a 'for' loop?
for item in [1, 2, 3]: # do something with item
How can you sort a list 'my_list' in descending order?
my_list.sort(reverse=True)
How do you check if 'a' is equal to 'b'?
a == b
How do you check if a variable 'x' is an instance of a class 'MyClass'?
isinstance(x, MyClass)
How do you write a single line 'for' loop in Python?
[expression for item in iterable]
What is the syntax to create an integer variable 'x' with value zero using binary literals?
x = 0b0
How do you exit a function early without returning any value?
return
How would you change the 'name' attribute of an instance 'my_instance' to 'Alice'?
my_instance.name = 'Alice'
What is the syntax to define a class named 'MyClass'?
class MyClass: # class body
How would you split the string 'a,b,c' by the commas?
'a,b,c'.split(',')
How would you reverse a list 'my_list'?
my_list.reverse()
How do you instantiate an object of a class named 'MyClass'?
my_object = MyClass()
How do you write an anonymous function that returns the square of its argument x?
lambda x: x ** 2
How is an error detected during the execution of a program called?
Exception
© Hypatia.Tech. 2024 All rights reserved.