Logo
Pattern

Discover published sets by community

Explore tens of thousands of sets crafted by our community.

Python Basics

50

Flashcards

0/50

Still learning
StarStarStarStar

How do you create a variable 'x' with the value 5?

StarStarStarStar

x = 5

StarStarStarStar

How do you handle exceptions in Python?

StarStarStarStar

try: # code that might throw an exception except ExceptionType: # code to handle the exception

StarStarStarStar

What is the correct syntax to import a module named 'math'?

StarStarStarStar

import math

StarStarStarStar

What syntax is used to specify default parameter values for a function?

StarStarStarStar

def my_function(param1=default_value):

StarStarStarStar

How do you get the length of a list or string 'my_list'?

StarStarStarStar

len(my_list)

StarStarStarStar

What is the output of the expression 'not False' in Python?

StarStarStarStar

True

StarStarStarStar

How would you read a file named 'myfile.txt' in Python?

StarStarStarStar

with open('myfile.txt', 'r') as file: file_content = file.read()

StarStarStarStar

How would you remove and return the last item from a list called 'my_list'?

StarStarStarStar

my_list.pop()

StarStarStarStar

How do you create an empty set in Python?

StarStarStarStar

set()

StarStarStarStar

What is the result of the 'in' operator in the expression '5 in [1, 2, 3]'?

StarStarStarStar

False

StarStarStarStar

How do you create a list containing the numbers 1, 2, 3?

StarStarStarStar

[1, 2, 3]

StarStarStarStar

How do you access the first element of a list 'my_list'?

StarStarStarStar

my_list[0]

StarStarStarStar

What does the 'pass' statement do in Python?

StarStarStarStar

It does nothing; it's a null statement.

StarStarStarStar

What is the output of '5 // 2' in Python?

StarStarStarStar

2

StarStarStarStar

How do you create an if statement in Python?

StarStarStarStar

if condition: # code to execute

StarStarStarStar

How would you append an element to a list named 'my_list'?

StarStarStarStar

my_list.append(element)

StarStarStarStar

How do you perform exponentiation, like raising 2 to the power of 3, in Python?

StarStarStarStar

2 ** 3

StarStarStarStar

How do you declare a global variable inside a function?

StarStarStarStar

global variable_name

StarStarStarStar

How do you check the type of a variable 'x' in Python?

StarStarStarStar

type(x)

StarStarStarStar

How do you capture both keys and values when iterating over a dictionary named 'my_dict'?

StarStarStarStar

for key, value in my_dict.items():

StarStarStarStar

How do you create a dictionary with keys 'a', 'b', and 'c'?

StarStarStarStar

{'a': value1, 'b': value2, 'c': value3}

StarStarStarStar

How would you concatenate two strings, 'Hello' and 'World'?

StarStarStarStar

'Hello' + 'World'

StarStarStarStar

How do you convert '101' (base 2) to an integer in Python?

StarStarStarStar

int('101', 2)

StarStarStarStar

How do you check if 'x' is not equal to 'y' in Python?

StarStarStarStar

x != y

StarStarStarStar

How do you slice the list 'my_list' to get only the first three elements?

StarStarStarStar

my_list[0:3]

StarStarStarStar

How do you check if 'my_dict' is a dictionary?

StarStarStarStar

isinstance(my_dict, dict)

StarStarStarStar

How do you group several statements into a block in Python?

StarStarStarStar

Indentation

StarStarStarStar

How do you print 'Hello World' in Python?

StarStarStarStar

print('Hello World')

StarStarStarStar

What is the syntax to raise an exception of type 'ValueError' in Python?

StarStarStarStar

raise ValueError('An appropriate error message')

StarStarStarStar

How do you get a list of keys from a dictionary named 'my_dict'?

StarStarStarStar

list(my_dict.keys())

StarStarStarStar

What is the correct syntax for a while loop in Python?

StarStarStarStar

while condition: # code to execute

StarStarStarStar

How do you convert the integer 5 to a string in Python?

StarStarStarStar

str(5)

StarStarStarStar

How do you create a tuple with the values 'a', 'b', and 'c'?

StarStarStarStar

('a', 'b', 'c')

StarStarStarStar

How do you make a multi-line string in Python?

StarStarStarStar

"""This is a multi-line string"""

StarStarStarStar

How do you write a comment in Python?

StarStarStarStar

# This is a comment

StarStarStarStar

What is the correct way to create a function in Python?

StarStarStarStar

def function_name():

StarStarStarStar

How do you iterate over a list [1, 2, 3] using a 'for' loop?

StarStarStarStar

for item in [1, 2, 3]: # do something with item

StarStarStarStar

How can you sort a list 'my_list' in descending order?

StarStarStarStar

my_list.sort(reverse=True)

StarStarStarStar

How do you check if 'a' is equal to 'b'?

StarStarStarStar

a == b

StarStarStarStar

How do you check if a variable 'x' is an instance of a class 'MyClass'?

StarStarStarStar

isinstance(x, MyClass)

StarStarStarStar

How do you write a single line 'for' loop in Python?

StarStarStarStar

[expression for item in iterable]

StarStarStarStar

What is the syntax to create an integer variable 'x' with value zero using binary literals?

StarStarStarStar

x = 0b0

StarStarStarStar

How do you exit a function early without returning any value?

StarStarStarStar

return

StarStarStarStar

How would you change the 'name' attribute of an instance 'my_instance' to 'Alice'?

StarStarStarStar

my_instance.name = 'Alice'

StarStarStarStar

What is the syntax to define a class named 'MyClass'?

StarStarStarStar

class MyClass: # class body

StarStarStarStar

How would you split the string 'a,b,c' by the commas?

StarStarStarStar

'a,b,c'.split(',')

StarStarStarStar

How would you reverse a list 'my_list'?

StarStarStarStar

my_list.reverse()

StarStarStarStar

How do you instantiate an object of a class named 'MyClass'?

StarStarStarStar

my_object = MyClass()

StarStarStarStar

How do you write an anonymous function that returns the square of its argument x?

StarStarStarStar

lambda x: x ** 2

StarStarStarStar

How is an error detected during the execution of a program called?

StarStarStarStar

Exception

Know
0
Still learning
Click to flip
Know
0
Logo

© Hypatia.Tech. 2024 All rights reserved.