1.2.1.2 PCEP Practice Test Compendium – Python basic types and literals
Python basic types and literals
01) A literal is data whose value is determined by the literal itself. Different kinds of data are coded in different ways, enabling Python (and the human reader of the source code) to determine each literal's type. Knowing each argument's type is crucial to understand what operations are legal, and what results can be returned.
02) Integer (int
for short) is a type dedicated to storing integral numbers, that is, numbers that lack fractional parts. For example, 1 is an integral number and 1.5 is not.
03) Most used integer literals in Python consist of a sequence of decimal digits. Such a sequence cannot include white spaces, but can contain any number of _
(underscore) characters. Note: there must not be more than one underscore between two digits, and the underscore must be neither the last nor the first character of the literal. Underscores don't change a literal's value, and their only role is to improve literal readability. Integer literals can be preceded by any number of -
(minus) or +
(plus) characters, which are applied to set the literal's sign.
04) Here are some examples of correct integer literals:
1_111
and1111
encode the same integer value (one thousand one hundred and eleven)-+-3
and-3
denote the same integer value (minus three)+1
and1
encode the same integer value (one)
05) Integer literals may be written using radices other than 10:
if a literal starts with either a
0o
or0O
digraph, it's an octal value (note: it must contain octal digits only!)0o10
encodes an integer value equal to eight.if a literal starts with either a
0x
or0X
digraph, it's a hexadecimal value (note: letters from a to f used as hexadecimal digits may be upper- or lower-case)0X11
encodes an integer value equal to seventeen.if a literal starts with either a
0b
or0B
digraph, it's a binary value (note: it must contain 0s and 1s only!)0b111 encodes an integer value equal to seven.
06) Floating point (float
for short) is a type designed to store real numbers (in the mathematical sense), that is, numbers whose decimal expansion is or can be non-zero. Such a class of numbers includes fractions (integers don't).
07) Float literals are distinguished from integers by the fact that they contain a dot (.
) or the letter e (lower- or upper-case) or both. If the only digits which exist before or after the dot are zeros, they can be omitted. Like integers, floats can be preceded by any number of -
and +
characters, which determine the number's sign. White spaces are not allowed, while underscores are.
08) If the float literal contains the letter e, it means that its value is scaled, that is, it's multiplied by a power of 10 while the exponent (which must be an integer!) is placed directly after the letter. A record like this:
mEn
is treated as a value equal to:
m × 10n
This syntax is called scientific notation and is used to denote a number whose absolute value is extremely large (close to infinity) or extremely small (close to zero).
09) Here are some examples of correct float literals:
1.1
– one and one-tenth1.0
(1.
for short) – one point zero0.1
(.1
for short) – one-tenth1E1
– ten point zero1e-1
– one-tenth-1.1E-1
– minus eleven hundredths
Last updated