0% found this document useful (0 votes)
9 views

Python Essentials 1 - Module 2

The document covers Python essentials, focusing on string arguments in functions, the use of literals, integers, floats, and various number systems (decimal, octal, hexadecimal). It explains the print() function, keyword arguments, and basic operators, including exponentiation, multiplication, division, and their priorities. Additionally, it introduces variables, their naming conventions, and reserved keywords in Python.

Uploaded by

teo.lenarduzzi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Python Essentials 1 - Module 2

The document covers Python essentials, focusing on string arguments in functions, the use of literals, integers, floats, and various number systems (decimal, octal, hexadecimal). It explains the print() function, keyword arguments, and basic operators, including exponentiation, multiplication, division, and their priorities. Additionally, it introduces variables, their naming conventions, and reserved keywords in Python.

Uploaded by

teo.lenarduzzi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Python Essentials 1 – Module 2.

1) String as the print() function's argument


A function (in this context) is a separate part of the computer code able to:
 cause some effect (e.g., send text to the terminal, create a file, draw an
image, play a sound, etc.); this is something completely unheard of in the
world of mathematics;
 evaluate a value (e.g., the square root of a value or the length of a given
text) and return it as the function's result; this is what makes Python
functions the relatives of mathematical concepts.
 There's also a third, very important, function component ‒
the argument(s).
The string is delimited with quotes ‒ in fact, the quotes make the string ‒
they cut out a part of the code and assign a different meaning to it.
You can imagine that the quotes say something like: the text between us is not
code. It isn't intended to be executed, and you should take it as is.
The empty print() invocation is not as empty as you may have expected ‒ it does
output an empty line, or (this interpretation is also correct) it outputs a newline.
This is not the only way to produce a newline in the output console. The
backslash (\) has a very special meaning when used inside strings ‒ this is
called the escape character. The letter n placed after the backslash comes
from the word newline.
Both the backslash and the n form a special symbol named a newline
character, which urges the console to start a new output line.
2) Multiples arguments
a print() function invoked with more than one argument outputs them all on
one line;
the print() function puts a space between the outputted arguments on its
own initiative.
3) Keyword arguments
In order to use it, it is necessary to know some rules:
 a keyword argument consists of three elements: a keyword identifying
the argument (end here); an equal sign (=); and a value assigned to that
argument;
 any keyword arguments have to be put after the last positional
argument (this is very important)

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).

Python Essentials 1 – Module 2.2


1) Literals – the data in itself
A literal is data whose values are determined by the literal itself, you use
literals to encode data and to put them into your code.
Exemple :

print("2")
print(2)

Through this example, you encounter two different types of literals:


 a string, which you already know,
 and an integer number, something completely new.
The print() function presents them in exactly the same way ‒ this example is
obvious, as their human-readable representation is also the same. Internally, in
the computer's memory, these two values are stored in completely different ways
‒ the string exists as just a string ‒ a series of letters.
2) Integers
Numbers handled by modern computers are of two types:
 integers, that is, those which are devoid of the fractional part;
 and floating-point numbers (or simply floats), that contain (or are able
to contain) the fractional part.
This definition is not entirely accurate, but quite sufficient for now. The distinction
is very important, and the boundary between these two types of numbers is very
strict. Both of these kinds of numbers differ significantly in how they're stored in
a computer memory and in the range of acceptable values.
The characteristic of the numeric value which determines its kind, range, and
application, is called the type.
If you encode a literal and place it inside Python code, the form of the literal
determines the representation (type) Python will use to store it in the memory.
Python doesn't accept things like 1 111 1111 (like we would write it with a
pencil). It's prohibited. What Python does allow, though, is the use
of underscores in numeric literals.*
Therefore, you can write this number either like this: 11111111, or like
this: 11_111_111.
And how do we code negative numbers in Python? As usual ‒ by adding a minus.
You can write: -11111111, or -11_111_111.
3) Decimal, octal and hexadecimal numbers
Décimal :

Position Equivalent
1 (Unité) 1
10 (Dizaine) 2
100 (Centaine) 4
1000 (Milliers) 8

Exemple : 1011 en base 2 (Decimal)


 1 à la place des milliers → 1×8=8
 0 à la place des centaines → 0×4=0
 1 à la place des dizaines → 1×2=2
 1 à la place des unités → 1×1=1
