CS 161 - Module 2: Comments, Arithmetic & String Concatenation

1. Introduction – Writing Code That Makes Sense

If you’ve ever looked back at your own code and thought, “Who wrote this nonsense?”, congratulations! You’ve just learned why comments matter. Programming isn’t just about making a computer understand your code—it’s about making sure future you (and others) don’t hate you for writing unreadable code.

This module covers:

  • Comments: How to write notes in your code.

  • Arithmetic Operations: Math in Python.

  • String Concatenation: Mashing strings together for fun and functionality.

💡 Fun Fact: Code without comments is like a book with missing pages—you might figure it out eventually, but it'll be painful.

2. Comments – Your Future Self Will Thank You

Comments are lines in your code that Python ignores. They are meant for humans, not computers.

# This is a comment explaining something important

height = 1.75  # This stores a person's height in meters

Why Use Comments?

✅ Explain tricky logic.
✅ Remind yourself why you did something.
✅ Help others understand your code.
✅ Avoid future frustration when debugging.

💡 Rule of Thumb: Write comments like you’re explaining to a tired version of yourself six months from now.

3. Arithmetic Operations – Python Does Math

Python can handle basic and advanced math operations using standard symbols.

Basic Operations:

add = 7 + 3    # 10

subtract = 7 - 3  # 4

multiply = 7 * 3  # 21

divide = 7 / 3  # 2.3333 (always returns a float)

Floor Division (//) – No Decimals Allowed

Floor division rounds down to the nearest whole number.

floor_result = 10 // 3  # 3

negative_floor = -10 // 3  # -4

Modulus (%) – Remainder of Division

remainder = 10 % 3  # 1 (since 10 ÷ 3 is 3 remainder 1)

Exponentiation () – Power Up**

squared = 4 ** 2  # 16 (4 squared)

cube_root = 8 ** (1/3)  # 2.0 (cube root of 8)

💡 Order of Operations: Parentheses first, then exponents, then multiplication/division/modulus, then addition/subtraction.

result_1 = 3 * 4 + 2  # 14 (Multiplication first)

result_2 = 3 * (4 + 2)  # 18 (Parentheses first)

4. Shortcut Operators – Because We’re Efficient

Python allows shortcut operators to modify variables in place.

num = 10

num += 2  # Same as num = num + 2 (Now num is 12)

num -= 3  # Now num is 9

num *= 2  # Now num is 18

num /= 3  # Now num is 6.0

num //= 2  # Now num is 3 (integer division)

num %= 2  # Now num is 1 (remainder)

num **= 3  # Now num is 1 (1 cubed is still 1)

💡 Most common? += (for updating totals).

5. Absolute Value – Making Numbers Positive

Want to make sure a number is always positive?

value = abs(-42)  # 42

💡 Use case: Keeping scores or distances positive.

6. String Concatenation – Combining Text

The + operator also works on strings, joining them together.

first_name = "Alice"

last_name = "Johnson"

full_name = first_name + " " + last_name

print(full_name)  # Alice Johnson

💡 Pro Tip: Always check for spaces! "Alice"+"Johnson" results in "AliceJohnson".

7. Interactive Learning & Challenges

Try running these examples in Python Tutor (pythontutor.com) to see them execute step by step.

Exercises

1. Print the remainder of 19 divided by 6

print(19 % 6)

✅ Expected Output: 1

2. Print 2 raised to the 4th power

print(2 ** 4)

✅ Expected Output: 16

3. Assign division result to a variable and print it

conversion_ratio = 9 / 5

print(conversion_ratio)

✅ Expected Output: 1.8

4. Update a variable using shortcut operators

num_planets = 9

num_planets -= 1  # Now 8

print(num_planets)

✅ Expected Output: 8

5. Concatenate strings and print the result

title = "Professor"

last_name = "Miller"

print(title + " " + last_name)

✅ Expected Output: Professor Miller

💡 Challenge Yourself! Try modifying these exercises and testing different values.

8. Reflection: What We Learned

  • Comments help make your code understandable.

  • Python arithmetic includes basic math, floor division, and exponentiation.

  • Shortcut operators make modifying values more efficient.

  • Concatenation joins strings together using +.

9. Conclusion & Call to Action

Now that you know how to use comments, math, and string concatenation, it's time to experiment!

💡 Question for You: What’s the weirdest arithmetic mistake you’ve ever made in Python? Share it below!

Previous
Previous

CS 161 - Module 2: Print Statements & Types

Next
Next

CS 161: Exploring Algorithms, Computers, and Python