Misc. Flashcards
//
What does the above do
Floor Division (returns a whole number rounded down)
The whole number could be an int or a float
What’s the short-command for quick-save
Ctrl + s
What’s the best case to use when naming .py files
lower-case letters with underscores
E.g.,
python_app.py
What is the best naming convention when defining a constant
ALL_CAPITALS_WITH_UNDERSCORES
How do you indent multiple lines at the same time
Highlight all the lines then hit tab
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
from processor import function_1
print(function_1)
Short command for redo
Ctrl + Shift + Z
are “num // num” & “math.floor(num / num)” identical
Both divide & round down, but…
“num // num” will return either an int or a float, but “ “math.floor(num / num)” always returns an int
“or” returns True if either condition is true. But what if both conditions are true
“or” will also return True
What does input() do
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)
What is the difference between “continue”, “pass”, and “break”
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
Give positive infinity
highest = float(‘inf’)
or
import math
highest = math.inf
Give negative infinity
lowest = float(‘-inf’)
or
import math
lowest = -math.inf
What does abs() do
abs() removes the sign of the number - always making it positive (or zero)
lst = [1, 2, 3]
How would the two operations below affect “lst”?
lst.append([4, 5])
lst.extend([4, 5])
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.
lst = [1, 2, 3]
How would the two operations below affect “lst”?
lst.append([4])
lst.extend([4])
append: [1, 2, 3, [4]]
extend: [1, 2, 3, 4]
Append() results in a List within a List, but extend() does notv
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])
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]]
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])
1) Only lst.execute([4]) can execute.
2) extend() requires an iterable.
3) lst.extend(4): TypeError
lst.extend([4]): [1, 2, 3, 4]
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?
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,))
lst = [ ]
for i in 100:
lst.append(i)
Why does the above return an Error
100 is not an iterable