Python PCEP

0.0(0)
studied byStudied by 1 person
0.0(0)
full-widthCall with Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/362

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No study sessions yet.

363 Terms

1
New cards

python

not meant for low level programming

emphasizes code readability

works in any environment

lots of libraries

many frameworks

2
New cards

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.

3
New cards

Does .title() affect letters after punctuation marks like hyphens or spaces?

Yes. It capitalizes the letter that comes right after a non letter character.

4
New cards

Example: what is the result of "hello-world".title()?

"Hello-World"

5
New cards

What is a function in Python?

A block of reusable code that performs a task and can exist independently of any class or object.

6
New cards

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.

7
New cards

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.

8
New cards

Convert 0x11 from hexadecimal into decimal.

16*1 times 1=16; 16*0 times 1=1; 16+1 = 17.

9
New cards

What is a method in Python?

A function that is defined inside a class and called an object (instance) of that class.

10
New cards

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.

11
New cards

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.

12
New cards

What is a global variable in Python?

A variable defined outside any function or class that can be accessed throughout the entire program.

13
New cards

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.

14
New cards

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.

15
New cards

How is a function called?

By its name directly, e.g. print("Hi") or len("abc").

16
New cards

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.

17
New cards

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.

18
New cards

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.

19
New cards

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].

20
New cards

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.

21
New cards

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`).

22
New cards

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.

23
New cards

What does this code do?

t = tuple('Hello World')

It creates a tuple out of a string.

24
New cards

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.

25
New cards

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.

26
New cards

What will Python print here?

t = tuple('Hello World')

print(t[:-6])

('H', 'e', 'l', 'l', 'o')

27
New cards

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.

28
New cards

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).

29
New cards

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.

30
New cards

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.

31
New cards

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)

32
New cards

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.

33
New cards

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.

34
New cards

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

35
New cards

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

36
New cards

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

37
New cards

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

38
New cards

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.

39
New cards

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.

40
New cards

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.

41
New cards

What does pop(index) do?

It deletes the element which the index specificies. Don't forget that the index starts with 0.

42
New cards

What does pop without an index do?

It deletes the last element in the list.

43
New cards

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.

44
New cards

How is a method called?

Using dot notation on an object, e.g. "abc".upper() or my_list.append(5).

45
New cards

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.

46
New cards

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.

47
New cards

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.

48
New cards

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.

49
New cards

What has higher precedence in Python-- or *?

** exponentiation has higher precedence than multiplication.

50
New cards

How is 123163 evaluated in Python?

It's read as (((12)3)(16))3.

51
New cards

What is the final result of print(123163)?

18

52
New cards

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.

53
New cards

Which operators are left-to-right associative in Python?

Exponentiation (**), assignment (= , += etc .), and lambda expressions.

54
New cards

What is a = b = c = 5 actually doing?

It's evaluated right to left. All variables become 5.

55
New cards

Why does Python make exponentiation right associative?

Because mathematical exponent chains like 2 * 3 2 naturally mean 2 (3 2), not (2 3) * 2.

56
New cards

def func(py):

return ['Python', 'Java']

Always returns ['Python', 'Java'], which is a list, regardless of the argument of func().

57
New cards

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.

58
New cards

What is an escape character in Python?

It's a backslash (\) used in strings to give special meaning to the character that follows it.

59
New cards

What does \n mean in Python strings?

It's a newline character that tells Python to start a new line in the output.

60
New cards

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.

61
New cards

What does print() do when called with no arguments?

It prints a blank line (just a newline character).

62
New cards

How do you stop print() from adding a newline at the end?

Use the optional argument end="" inside print() to prevent the newline.

63
New cards

What does the sep argument do in the print() function?

It specifies the separator between multiple values printed in one print() call.

64
New cards

Give an example of using sep in print()

print("A", "B", "C", sep="-")

# Output: A-B-C

65
New cards

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

66
New cards

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.

67
New cards

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.

68
New cards

What is string formatting in Python?

It's a way to insert variables into strings using methods like f-strings, .format() or % formatting.

69
New cards

What does returning a value mean?

the main purpose of the function is to calculate and give something back.

70
New cards

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."

71
New cards

Give an example of an f-string.

age = 7

print(f"My son is {age} years old.")

#Output: My son is 7 years old.

72
New cards

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.

73
New cards

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.

74
New cards

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.

75
New cards

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.

76
New cards

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.

77
New cards

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.

78
New cards

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.

79
New cards

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.

80
New cards

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.

81
New cards

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.

82
New cards

Mnemonic for forwardslash and backslash

Backslash leans back \ like you're reclining.

Forward slash leans forward / like you're charging ahead.

83
New cards

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.

84
New cards

How do you print a single backslash in a Python string?

Use two backslashes: print("\\"). The first backslash escapes the second,

85
New cards

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.

86
New cards

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.

87
New cards

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.

88
New cards

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.

89
New cards

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.

90
New cards

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.

91
New cards

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.

92
New cards

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.

93
New cards

What does print("A", "B", "C") output in Python?

It prints

A B C

Each item is separated by a. space (the default separator).

94
New cards

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.

95
New cards

Does Python insert spaces between multiple print() arguments?

Yes. By default, print() inserts a single space between each argument.

96
New cards

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.

97
New cards

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.

98
New cards

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).

99
New cards

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.

100
New cards

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