Until now, we have mostly worked with values, like strings and numbers (for instance integers), that we printed directly to the screen. One of the most powerful features of programming languages is the ability to save a value with a name, which makes it a variable. Now that you can define variables, we can start to make our formulas dynamic and dependent on the values of the variables.
For our first formula, we will define two variables that we need in order to calculate the area of a rectangle: the length and the width. These variables should be named exactly `length` and `width` in your code.
Your task is to assign values to these variables (in a so-called assignment statement) and use them in a formula to print out the rectangle area. Write this in a program called `rectangle_area.py`.
After creating this script, please note that you can simply change the values of these variables to produce different outputs. In our example below, we have a width of `10` and a length of `33`.
-!- CODE language-bash -!-$ python3 rectangle_area.py
330
CodeGrade is your virtual teaching assistant for your coding classroom. You teach, CodeGrade takes care of the rest.
With our 40+ built-in fully automatically graded Introduction to Python assignments for university-level courses, you can start teaching Python at your institution with confidence now. Or, scale your classroom with CodeGrade's tools.
Book a personal 30-minute demo now and we'll show you how to get started with CodeGrade in less than 1 hour!