Please answer the following questions to check your understanding before continuing. These are quick questions to refresh your memory.
Please answer the following questions to check your understanding before continuing. These are quick questions to refresh your memory.
Variables are needed for various purposes in programming. You can use variables to store any information that will be needed later in the program's execution.
In Python programming variables are created like so:
Here ... means the value stored in the variable.
For example, when you used the input command to read a string from the user, you stored the string in a variable and then used the variable later in your program:
The value stored in a variable can also be defined using other variables:
Here the values stored in the three variables are not obtained from user input. They remain the same every time the program is executed. This is called hard-coding data into the program.
As implied by the name variable, the value stored in a variable can change. In the previous section we noticed that the new value replaces the old one.
During the execution of the following program, the variable word will have three different values:
The value stored in the variable changes each time the variable is assigned a new value.
The new value of a variable can be derived from its old value. In the following example the variable word is first assigned a value based on user input. Then it is assigned a new value, which is the old value with three exclamation marks added to the end.
Python variables are case-sensitive. These are all different variables:
Integers are whole numbers without decimal points. They can be positive, negative, or zero.
Notice there are no quotation marks around integers! If you add quotes, Python treats it as a string:
Different data types behave differently with various operations. Understanding this is crucial for writing correct programs.
Sometimes you need to print a message that combines strings and numbers. Python provides several ways to do this.
Convert numbers to strings using the str() function:
Python automatically adds spaces between comma-separated values:
F-strings are a modern and convenient way to format strings in Python. They were introduced in Python 3.6.
If you are using an older version of Python, f-strings may not work. They were introduced in Python version 3.6. Later on during the course you will install Python on your own computer. Unfortunately, the more modern versions of Python are not always available for older operating systems. If that is the case with your computer, when there are exercises requiring the use of f-strings, you can always try them out in the in-browser exercise templates in this course.
Floating point numbers (or floats) are numbers with decimal points. They can represent fractional values.
Your friend is working on an app for jobseekers. She sends you this bit of code:
The program should print out exactly the following:
The easiest way to transform the code so that it meets requirements is to use f-strings. You can print an empty line by adding an empty print command, or by adding the newline character \n into your string.
This program already contains two integer variables, x and y:
Please complete the program so that it also prints out the following:
The program should work correctly even if the values of the variables are changed. That is, if the first two lines are replaced with this
the program should print out the following:
Each print command usually prints out a line of its own, complete with a change of line at the end. However, if the print command is given an additional argument end = "", it will not print a line change.
For example:
Please fix this program so that the entire calculation, complete with result, is printed out on a single line. Do not change the number of print commands used.
Sometimes you need to convert data from one type to another. Python provides built-in functions for type conversion.
int() - Converts to integerfloat() - Converts to floating point numberstr() - Converts to stringBe careful when converting! If the string cannot be converted to the requested type, Python will raise an error:
The input() function always returns a string. When you need to work with numbers from user input, you must convert them first.
Continue to the next section: Arithmetic operations
Test your understanding with these additional interactive exercises! Write your code in the editors below and click the buttons to see the results.
Create variables to store your name, age, and favorite number:
Practice changing variable values:
Practice arithmetic with integers. Write code that performs addition, subtraction, multiplication, and division:
Understand the difference between strings and integers. Show what happens when you add integers vs concatenate strings:
Practice using f-strings for formatted output. Create a profile with name, age, and city:
Work with floating point numbers. Calculate the average of three decimal numbers:
Practice reassigning values based on current values. Start with a score of 10, add 5, then multiply by 2:
Combine different data types in output. Create variables for name (string), age (integer), and height (float):
Practice proper variable naming conventions. Use descriptive names with underscores for multi-word variables:
Create a complete student profile with multiple variables and formatted output: