Home / Community / Stanford CS106A - Python Programming Methodology
Public

Stanford CS106A - Python Programming Methodology

Unlock core Python programming skills with this comprehensive flashcard deck, mirroring Stanford CS106A's foundational concepts. Ideal for beginners, master variables, control flow, functions, data structures, and algorithmic thinking for robust problem-solving.

21 accessible of 21 cards

Card Preview

21 accessible of 21 cards

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

Term

What is a variable in Python?

Definition

A named storage location that holds a value. Variables are dynamically typed in Python, meaning their type can change during execution. Example: x = 10, name = "Alice".

Term

List the four primary built-in data types in Python.

Definition

1. int: Integers (e.g., 5, -100)
2. float: Floating-point numbers (e.g., 3.14, -0.5)
3. str: Strings (sequences of characters, e.g., "hello")
4. bool: Booleans (either True or False)

Term

Explain type conversion (casting) in Python with an example.

Definition

Type conversion is changing a value from one data type to another. Python provides built-in functions like int(), float(), str(), and bool().
Example:
```python
num_str = "123"
num_int = int(num_str) # Converts string "123" to integer 123
```

Term

Describe the basic structure of an if/elif/else statement.

Definition

Used for conditional execution. Code blocks are executed based on whether a condition evaluates to True.
```python
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

How do you iterate a specific number of times using a for loop?

Definition

Use the range() function. range(n) generates numbers from 0 up to (but not including) .
Example:
```python
for i in range(5):
print(i) # Prints 0, 1, 2, 3, 4
```

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 sequence or a known range.
Example:
```python
count = 0
while count < 3:
print(count)
count += 1
```

Term

Define a simple Python function that takes two numbers and returns their sum.

Definition

```python
def add_numbers(a, b):
return a + b
result = add_numbers(5, 3) # result will be 8
```

Term

Explain the difference between local and global scope in Python functions.

Definition

  • Local scope: Variables defined inside a function are local to that function and cannot be accessed from outside it.
  • Global scope: Variables defined outside any function are global and can be accessed (but not directly modified without the global keyword) from anywhere in the program.