We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
form actions. In the previous exam-
ple, we passed arguments to the print()
function, and the value of each argu-
ment was displayed on the screen. We
will discuss the topic of functions and
arguments in detail in Chapter 14.
Finally, let’s analyze the following code:
Code and execute
age=21
print(agee)
When we execute the above code, an error will ap-
pear on the screen (see Figure 5-4), indicating that
the variable agee is not defined. The problem is that
the variable name contains an extra letter “e”. In
Python, we must be very careful when using vari-
able names, as the name must be exactly the same in
every line of code where we use the variable. Python
even distinguishes between uppercase and lower-
case letters.ck (most recent call
2>()
Nametrror
Sipython -input-2-43f79bfa7606> in 2 print(agee)
Nameerror: name ‘agee' is not defined
Figure 5-4. Error when using an undefined variable.
5.5. Modifying Variables
As their name implies, variables can change their
values. For example, we can create a variable to store
the price of a product and then modify that price in
the code (e.g., to apply a discount).
Modifying the value of a variable
The following code shows how we create a variable
named productPrice, print its contents, modify its
value, and print its contents again. Note that the
numbers on the left-hand side of the code are there
only to help us easily identify the lines of code and
should not be included in your code.
Code and execute
1. productPrice = 150
2. print(productPrice)3. productPrice = 250
4. print(productPrice)
Figure 5-5 shows what happens internally in the
computer’s memory. Lines 1 to 4 are executed in se-
quence. When line 1 is executed, a variable named
productPrice is created with a value of 150. Later,
when line 3 is executed, the value of the productPrice
variable is modified from 150 to 250. Since the vari-
ables have the same name, Python only reserves one
space in memory (i.e., it only creates one box).
Line 1 Line 3
Figure 5-5. Modifying the value of a variable.
Modifying the value and type of a variable
In Python, it is possible to modify both the value and
type of a variable. The following code shows how we
create a variable named productPrice (initially with