Ce qui donnerait 1011 (base 2) = 8 + 0 + 2 + 1 = 11 en décimal.

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

Exemple : 234 en base 8 (Octal)


 2 à la place des centaines → 2×64=128
 3 à la place des dizaines → 3×8=24
 4 à la place des unités → 4×1=4
Ce qui donnerait 234 (base 8) = 128 + 24 + 4 = 156 en décimal.
Hexadécimal :
The second convention allows us to use hexadecimal numbers. Such numbers
should be preceded by the prefix 0x or 0X (zero-x).
0x234 is a hexadecimal number with a (decimal) value equal to 564

Position Equivalent
1 (Unité) 1
10 (Dizaine) 16
100 (Centaine) 256
1000 (Milliers) 4096

Exemple : 234 en base 16 (Hexadécimal)


 2 à la place des centaines → 2×256=512
 3 à la place des dizaines → 3×16=48
 4 à la place des unités → 4×1=4
Ce qui donnerait 234 (base 8) = 512 + 48 + 4 = 564 en décimal.
4) Floats
Whenever we use a term like two and a half or minus zero point four, we think of
numbers which the computer considers floating-point numbers
If your native language prefers to use a comma instead of a point in the number,
you should ensure that your number doesn't contain any commas at all.
Python will not accept that, or (in very rare but possible cases) may
misunderstand your intentions, as the comma itself has its own reserved
meaning in Python.
On the other hand, it's not only points that make a float. You can also use the
letter e.
When you want to use any numbers that are very large or very small, you can
use scientific notation.
Take, for example, the speed of light, expressed in meters per second. Written
directly it would look like this: 300000000.
To avoid writing out so many zeros, physics textbooks use an abbreviated form,
which you have probably already seen: 3 x 108
In Python, the same effect is achieved in a slightly different way ‒ take a look:
3E8 = The letter E (you can also use the lower-case letter e ‒ it comes from the
word exponent) is a concise record of the phrase times ten to the power of.
5) Coding strings
How to encode a quote inside a string which is already delimited by quotes ? 2
possibilities :
print('I\'m Monty Python.')
print("I'm Monty Python.")

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

The result produced by the division operator is always a float, regardless


of whether or not the result seems to be a float at first glance: 1 / 2, or if it looks
like a pure integer: 2 / 1.
Is this a problem? Yes, it is. It happens sometimes that you really need a division
that provides an integer value, not a float.
A // (double slash) sign is an integer division operator. It differs from the
standard / operator in two details:
 its result lacks the fractional part ‒ it's absent (for integers), or is always
equal to zero (for floats); this means that the results are always
rounded;
 it conforms to the integer vs. float rule.

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

As you can see, the result is two. This is why:


 14 // 4 gives 3 → this is the integer quotient;
 3 * 4 gives 12 → as a result of quotient and divisor multiplication;
 14 - 12 gives 2 → this is the remainder.
Addition :
The addition operator is the + (plus) sign, which is fully in line with mathematical
standards.
Subtraction :
The subtraction operator is obviously the - (minus) sign
2) Operators and their priorities
The binding of the operator determines the order of computations performed by
some operators with equal priority, put side by side in one expression.
Most of Python's operators have left-sided binding, which means that the
calculation of the expression is conducted from left to right.
/!\ Becarefull, the exponentiation operator uses right-sided binding.
Python Essentials 1 – Module 2.4

1) Variables – data-shaped boxes


How do you save the intermediate results, and use them again to produce
subsequent ones?
Python will help you with that. It offers special "boxes" (or "containers" as we
may call them) for that purpose, and these boxes are called variables
If you want to give a name to a variable, you must follow some strict rules:
 the name of the variable must be composed of upper-case or lower-case
letters, digits, and the character _ (underscore)
 the name of the variable must begin with a letter;
 the underscore character is a letter;
 upper- and lower-case letters are treated as different (a little differently
than in the real world – Alice and ALICE are the same first names, but in
Python they are two different variable names, and consequently, two
different variables);
 the name of the variable must not be any of Python's reserved words (the
keywords
Take a look at the list of words that play a very special role in every Python
program.

['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.

You might also like