Python tutorial: literals, values, operators and expressions
Numbers
the python interpreter reads the code and translate it into bytecode – a lower level language which is then executed.
This translation process consists in reading code text and create data structures which represents data objects and actions to be performed in memory
Data can be read from formats which are called literals: there can be different ways to represent the same object.
Here we will go through some of the numeric formats and their object representation
Try to type these literals into the CLI and python will return a “canonical” representation of the equivalent numeric object
Integers
integer values can be represented in multiple ways:
# decimal 1000000 # decimal with underscore for readability 1_000_000 # hexadecimal 0xAA19 # binary 0b1001010 # octal 0o675
Floats
floating point values can be represented with floating point or scientific format
# floating point -123.4 # scientific -1.234e2
Complex
python supports complex numbers literals with cartesian representation
(1*2j) # also floating point can be used in the cartesian format (1.0+2.0j)
Operator and expressions on numbers
ordinary infix operators are available for all numeric types, with the ordinary precedence rules.
Precedence can be adjusted by means of parethesis as usual.
for integer numbers also binary operators are useful:
integer numbers also have modulo operator
(2 + 3) * 4 # 20 3 / 4 # 0.75 promoted to float 3 // 4 # 0 integer division 3 ** 4 # 81 integer power (2+3j) * (2-3j) # 25 complex product
0b1001 | 0b0101 # yields 0b1101 or 13 0b1001 & 0b1001 # yields 0b0001 or 1 0b1001 ^ 0b0101 # yields 0b1100 or 12 ~ 0b0101 # yields 0b1010 or 10
13 % 4 # yields 1
Strings
Strings are data sequances mostly used for human readable text; python strings are of two kind:
- Unicode text strings: each readable character can be represented by one or more bytes
- Binary strings: each character will be represented by exactly one bytes, non-readable bytes by escaped hexadecimal sequences of two digits
Text strings
string literals are surrounded by single quotes
when the python REPL returns the canonical representation it uses only the single quote
'
or double quotes "
"hi mom" 'hi mom'
Triple quote
text in a string can be surrounded by triple quotes
when typing multiline expressions the CLI prompt switch to
"""
or '''
, this allows to introduce multiline string literals
"""hi mom"""
...
Escaping and Unicode
text strings include special characters which were used to control text representation: e.g. newline (ASCII 10) and carriage return (ASCII 13).
These characters are represented by a backward slash
\
followed by
- a character e.g. newline is
\n
and carriage return is\r
- a three digit octal number representing an ASCII character e.g.
\012
- a two digit hexadecimal number e.g.
\x0A
- an unicode number below 0x10000
\u000A
, →\u2192
- a 32 bit unicode number
\U0000000A
, →\U00002192
- a unicode name
"\N{RIGHTWARDS ARROW}"
- the forward slash itself
\\
- quotes when identical with the surrounding quotes
\'
and\"
Raw strings
by prefixing the letter
r
to the first quote, escaping characters will be ignored and treated literally: these are called raw strings
Forward slash appear in a couple of situation:
- in windows paths (here I will suggest better options later) e.g.
c:\TEMP
- in regular expressions character classes e.g.
\d
the class of digit characters
Binary strings
By prefixing a quote with the
b
letter the string will be interpreted as a binary sequence.
This is useful when
- reading or writing to an external device, possibly connected through a serial
- reading or writing binary files
b"\xCA\xFE\xBA\xBE"
Operators on strings
strings can use sum and multiplication as following
I will introduce more operators and activities on strings later
"hello " + "world" # yields hello world "hello " * 3 # yields "hello hello hello "
Booleans and None
there are a couple of predefined data types with a limited number of values which play a fundamental role in python
None
the None type contains just the
None
value
This can be seen as an equivalent of a NULL pointer, its actual usage will be shown later
Boolean values and operators
Boolean type has exactly two values, boolean shortcut operators are written as words and have the usual precedence rules
True False True and False True or False not True
Triple operator and truth values
the triple operator is composed of
the clause may contain also non boolean values (a deprecated practice)
In python the following objects are false
I will add more falsy values later. In contrast the following objects are considered “True”
I don’t recommend using this way to evaluate clauses as they may be less readable.
- the value to be returned if the clause is true
- the boolean clause
- the value to be returned if the clause is false
"there is sunshine" if True else "it rains" # returns "there is sunshine" -1 if False else 42 # yields 42
0 | integer or floating point number 0 |
None | the None object |
“” | the empty string |
1 | any integer, float or complex number different from 0 |
“hi” | any non-empty string |