1/362
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
python
not meant for low level programming
emphasizes code readability
works in any environment
lots of libraries
many frameworks
What does the .title() method do in Python?
It capitalizes the first letter of every word in the string and makes the rest of each word lowercase.
Does .title() affect letters after punctuation marks like hyphens or spaces?
Yes. It capitalizes the letter that comes right after a non letter character.
Example: what is the result of "hello-world".title()?
"Hello-World"
What is a function in Python?
A block of reusable code that performs a task and can exist independently of any class or object.
What does the bin() function do in Python?
It converts an integer to a string, representing its binary value, prefixed with 0b: 0b1010 = 10. For instance, all the places are to the left of the decimal point. 2*3 times 1, 22 times 0, 21 times 1, 2*0 times 0. You then add these up: 8+2 = 10.
Convert 0o123 into a decimal number.
8*2 = 64 times 1. 81 times 2 = 16. 8*0 times 3 equals 1 times 3 equals 3.
Convert 0x11 from hexadecimal into decimal.
16*1 times 1=16; 16*0 times 1=1; 16+1 = 17.
What is a method in Python?
A function that is defined inside a class and called an object (instance) of that class.
def swap(num1, num2):
temp = num1
num1 = num2
num2 = temp
num1 = 2
num2 = 5
swap(num1, num2)
What does swap function actually do?
It swaps the local copies of num1 and num2 inside the function, but doesn't affect the variables outside because integers are immutable and passed by value.
What is the difference between variables inside the function and outside the function?
Variables inside the function only exist in the function's local scope and disappear when it ends. Variables outside the function exist in the global scope and are not changed by what happens inside, unless explicitly returned or declared global.
What is a global variable in Python?
A variable defined outside any function or class that can be accessed throughout the entire program.
What is a local variable in Python?
A variable defined inside any function, ie when the function originally runs but not when it is called.
Do local variables become global when a function is called?
No. Local variables are created fresh each time a function runs and exist only in that function. They never replace global variables with the same name.
How is a function called?
By its name directly, e.g. print("Hi") or len("abc").
What is the structure and initial value of list in this code?
list = [[1, 2, 3]]
result = 1
list is a nested list containing one inner list: [[1, 2, 3]].So list[0] is [1, 2, 3], and list[0][0] is 1.
How many times will this loop run?
for i in range(1):
result *= 10
Only once — because range(1) produces [0]. After it runs, result becomes 10.
What happens inside the nested loop here?
for i in range(1):
result *= 10
for j in range(1):
list[i][j] *= result
Both loops run once (i = 0, j = 0).list[0][0] (which was 1) gets multiplied by 10, becoming 10.
What will this print statement output?
for i in range(1):
result *= 10
for j in range(1):
list[i][j] *= result
print(list)
[[10, 2, 3]]Only the first element changed because the loops only touched list[0][0].
Why do only the first numbers change in the inner list?
for i in range(1):
...
for j in range(1):
list[i][j] *= result
Because range(1) means only index 0 is visited.Indexes 1 and 2 (values 2 and 3) are never reached.
If we changed both ranges to range(3), what would happen?
for i in range(3):
result *= 10
for j in range(3):
list[i][j] *= result
It would cause an IndexError because list has only one inner list (i=0 is valid, i=1 and i=2 are out of range`).
How do quotes work in Python?
With print() function, Python gives the viewer a string without quotes although it represents the string to itself internally as having quotes.
What does this code do?
t = tuple('Hello World')
It creates a tuple out of a string.
What does this slicing expression mean?
t = tuple('Hello World')
print(t[:-6])
t[:-6] takes all elements from the start of the tuple up to (but not including) the element that is 6 places from the end. So it keeps everything except the last six characters.
Where does the index -6 point to in this tuple?
t = tuple('Hello World')
print(t)
-6 counts backward from the end and lands on the space ' ' after 'o'.So t[:-6] will stop just before that space, including only the first five characters.
What will Python print here?
t = tuple('Hello World')
print(t[:-6])
('H', 'e', 'l', 'l', 'o')
Why does the output have parentheses and quotes?
t = tuple('Hello World')
print(t[:-6])
Because parentheses ( ) indicate a tuple,and single quotes ' ' indicate string elements inside that tuple.
Why does print("Hello") show no quotes, but printing a list or tuple of strings shows quotes?
Because:
print("Hello") displays a human-friendly string (no quotes).
But when printing a container (list, tuple, dict), Python uses repr() on its elements — showing their internal representations (with quotes).
What does the outer loop do?
var = 0
for i in range(3):
pass
print(list(range(3)))
range(3) → [0, 1, 2] → 3 iterations of the outer loop.
What does the inner loop do?
print(list(range(-2, -7, -2)))
With a negative step, Python counts down and stops before -7:[-2, -4, -6] → 3 iterations of the inner loop.
How many times does var += 1 run?
var = 0
for i in range(3): # 3 i's: 0,1,2
for j in range(-2, -7, -2): # 3 j's: -2,-4,-6
var += 1
print(var)
Visualize the nested counting
for i in range(3):
for j in range(-2, -7, -2):
print(f"(i,j)=({i},{j})")
You’ll see 9 pairs:(0,-2) (0,-4) (0,-6) (1,-2) (1,-4) (1,-6) (2,-2) (2,-4) (2,-6) — confirming 9 inner hits.
What happens when f1() is defined?
def f1():
print("f2")
def f2():
print("f3")
def f3():
print("f1")
f3()
f2()
At this point, Python stores the function f1 in memory.No code inside it runs yet — the inner functions f2 and f3 only exist when f1() is called.
What happens when f1() runs?
def f1():
print("f2")
def f2():
print("f3")
def f3():
print("f1")
f3()
f2()
f1()
When f1() is called:
It prints "f2".
Then it defines f2() and immediately calls it.→ First output line:
f2
What happens when f2() runs?
def f1():
print("f2")
def f2():
print("f3")
def f3():
print("f1")
f3()
f2()
f1()
Inside f2():
It prints "f3".
Then it defines f3() and calls it.→ Second output line:
f3
What happens when f3() runs?
def f1():
print("f2")
def f2():
print("f3")
def f3():
print("f1")
f3()
f2()
f1()
When f3() executes, it prints "f1".→ Third output line:
f1
What is the total output of this program?
def f1():
print("f2")
def f2():
print("f3")
def f3():
print("f1")
f3()
f2()
f1()
The complete output is:
f2
f3
f1
Why is the order not f1, f2, f3?
def f1():
print("f2")
def f2():
print("f3")
def f3():
print("f1")
f3()
f2()
f1()
Because printing happens when the function is executed, not when it's defined.
Enter f1() → print "f2".
Call f2() → print "f3".
Call f3() → print "f1".
The program follows the call stack, not the definition order.
What happens when we call f(5) in this function?
def f(x):
if x <= 0:
return
print(x)
f(x - 2)
f(5)
5 is greater than 0, so this function does not return. It prints 5, then 3, then 1.
What does return mean inside a recursive function?
It stops the function and immediately goes back to the caller. In recursion, when the base case hits return, it begins unwinding all the previous calls--ending the recursion completely.
What does pop(index) do?
It deletes the element which the index specificies. Don't forget that the index starts with 0.
What does pop without an index do?
It deletes the last element in the list.
a = 1
b = 1
print(a ^ b)
0
^ is the bitwise XOR (exclusive OR) operator.
It compares bits of two integers bit-by-bit, returning 1 if exactly one of the bits is 1.
So when a = 1 and b = 1, both bits are 1 → XOR gives 0.
How is a method called?
Using dot notation on an object, e.g. "abc".upper() or my_list.append(5).
What is the main difference between a function and a method?
A function stands alone, while a method is bound to an object and automatically receives that object as its first argument.
What does the range() function do in Python?
It generates a sequence of numbers starting from a given value, up to (but not including) an end value, increasing by a step value.
In range(3, 12, 2), what does each argument mean, and what numbers would be produced?
3 is the number where you start, 12 is the number where you end (although it may not be included), and 2 is the step, so the numbers produced in this case are 3, 5, 7, 9, 11.
Why doesn't the number 12 appear in the produced range in the previous question?
Because the stop value in range() is exclusive, and unless it fits in the step, the endpoint won't be produced.
What has higher precedence in Python-- or *?
** exponentiation has higher precedence than multiplication.
How is 123163 evaluated in Python?
It's read as (((12)3)(16))3.
What is the final result of print(123163)?
18
What does operator associativity mean?
It determines the order in which operators of the same precedence are evaluated: either left to right or right to left. Exponentiation, for instance, is right to left.
Which operators are left-to-right associative in Python?
Exponentiation (**), assignment (= , += etc .), and lambda expressions.
What is a = b = c = 5 actually doing?
It's evaluated right to left. All variables become 5.
Why does Python make exponentiation right associative?
Because mathematical exponent chains like 2 * 3 2 naturally mean 2 (3 2), not (2 3) * 2.
def func(py):
return ['Python', 'Java']
Always returns ['Python', 'Java'], which is a list, regardless of the argument of func().
In Python, when performing modulus with a negative number, how is the sign of the result determined?
In Python, the modulus result always takes the sign of the divisor.
What is an escape character in Python?
It's a backslash (\) used in strings to give special meaning to the character that follows it.
What does \n mean in Python strings?
It's a newline character that tells Python to start a new line in the output.
Why does Python treat \n as one character, not two?
Because the backslash tells Python to "escape" the next character, changing its meaning to something special--in this case, starting a new line.
What does print() do when called with no arguments?
It prints a blank line (just a newline character).
How do you stop print() from adding a newline at the end?
Use the optional argument end="" inside print() to prevent the newline.
What does the sep argument do in the print() function?
It specifies the separator between multiple values printed in one print() call.
Give an example of using sep in print()
print("A", "B", "C", sep="-")
# Output: A-B-C
What happens when you call print() in Python?
By default, print() sends output to the console and adds a newline at the end--this happens whether or not you include an argument.e
What does the escape in "escape character" mean?
It means the normal string behavior is "escaped" to allow a special command or meaning to be inserted briefly.
What happens when you include \n in a string passed to print()?
The output appears on multiple lines--each\n starts a new output line.
What is string formatting in Python?
It's a way to insert variables into strings using methods like f-strings, .format() or % formatting.
What does returning a value mean?
the main purpose of the function is to calculate and give something back.
What does the f do in an f-string in Python?
The f tells Python too evaluate expressions inside curly braces {} and insert their values directly into the string. It stands for "formatted string literal."
Give an example of an f-string.
age = 7
print(f"My son is {age} years old.")
#Output: My son is 7 years old.
In programming, what is a literal like in a recipe analogy?
A literal is like writing "3 eggs" directly into the recipe--an exact, unchanging value.
In a recipe analogy, what is a variable like?
A variable is like a labeled container--reusable and changeable, like a bowl labeled 'eggs' that might hold 2 or 5 eggs at different times.
What is an expression like in a recipe analogy?
An expression is like an instruction--"crack and whisk the eggs." It does something to produce a result.
In a recipe analogy, what is a function call like?
A function call is like following a mini-recipe--"make the frosting"--that runs specific steps to produce a result you can use in the main recipe.
In a recipe analogy, what is a reference or name like?
A reference or name is like a labeled container--
sugar" or "milk"--pointing to where the ingredient is stored, not the ingredient itself.
In a recipe analogy, what is a loop like?
A loop is like saying, 'Stir 10 times' or 'Repeat this step for each egg.' It means repeating. a set of instructions a specific number of times or for each item.
In a recipe analogy, what is a conditional (if-statement) like?
A conditional is like saying, "If the sauce is too thick, add water." It lets the cook choose actions based on specific conditions.
In a recipe analogy, what is a class like?
A class is like a template for a recipe--like a master recipe for "Cake." You can make different versions (objects) like "Chocolate Cake" or "Vanilla Cake" using the same structure.
In a recipe analogy, what is an object (from a class) like?
An object is like a specific dish made from a recipe--like the chocolate cake you baked from the Cake class template.
In a recipe analogy, what are class attributes like?
class attributes are like default ingredients in the recipe (e.g., sugar=1 cup). They describe the basic setup for every version of that dish.
Mnemonic for forwardslash and backslash
Backslash leans back \ like you're reclining.
Forward slash leans forward / like you're charging ahead.
What happens if you try to print a single backslash in Python with print("\")?
It causes a syntax error because the backslash is interpreted as the start of an escape character, but it's not followed by a valid one.
How do you print a single backslash in a Python string?
Use two backslashes: print("\\"). The first backslash escapes the second,
Why do you need to double the backslash (\\) in a Python string?
Because the backslash is an escape character--doubling it prevents it from escaping and tells Python to treat it as a literal backslash.
Do all backslash-character combinations in Python strings create valid escape sequences?
No. Not all escape pairs mean something. Some combinations are invalid and will either produce an error or behave unexpectedly.
What happens if you use \c in a Python string?
\c is not a valid escape sequence in Python and will raise a syntax error.
Is \d a valid escape sequence in Python?
No. It is not defined, so Python just treats it as 'd', but it's confusing and not recommended.
What about \e in a Python string?
\e is not recognized in Python--it may work in other languages but will cause issues or warnings in Python.
Are \y and \z valid escape sequences?
No, Python does not define \y or \z, and they will just be treated as 'y' and 'z', but they can confuse readers or cause issues in strict settings.
What happens with a backslash at the end of a string? (Hello\)
It causes a SyntaxError, because Python expects another character to complete the escape sequence.
Can the print() function take more than one argument?
Yes, print() can take multiple arguments, separated by commas, and it will print them in order.
What does print("A", "B", "C") output in Python?
It prints
A B C
Each item is separated by a. space (the default separator).
What is the difference between a comma in a print() argument list and a comma inside a string?
A comma in the argument list is Python syntax for separating arguments. A comma inside a string is just text to be displayed.
Does Python insert spaces between multiple print() arguments?
Yes. By default, print() inserts a single space between each argument.
What is the "positional way" of passing arguments in Python?
It's the most common method where the order of arguments determines how they are used. The first argument is processed first, the second second, and so on.
In print("A", "B", " C"), how is the output determined?
By position: "A" is printed first, then "B", then "C" in the exact order they appear.
Why does each new print() call create a new line in the output?
By default, the print() function ends its output with a newline character (\n).
What is a keyword argument in Python?
A keyword argument is a named argument passed to a function using name=value syntax. Its meaning comes from the name (keyword), not the position.
What are the three parts of a keyword argument?
1. A keyword (e.g. end).
2. An equal sign (=)
3. A value assigned to that keyword (e.g., " ")v