Misc. Flashcards

1
Q

//

What does the above do

A

Floor Division (returns a whole number rounded down)

The whole number could be an int or a float

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

What’s the short-command for quick-save

A

Ctrl + s

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

What’s the best case to use when naming .py files

A

lower-case letters with underscores

E.g.,

python_app.py

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

What is the best naming convention when defining a constant

A

ALL_CAPITALS_WITH_UNDERSCORES

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

How do you indent multiple lines at the same time

A

Highlight all the lines then hit tab

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

When importing functions from modules, you will generally need to use dot notation, e.g.,

import processor
print(processor.function_1)

How can you import functions and NOT have to use dot notation

A

from processor import function_1

print(function_1)

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

Short command for redo

A

Ctrl + Shift + Z

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

are “num // num” & “math.floor(num / num)” identical

A

Both divide & round down, but…

“num // num” will return either an int or a float, but “ “math.floor(num / num)” always returns an int

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

“or” returns True if either condition is true. But what if both conditions are true

A

“or” will also return True

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

What does input() do

A

It pauses the program and waits for the user to type something, then returns that input as a string.

E.g.,
name = input(“What is your name? “)
print(“Hello, “ + name)

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

What is the difference between “continue”, “pass”, and “break”

A

break: Exits the loop completely

continue: Skips the rest of the current loop iteration and moves to the next one

pass: Does nothing — it merely avoids syntax errors

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

Give positive infinity

A

highest = float(‘inf’)

or

import math
highest = math.inf

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

Give negative infinity

A

lowest = float(‘-inf’)

or

import math
lowest = -math.inf

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

What does abs() do

A

abs() removes the sign of the number - always making it positive (or zero)

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

lst = [1, 2, 3]

How would the two operations below affect “lst”?

lst.append([4, 5])

lst.extend([4, 5])

A

append: [1, 2, 3, [4, 5]]

extend: [1, 2, 3, 4, 5]

append() adds one thing to the end of a List.

extend() adds each item from another iterable one by one

Note that append() results in a List within a List, but extend() does not.

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

lst = [1, 2, 3]

How would the two operations below affect “lst”?

lst.append([4])

lst.extend([4])

A

append: [1, 2, 3, [4]]

extend: [1, 2, 3, 4]

Append() results in a List within a List, but extend() does notv

17
Q

lst = [1, 2, 3]

1) Can both of the below execute?
2) If so, or if not, why?
3) What result do they individually produce?

lst.append(4)

lst.append([4])

A

1) Yes, both can execute.
2) append() always accepts one thing — no matter what that one thing is. It doesn’t open or unpack that thing.
3) lst.append(4): [1, 2, 3, 4]
lst.append([4]): [1, 2, 3, [4]]

18
Q

lst = [1, 2, 3]

1) Can both of the below execute?
2) If so, or if not, why?
3) What result do they individually produce?

lst.extend(4)

lst.extend([4])

A

1) Only lst.execute([4]) can execute.
2) extend() requires an iterable.
3) lst.extend(4): TypeError
lst.extend([4]): [1, 2, 3, 4]

19
Q

lst = [1, 2, 3]
lst.extend((4))
print(lst)

.extend() can accept any iterable (including Tuples). However, when “lst” is printed, it returns an Error.
1) Why?
2) How can lst.extend((4)) be fixed?

A

1) Python does not recognise “4” as a Tuple just because it’s in parentheses - “4” also requires a comma after it.

2) lst.extend((4,))

20
Q

lst = [ ]
for i in 100:
lst.append(i)

Why does the above return an Error

A

100 is not an iterable