CS 161 - Module 2: Variables & Assignment

1. Introduction – What’s the Deal with Variables?

If programming is like magic, then variables are the spells that make things happen. A variable stores a value, which can be changed, manipulated, or reassigned throughout your program. Unlike math, where x is always x, in programming, x can start as a number, then become a string, and then—if you’re feeling particularly chaotic—a Boolean.

Key Takeaways:

  • Variables store values and can be reassigned.

  • Python allows dynamic typing, meaning variables can change types.

  • Constants are just variables we agree not to change (but Python won’t stop us).

  • There are multiple ways to format strings with variable values.

💡 Fun Fact: In some programming languages, variables are strictly typed—but in Python, you can assign 100 to a variable and then turn it into "one hundred" without Python complaining. Because, hey, we’re all adults here.



2. Variables & Assignment Statements

A variable is a name that refers to a particular value. Think of it like a box where you store something, but you can swap out what’s inside at any time.

car_speed = 60  # 'car_speed' now refers to 60

username = "Charlie"  # 'username' now refers to "Charlie"

is_logged_in = True  # 'is_logged_in' now refers to True



Reassigning Variables

You can change the value of a variable at any time. Python won’t stop you.

score = 25

new_score = score  # Now both variables refer to 25



💡 Takeaway: Python doesn’t care about what a variable used to be—only what it is right now.



3. Variable Naming Rules (a.k.a. How Not to Annoy Python)

  • Variable names must start with a letter or underscore.

  • They can’t be Python keywords (like if, while, return).

  • They are case-sensitive (apple, Apple, and APPLE are all different variables).

user_age = 30  # ✅ Valid

5dogs = "Bark"  # ❌ Invalid (cannot start with a number)

def = "Function"  # ❌ Invalid (reserved keyword)



💡 Pro Tip: Choose descriptive variable names! total_price makes more sense than tp (unless you enjoy confusing your future self).



4. Python’s Dynamic Typing – Variables Can Change Types

In some languages, once you assign a variable a type, it stays that type forever. Not in Python!

item_count = 12

print(type(item_count))  # Output: <class 'int'>



item_count = "twelve"

print(type(item_count))  # Output: <class 'str'>



💡 Takeaway: Python lets you be flexible, but with great power comes great responsibility (don’t change types randomly—future you will hate it).



5. Constants – Python’s "Honor System"

A constant is a variable that shouldn’t change. Python doesn’t enforce this, but by convention, we use ALL CAPS for constant names.

MAX_USERS = 500

GRAVITY_EARTH = 9.81



💡 Reality Check: Python won’t stop you from changing a constant. But you shouldn’t. Be nice to your future self.



6. Literals – Values Written Directly in Code

Literals are just hardcoded values, like:

2024  # Integer literal

99.99  # Float literal

"Coding is fun!"  # String literal

False  # Boolean literal



💡 Takeaway: Literals are like the "raw ingredients" of programming—use them wisely.



7. Printing Variables & String Formatting

Python gives us multiple ways to insert variables into strings. Let’s break them down.

1. Comma-Separated Printing (Default Way)

print("Your score is", score, "points.")



✅ Automatically adds spaces between values.

2. String Concatenation (Using +)

print("Your score is " + str(score) + " points.")



✅ Requires converting non-strings into strings manually.

3. f-Strings (Recommended)

print(f"Your score is {score} points.")



✅ Cleaner and more readable.

💡 Takeaway: f-strings are the modern, Pythonic way to format text—use them!



8. Interactive Learning & Challenge

Want to test these concepts live? Try them in Python Tutor (pythontutor.com) and see your code execute step by step.

Exercises

1. Assign a float and print it

pi_value = 3.14159

print(pi_value)



✅ Expected Output: 3.14159

2. Assign a string and print it

last_name = "Anderson"

print(last_name)



✅ Expected Output: Anderson

3. Assign a float and print its type

height_cm = 172.5

print(type(height_cm))



✅ Expected Output: <class 'float'>

4. Assign a string and print its type

job_title = "Astronaut"

print(type(job_title))



✅ Expected Output: <class 'str'>

💡 Challenge Yourself! Try changing the variable types and see how Python reacts.



9. Reflection: What We Learned

  • Variables store values and can change.

  • Python allows dynamic typing (but don’t abuse it).

  • Constants are just variables in disguise that we agree not to change.

  • f-strings are the best way to format text with variables.



10. Conclusion & Call to Action

Now that you know how to use variables, assignment, and printing, it’s time to put these into practice!

💡 Question for You: What’s the funniest variable name you’ve ever used? Let me know in the comments! 🚀

Next
Next

CS 161 - Module 2: Input & Casting