HomepageTeaching PageResearch PageResources Page

Homework: Variables and Statements

Read sections 2.1, 2.2, and 2.3 in the book and do the examples.

The use of underscores in variable names is one way to separate words without using spaces. This is called a case in programming; using the unscores and all lowercase characters is specifically known as snake_case and is the standard for Python variables. Other cases include camelCase, PascalCase, and ALL_CAPS.

If we wanted to calculate the volume of a box with sides length 3, 4, and 5, we could either do that in one step:

>>> volume = 3 * 4 * 5
>>>        
        

... or we could do it in two:

>>> base_area = 3 * 4
>>> volume = base_area * 5
>>>
        

Either way, the value of volume will be 60.

Read sections 2.4 through 2.8 in the book and do the examples. The debugging section at the end of the chapter is common: there's one in each chapter and they are very helpful. I recommend checking it out because the tricks there can help in the projects.

PEMDAS is relevant with string operations too!

>>> lyrics = "money " * 3 + "must be funny"
>>> print(lyrics)
money money money must be funny
>>>        
        

Also, we can use parentheses:

>>> 3 * ("mon" + "key")
monkeymonkeymonkey
>>>
        

And, you can use variables:

>>> animal = "ant"
>>> new_animal = animal + "eater"
>>> print(new_animal)
anteater
>>>