Home / Community / Stanford CS106A - Programming Methodology (Python)
Public

Stanford CS106A - Programming Methodology (Python)

Master Python fundamentals and core computer science principles with this comprehensive flashcard deck, mirroring Stanford CS106A. Dive into syntax, control flow, functions, data structures, OOP, and essential algorithms to build a strong programming foundation.

22 accessible of 22 cards

Card Preview

22 accessible of 22 cards

A quick, read-only look at the deck content.

Term

What is the primary purpose of print() and input() in Python?

Definition

print() is used to display output to the console, while input() is used to get user input from the console, typically as a string.

Term

Explain the difference between mutable and immutable data types in Python. Provide examples.

Definition

Mutable types can be changed after creation (e.g., list, dict, set). Immutable types cannot be changed after creation; any 'modification' creates a new object (e.g., int, float, str, tuple).

Term

How do you define a function in Python, and what is the significance of the return statement?

Definition

Functions are defined using the def keyword, followed by the function name, parameters in parentheses, and a colon. The return statement sends a value back to the caller and exits the function. If omitted, the function implicitly returns None.

Term

Describe the basic structure of an if-elif-else statement in Python.

Definition

An if-elif-else statement allows conditional execution based on boolean conditions.
python6 lines
if condition1:
    # code if condition1 is true
elif condition2:
    # code if condition1 is false and condition2 is true
else:
    # code if all conditions are false

Term

What is a for loop used for in Python? Provide an example iterating over a list.

Definition

A for loop is used to iterate over a sequence (like a list, tuple, string, or range) or other iterable objects.
python3 lines
my_list = [10, 20, 30]
for item in my_list:
    print(item)

Term

When would you use a while loop instead of a for loop?

Definition

A while loop is used when the number of iterations is not known beforehand and depends on a condition remaining true. A for loop is typically used when iterating over a known sequence or a fixed number of times.

Term

How do you access elements in a Python list? What is slicing?

Definition

Elements are accessed by their zero-based index (e.g., my_list[0]). Slicing allows accessing a sub-sequence of a list using my_list[start:end:step], creating a new list.

Term

Explain the purpose of a Python dictionary. How do you add or access elements?

Definition

A dictionary is an unordered collection of key-value pairs, where each key must be unique and immutable. It's optimized for fast lookups.
python5 lines
my_dict = {"name": "Alice", "age": 30}
# Access
print(my_dict["name"]) # Output: Alice
# Add/Modify
my_dict["city"] = "New York"