Python Flashcards

(17 cards)

1
Q

What is the evaluation of the following line in Python ?

6 + 8 / 2

A

10.0, not 10.

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

What is the evaluation of the following line in Python ?

3 // 2  *  4
A

4, not 4.0. The operand // tells how many times a number in another. 2 enters 1 time in 3

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

What is the output of the following Python expression?

17 % 5

A

2

5 × 3 = 15
17 − 15 = 2

So the remainder is 2, and that’s what the % operator returns.

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

What is the result and type of the following Python expression?

12 / 3

A

Result: 4.0
Type: float

Explanation:

In Python, the / operator always performs true division, which means it always returns a floating-point number, even when the division is exact.

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

When should you use single quotes '...', double quotes "...", or triple quotes """...""" in Python ?

A

Use single '...' or double "..." quotes for normal one-line strings — choose whichever avoids needing escape characters.
Use triple quotes """...""" or '''...''' for multi-line strings or docstrings.

Example:

‘Hello’ — single-line string

“He said ‘Hi’” — avoids escaping

”"”This is a multi-line string””” — spans multiple lines or serves as a docstring

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

What does the :: operand do when used with a Python list?

A

The :: is used in list slicing to specify a step (stride). It allows you to:

  • Skip elements
  • Reverse the list
  • Select every n-th element

Examples:

numbers = [0,1,2,3,4,5,6,7,8,9]

numbers[::2] # [0, 2, 4, 6, 8] → every 2nd element
numbers[::-1] # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] → reverses the list

:: is shorthand for [start:stop:step] with start and stop optional.

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

When should you use a tuple vs a list in Python?

A

List: use when you need a mutable sequence (can change elements).
**Tuple: **use when you need an immutable sequence (fixed, cannot change).

Also

List: usually for similar items (numbers, names, etc.)
Tuple: often for heterogeneous data (a record: name, age, id)

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

In Python, what does the second argument in dict.pop(key, 0) represent?

A

The value that should be returned if the key is not found in the dictionary.

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

What’s the difference between and and & in Python?

A

and → logical AND, short-circuits, safe for all truthy/falsy values.

& → bitwise AND, always evaluates both sides, may error with non-booleans.

x = 5; y = None
x > 0 and y > 0 ✅ safe
(x > 0) & (y > 0) ❌ TypeError

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

In Python, can you use AND instead of and?

A

No. Python is case-sensitive:

and → valid logical AND

AND → invalid, causes SyntaxError

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

What does True and False evaluate to in Python?

A

False — and returns True only if both operands are True.

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

What does True or False evaluate to in Python?

A

True — or returns True if at least one operand is True.

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

By default, what does for key in my_dict: iterate over in a Python dictionary?

A

The keys of the dictionary.

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

How do you iterate over only the values of a dictionary?

A

Use the .values() method:

for value in my_dict.values():
    print(value)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you iterate over both keys and values of a dictionary?

A

Use the .items() method:

for key, value in my_dict.items():
    print(key, value)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

In Python, what does mode=”at” do when opening a file with open("example.txt", mode="at")?

A

a → append: adds content to the end of the file without deleting existing content.
t → text mode: reads/writes strings (default mode).

If the file doesn’t exist, it is created automatically.

17
Q

What is the purpose of the **mode** parameter in Python’s **open()** function?

A

Determines how the file is opened: reading, writing, appending, or binary/text.

Common options:

  • ‘r’ → read (default)
  • ‘w’ → write (overwrite)
  • ‘a’ → append (add to end)
  • ‘b’ → binary mode
  • ‘t’ → text mode (default)

Can combine letters, e.g., 'at' = append in text mode.