CSE 231 · Flashcards
CSE 231 Exam Review: Python Fundamentals, Control Flow, Functions, Collections, Files and Classes
32 termsMichigan State Universityby @studyboardUpdated October 2026
A study set written by the Studyboard team for the topics in this course's published outline. It isn't official course material from Michigan State University and doesn't contain real exam questions.
Terms in this set (32)
- Result of 7 / 2, 7 // 2 and 7 % 2 in Python 37 / 2 is 3.5 (true division, float); 7 // 2 is 3 (floor division); 7 % 2 is 1 (remainder).
- What does int('42') + 1 return, and what does '42' + 1 do?int('42') + 1 returns 43. '42' + 1 raises a TypeError because you cannot add a str and an int.
- Mutable vs immutable types in PythonLists, dictionaries and sets are mutable (can change in place). Integers, floats, strings and tuples are immutable.
- What does 'python'[1:4] evaluate to?'yth'. Slicing includes the start index and excludes the stop index; indexing starts at 0.
- What does s[-1] return for a string s?The last character. Negative indexes count from the end, so s[-2] is the second-to-last.
- Why does s[0] = 'X' fail for a string s?Strings are immutable, so item assignment raises a TypeError. Build a new string instead, for example 'X' + s[1:].
- What values does range(2, 10, 3) produce?2, 5, 8. The stop value 10 is excluded.
- for loop vs while loopA for loop iterates over each item of a sequence or iterable; a while loop repeats as long as a condition is True and is used when the number of iterations is not known in advance.
- break vs continuebreak exits the loop entirely; continue skips the rest of the current iteration and moves on to the next one.
- Common infinite loop pitfall in a while loopForgetting to update the variable in the loop condition, so the condition never becomes False.
- Difference between = and === assigns a value to a variable; == compares two values and returns True or False.
- How does if / elif / else evaluate?Conditions are checked top to bottom; only the first True branch runs. else runs only if none of the conditions were True.
- Short-circuit evaluation of and / orWith and, if the left side is False the right side is not evaluated; with or, if the left side is True the right side is not evaluated.
- print vs return in a functionprint displays a value on screen; return sends a value back to the caller. A function with no return statement returns None.
- Local vs global scopeVariables assigned inside a function are local to it and disappear when it returns; globals are defined at the top level of the module. Use parameters and return values instead of modifying globals.
- Default parameter valuesdef greet(name, greeting='Hi'): lets callers omit greeting. Parameters with defaults must come after those without.
- Pitfall: using a mutable default argument like def f(x, items=[])The default list is created once when the function is defined and is shared across calls, so items accumulate unexpectedly. Use items=None and create a new list inside.
- lst.append(x) vs lst.extend(seq)append adds x as a single element at the end; extend adds each element of seq individually.
- Aliasing: after b = a where a is a list, what happens when you run b.append(5)?a also changes, because a and b refer to the same list object. Use b = a.copy() or a[:] for a separate copy.
- What does sorted(lst) do vs lst.sort()?sorted(lst) returns a new sorted list and leaves lst unchanged; lst.sort() sorts the list in place and returns None.
- List comprehension for the squares of 0 through 4[x**2 for x in range(5)] gives [0, 1, 4, 9, 16].
- Tuple vs listBoth are ordered sequences, but tuples are immutable and are written with parentheses; they can be used as dictionary keys if their contents are hashable.
- Dictionary basics: what are keys and values?A dict maps unique, immutable (hashable) keys to values. Access with d[key]; d[key] raises KeyError if missing, while d.get(key, default) returns the default instead.
- How do you loop over a dictionary's keys and values together?for key, value in d.items(): ...
- Counting occurrences of words with a dictionaryfor w in words: counts[w] = counts.get(w, 0) + 1
- Why use 'with open(name) as f:' to read a file?The with statement automatically closes the file when the block ends, even if an error occurs.
- File modes 'r', 'w' and 'a''r' reads (error if missing), 'w' writes and erases existing contents (creates file if missing), 'a' appends to the end.
- What does line.strip().split(',') do?strip removes leading and trailing whitespace including the newline; split(',') breaks the string into a list of substrings at each comma.
- try / except: purposeCode in try runs normally; if a matching exception is raised, control jumps to the except block instead of crashing, for example except ValueError when converting user input with int().
- Class vs object, and the role of __init__A class is a blueprint; an object is an instance of it. __init__ is the initializer method that runs when an object is created and typically sets instance attributes.
- What is self in a Python method?self is the first parameter of an instance method and refers to the specific object the method was called on, giving access to its attributes.
- What does defining __str__ in a class do?It returns the string used when the object is printed or passed to str(), giving a readable representation.
Something wrong with this flashcard set? Open it in Studyboard and choose Report.