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
In the previous sections, we identified the types
of variables by imagining how they are stored in
memory. However, Python offers a built-in function
called type that allows us to identify the type of
each variable. The type function takes the name of
a variable as an argument and returns its type (we
will cover functions, arguments, and return values
in Chapter 14). Then, we can use the print function
to print the types of those variables on the screen.
The following code shows the creation of the vari-
able productPrice and then prints the defined type of
that variable (which is initially an int type). Then,
the variable is modified with a float value, and the
variable type is printed again (this time float type).
Figure 5-8 shows the output on the screen when
running this code.
Code and execute
1. productPrice = 150
2. print(type(productPrice))
3. productPrice = 250.54. print(type(productPrice))
[>
Figure 5-8. Execution of the previous code.
5.7. Comments in Python
Many programming languages allow adding com-
ments to the code we are developing. These com-
ments are defined to leave clues about what the code
does. These clues can be for my “future self” (for ex-
ample, if I return to the project in two weeks or two
months and need to quickly remember what that
piece of code did) or for colleagues to understand my
code.
The following code shows two ways to define com-
ments in Python. Line 1 shows how to define single-
line comments (using the hashtag symbol “#”). If a
line of code in Python starts with “#’, Python will
ignore it (will not execute it). Lines 4 to 5 show how
to define multiline comments (using three double
quotes ""). Python will ignore all lines starting withthree double quotes until it finds another set of three
double quotes representing the end of the comment.
Code and execute
1. #The following line represents the monthly salary
of a person
2. salary = 5000
aa
4." We will divide the salary by 2 to distribute it
among the family
5. members and print the result to the screen
6. print(salary/2)
5.8. Exercises
Theoretical exercises
E5.1. What type is each of the following variables?
Analyze
1. totalToPay = 1450.7
2. productName = "iPhone 14"
3. satellites = 7