Python Essentials 1 - Module 2
Python Essentials 1 - Module 2
As you can see, the end keyword argument determines the characters
the print() function sends to the output once it reaches the end of its positional
arguments.
The default behavior reflects the situation where the end keyword argument
is implicitly used in the following way: end="\n".
We said previously that the print() function separates its outputted arguments
with spaces. This behavior can be changed, too.
The keyword argument that can do this is named sep (as in separator).
print("2")
print(2)
Position Equivalent
1 (Unité) 1
10 (Dizaine) 2
100 (Centaine) 4
1000 (Milliers) 8
There are two additional conventions in Python that are unknown to the world of
mathematics.
Octal :
The first allows us to use numbers in an octal representation.
If an integer number is preceded by an 0O or 0o prefix (zero-o), it will be treated
as an octal value. This means that the number must contain digits taken from the
[0..7] range only.
0o234 is an octal number with a (decimal) value equal to 156.
Position Equivalent
1 (Unité) 1
10 (Dizaine) 8
100 (Centaine) 64
1000 (Milliers) 512
Position Equivalent
1 (Unité) 1
10 (Dizaine) 16
100 (Centaine) 256
1000 (Milliers) 4096
As you can see, the backslash is a very powerful tool ‒ it can escape not only
quotes, but also apostrophes.
6) Boolean values
Each time you ask Python if one number is greater than another, the question
results in the creation of some specific data ‒ a Boolean value
A programmer writes a program, and the program asks questions. Python
executes the program, and provides the answers. The program must be able to
react according to the received answers.
Fortunately, computers know only two kinds of answers:
Yes, this is true;
No, this is false.
Python Essentials 1 – Module 2.3
1) Basic operators
Exponentiation :
An **(asterisks) sign is a exponentiation operator.
When both ** arguments are integers, the result is an integer, too. When at
least one ** argument is a float, the result is a float, too.
/!\ Becarefull, the exponentiation operator uses right-sided binding.
print(2 ** 3)
print(2 ** 3.)
8
8.0
Multiplication :
An * (asterisk) sign is a multiplication operator.
print(2 * 3)
print(2 * 3.)
6
6.0
Division :
A / (slash) sign is a division operator.The value in front of the slash is
a dividend, the value behind the slash, a divisor
print(6 / 3)
print(6 / 3.)
2.0
2.0
print(6 // 3)
print(6 // 3.)
2
2.0
The result of integer division is always rounded to the nearest integer value that
is less than the real (not rounded) result.
This is very important: rounding always goes to the lesser integer.
print(-6 // 4)
print(6. // -4)
-2
-2.0
The result is two negative twos. The real (not rounded) result is -1.5 in both
cases. However, the results are the subjects of rounding. The rounding goes
toward the lesser integer value, and the lesser integer value is -2, hence: -
2 and -2.0.
Remainder (modulo):
The next operator is quite a peculiar one, because it has no equivalent among
traditional arithmetic operators.
Its graphical representation in Python is the % (percent) sign, which may look a
bit confusing.
The result of the operator is a remainder left after the integer division.
In other words, it's the value left over after dividing one value by another to
produce an integer quotient.
Note: the operator is sometimes called modulo in other programming languages.
print(14 % 4)
2
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del',
'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
They are called keywords or (more precisely) reserved keywords. They are
reserved because you mustn't use them as names: neither for your variables, nor
functions, nor any other named entities you want to create.
The meaning of the reserved word is predefined, and mustn't be changed in any
way.