Programming:Python Flashcards

(57 cards)

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

In Python, what is the purpose of an if statement?

A

It is used for selection, executing a block of code only if a specified condition is true.

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

What is the role of the elif keyword in a Python selection statement?

A

It allows for checking multiple alternative conditions after an initial if statement fails.

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

In a Python if...elif...else structure, when is the else block executed?

A

It is executed when all preceding if and elif conditions evaluate to false.

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

What type of loop is created using the for i in range(0,10): syntax in Python?

A

A count-controlled loop.

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

In the Python code for i in range(0,10):, how many times will the loop iterate and what are the values of i?

A

The loop will iterate 10 times, with i taking values from 0 to 9.

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

What is the purpose of the while loop in Python?

A

It creates a condition-controlled loop that repeats as long as a specified condition remains true.

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

What function is used to get input from a user in Python?

A

The input() function.

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

How do you define a function in Python?

A

Using the def keyword, followed by the function name, parentheses for parameters, and a colon.

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

What does the return statement do inside a Python function?

A

It exits the function and sends a specified value back to where the function was called.

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

The process of converting a value from one data type to another is known as _____.

A

casting

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

Which Python function converts a value to an integer data type?

A

The int() function.

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

Which Python function would you use to convert the string "3.14" into a floating-point number?

A

The float() function.

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

Which Python function converts a numeric value into a string?

A

The str() function.

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

What is the Python operator for addition?

A

The + operator.

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

What is the Python operator for subtraction?

A

The - operator.

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

What is the Python operator for multiplication?

A

The * operator.

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

What is the Python operator for division that results in a float?

A

The / operator.

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

The % operator in Python performs which arithmetic operation?

A

Modulus (finds the remainder of a division).

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

What does the ** operator do in Python?

A

It performs exponentiation (raises a number to a power).

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

Which Python operator performs integer division (quotient), discarding the remainder?

A

The // operator.

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

What statement is used to open a file in Python, for example, to write to it?

A

The with open("filename.txt", 'w') as file: statement is commonly used.

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

What does the mode 'w' signify when opening a file in Python?

A

It opens the file in write mode, creating the file if it doesn’t exist or overwriting it if it does.

24
Q

What is the purpose of opening a file with the mode 'a' in Python?

A

It opens the file in append mode, allowing content to be added to the end of the file without overwriting existing content.

25
What does the mode `'r'` signify when opening a file in Python?
It opens the file in read mode, which is the default mode.
26
Which method is used to write a string to an open file in Python?
The `.write()` method.
27
Which method is used to read the entire content of an open file into a single string?
The `.read()` method.
28
How do you define a list containing the strings "Apple", "Banana", and "Cherry" in Python?
`fruits = ["Apple", "Banana", "Cherry"]`
29
Given the list `fruits = ["Apple", "Banana", "Cherry"]`, what does `fruits[0]` return?
It returns the string "Apple".
30
How can you add a new item, for example "Orange", to the end of an existing list called `fruits`?
By using the append method: `fruits.append("Orange")`.
31
How would you remove the item "Orange" from a list named `fruits`?
By using the remove method: `fruits.remove("Orange")`.
32
Which function returns the number of items in a list?
The `len()` function.
33
In Python, what is the syntax for getting the length of a string variable named `subject`?
`len(subject)`
34
What string method converts all characters in a string to uppercase?
The `.upper()` method.
35
What string method converts all characters in a string to lowercase?
The `.lower()` method.
36
Given `subject = "ComputerScience"`, what would `subject[8:15]` return?
The substring "Science".
37
Given `subject = "ComputerScience"`, what would `subject[-3:]` return?
The substring "nce" (the last 3 characters).
38
Given `subject = "ComputerScience"`, what does the slice `subject[:3]` extract?
The first 3 characters: "Com".
39
What is the term for combining two strings together, often done using the `+` operator?
Concatenation.
40
Which module must be imported to generate random numbers in Python?
The `random` module.
41
Which function from the `random` module is used to generate a random integer within a specific inclusive range?
The `random.randint(a, b)` function.
42
What does the comparison operator `==` check for?
It checks if two values are equal to each other.
43
What is the Python comparison operator for 'not equal to'?
The `!=` operator.
44
What is the Python comparison operator for 'less than'?
The `<` operator.
45
What is the Python comparison operator for 'greater than or equal to'?
The `>=` operator.
46
What does the logical operator `and` do?
It returns `True` only if both conditions on its left and right are true.
47
When does the logical operator `or` return `True`?
It returns `True` if at least one of the conditions on its left or right is true.
48
In Python, `range(5)` generates a sequence of numbers from _____ to _____.
0 to 4
49
What sequence of numbers is generated by `range(2, 6)` in Python?
2, 3, 4, 5
50
In the function `range(0, 10, 3)`, what is the purpose of the number 3?
It is the step value, meaning the counter increases by 3 in each iteration.
51
What is a variable in programming?
A named location in memory used to store data that can be changed.
52
According to Python conventions, what is a good practice for naming variables?
Use all lowercase letters, with words separated by underscores (snake_case) or using camelCase.
53
What does it mean for variable names to be case-sensitive?
It means that `myVariable` and `myvariable` would be treated as two different variables.
54
How does Python handle constants, given it has no specific `const` keyword?
By convention, a variable intended to be a constant is named in all uppercase letters and programmers simply do not change its value.
55
What is the purpose of the `break` statement within a loop?
It immediately terminates the loop and execution continues at the first statement after the loop.
56
What does the `continue` statement do inside a loop?
It stops the current iteration and immediately jumps to the beginning of the next iteration of the loop.
57
What is the purpose of f-string formatting in Python, such as `print(f"Sum is {result}")`?
It provides a concise way to embed expressions and variable values directly inside a string literal.