Chapter 2 Using Python as a Calculator
2.2 Values and Data Types¶
A value is one of the fundamental things — like a word or a number —
that a program manipulates. The values seen so far is 5
(the
result when 2 + 3
is added). These values are often referred as objects.
Note
Actually, the 2 and the 3 that are part of the addition above are values(objects) as well.
These objects are classified into different classes, or data types: 4
is an integer, and "Hello, World!"
is a string, so-called because it
contains a string or sequence of letters. You (and the interpreter) can identify strings
because they are enclosed in quotation marks.
If you are not sure what class a value falls into, Python has a function called type which can tell you.
Not surprisingly, strings belong to the class str and integers belong to the class int.
Note
When the value of a string is displayed using the print
function, such as in the third example above, the quotes are no longer present. The
value of the string is the sequence of characters inside the quotes. The quotes are only necessary to help Python know what the value is.
In the Python shell, it is not necessary to use the print
function to see the values shown above. The shell evaluates the Python function and automatically prints the result. For example, consider the shell session shown below. When
the shell is asked to evaluate type("Hello, World!")
, it responds with the appropriate answer and then goes on to
display the prompt for the next use.
Python 3.6.9 (default, Dec 8 2021, 21:08:43)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> type("Hello, World!")
<class 'str'>
>>> type(17)
<class 'int'>
>>> "Hello, World"
'Hello, World'
>>>
Note that in the last example, the shell is simply asked to evaluate the string “Hello, World”. The result is as you might expect, the string itself.
Continuing with the discussion of data types, numbers with a decimal point belong to a class called float, because these numbers are represented in a format called floating-point. At this stage, you can treat the words class and type interchangeably. We’ll come back to a deeper understanding of what a class is in later chapters.
What about values like "17"
and "3.2"
? They look like numbers, but they
are in quotation marks like strings.
They’re strings!
Strings in Python can be enclosed in either single quotes ('
) or double
quotes ("
), or three of each ('''
or """
)
Double quoted strings can contain single quotes inside them, as in "Bruce's
beard"
, and single quoted strings can have double quotes inside them, as in
'The knights who say "Ni!"'
.
Strings enclosed with three occurrences of either quote symbol are called
triple quoted strings. They can contain either single or double quotes:
Triple quoted strings can even span multiple lines:
Python doesn’t care whether you use single or double quotes or the three-of-a-kind quotes to surround your strings. Once it has parsed the text of your program or command, the way it stores the value is identical in all cases, and the surrounding quotes are not part of the value.
So the Python language designers usually chose to surround their strings by single quotes. What do you think would happen if the string already contained single quotes?
The print function can print any number of values as long as you separate them by commas. Notice that the values are separated by spaces when they are displayed.
Check your understanding
- Print out the value and determine the data type based on the value printed.
- You may be able to determine the data type based on the printed value, but it may also be deceptive, like when a string prints, there are no quotes around it.
- Use the type function.
- The type function will tell you the class the value belongs to.
- Use it in a known equation and print the result.
- Only numeric values can be used in equations.
- Look at the declaration of the variable.
- In Python variables are not declared.
2.2Q-1: How can you determine the type of a variable?
- Character
- It is not a single character.
- Integer
- The data is not numeric.
- Float
- The value is not numeric with a decimal point.
- String
- Strings can be enclosed in single quotes.
2.2Q-2: What is the data type of ‘this is what kind of data’?
© Copyright 2024 GS Ng.