computer science coding Flashcards

1
Q

What is a variable in Python?

A

A variable stores data that can be used and changed in a program. Example: x = 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you create a list in Python?

A

Use square brackets. Example: my_list = [1, 2, 3, 4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a function in Python?

A

A function is a block of code that performs a specific task. Example:
python
CopyEdit
def greet():
print(“Hello!”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you write a loop that counts from 1 to 5?

A

python
CopyEdit
for i in range(1, 6):
print(i)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an if statement used for in Python?

A

To make decisions in code. Example:
python
CopyEdit
if x > 10:
print(“x is greater than 10”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you write a comment in Python?

A

Use the # symbol. Example: # This is a comment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a dictionary in Python?

A

A collection of key-value pairs. Example:
python
CopyEdit
my_dict = {“name”: “Aarav”, “age”: 16}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the input() function do?

A

It gets user input as a string. Example:
python
CopyEdit
name = input(“Enter your name: “)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you handle errors in Python?

A

Use try and except blocks. Example:
python
CopyEdit
try:
print(10 / 0)
except ZeroDivisionError:
print(“Cannot divide by zero!”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the len() function do?

A

It returns the length of a list, string, or other iterable. Example:
python
CopyEdit
len(“Hello”) # Output: 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you open and read a file in Python?

A

python
CopyEdit
with open(“file.txt”, “r”) as file:
content = file.read()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a loop, and what types are there in Python?

A

Loops repeat code. Python has for loops and while loops.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you define a class in Python?

A

python
CopyEdit
class Student:
def __init__(self, name):
self.name = name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly