Values and Types#

In this sheet, we will use Jupyter as a calculator and introduce

some underlying concepts: values, operations, expressions, types.

Expressions and initial calculations#

  1. Click on the following cell, then execute it by pressing the Shift+Enter keys:

1 + 1
2

Technically speaking, your web browser sent the content of the expression 1+1

in the cell to a noyau (kernel) (kernel in English) which

exécutée (execution) it, before sending the result back to be displayed. The noyau (kernel)

we are using (Python3) allows us to work in Python. There are noyau (kernel)s

for many other programming languages.

Definition: Expressions, values and operations

An expression (expression) is a combination of values by

operations (operation)” /> giving a new value.

Example

The expression 3 * (1 + 3) + (1 + 4) * (2 + 4) equals 42.

  1. Execute the following cell to calculate the value of the expression:

3 * (1 + 3) + (1 + 4) * (2 + 4)
42
  1. Modify the expression in the previous cell and execute it again.

  1. Use the following cell to calculate the area of a rectangle with a width of 25 and a length of 37:

### BEGIN SOLUTION
25 * 37
### END SOLUTION
925

Concept of type#

A value can represent data of very different kinds, such as an integer

, a real number, a boolean, but also a text, a record of notes, an image,

, a music, etc.

Definition: Types

In Python, each valeur has a type (type) that describes its nature. This type

also specifies:

  • The data structure: how the value is represented in memory.

  • The semantics of the operations.

Types in programming are essential because they allow computers to determine the nature of the information they need to process, store, or manipulate.

They define the properties and possible operations on the data represented, and indicate how this data is stored in memory.

Types impose constraints that help reinforce the accuracy of the data.

In addition, types are useful to developers for reasoning about the structure of software.

Definition: Basic Types

The basic types in Python are:

  • entiers (integer) (type int for integer) Examples: 1, 42, -32765

  • réels (float) (type float) Examples: 10.43, 1.0324432e22

  • chaînes de caractères (string) (type str forstring), between quotation marks or apostrophes: Examples: “hello”, ‘Alice likes Bob’

  • booléens (Boolean type) (type bool for boolean) Examples: True (true), False (false)

Integers, characters, and booleans form the

types ordinaux (ordinal type).

Example

The type function allows you to obtain the type of a value, as in the following examples:

type("Jean")
str
type(23)
int
type(1.70)
float
type(23.)
float
type(True)
bool

Arithmetic Operations on Integers

Operation

Example

Result

opposite

-(-5)

5

addition

17 + 5

22

subtraction

17 - 5

12

Exercise

The following cells present examples of calculations. Run them while carefully observing

the value of the result and its type.

3 * (1 + 3) + (1 + 4) * (2 + 4)
42
type(3 * (1 + 3) + (1 + 4) * (2 + 4))
int
42 / 2
21.0
type(42 / 2)
float
42 / 5
8.4
type(42 / 5)
float
42. / 5
8.4
type(42. / 5)
float
42 % 5
2
type(42 % 5)
int

Tip

Note To avoid incorrect operations, we configure many cells, such as those

below, to be non-modifiable. To do your own experiments

or to take personal notes, you can insert new

cells at any time. Insert a cell below now using the + button under the

tabs. In the toolbar of this sheet, select Markdown instead of

Codeto make it a text cell.

1 + 1.
2.0
type(1 + 1.)
float
42 == 6*7
True
type(42 == 6*7)
bool
41 == 2*21
False
type(41 == 2*21)
bool

Boolean expressions#

Note

In the last previous cells, we tested the equality between two

values. The result was either “true” or “false”, therefore of type bool. This is an

example of a expression booléenne.

Note the use of a double equal sign == for the equality test. We will soon see

the meaning in Python of the single equal sign =.

Definition: Boolean Expressions (Conditions)

A expression booléenne (Boolean expression) is an expression whose value is

“true” or “false” (type: bool).

A expression booléenne is also called a condition (condition).

Examples:

True
True
False
False
  • regarde() == Vide

  • x > 3.14

  • 2 <= n  and  n <= 5

Usual boolean operations

Operation

Example

Result

comparison

3 <= 5

True

strict comparison

3 < 5

True

strict comparison

3 > 5

False

equality

3 == 5

False

logical negation

not 3 <= 5

False

and

3 < 5 and 3 > 5

False

inclusive or

3 < 5 or  3 > 5

True

Exercise

For each of the following cells, determine in your head the value of the boolean expression, then verify it by running the cell.

1 < 2
True
2 > 3
False
1 < 2 and 2 > 3
False
1 < 2 or 2 > 3
True

Training#

Exercise

For each of the following expressions, determine mentally the type and value of the

result, and give your answer in the location indicated in the text cell below in the

form type, valeur; then verify by running the cells.

  1. Type: int, bool, or float? Value: 7, 9, 11, or 21?BEGIN SOLUTIONint, 11END SOLUTION

3 * 2 + 5
11
type(3 * 2 + 5)
int
  1. Type: int, bool, or float? Value: 12 or 12.5?BEGIN SOLUTIONint, 12END SOLUTION

25 / 2
12.5
type(25 / 2)
float
  1. Type: int, bool, or float? Value: 6, 7, 6.0000, or 7.0000?BEGIN SOLUTIONint, 7.0000END SOLUTION

3.5*2
7.0
type(3.5*2)
float
  1. Type: int, bool, or float? Value: 5, 4, or 4.66667?BEGIN SOLUTIONdouble, 4.66667END SOLUTION

14. / 3
4.666666666666667
type(14. / 3)
float
  1. Type: int, bool, or float? Value: 3, 7, 21, true, or false?BEGIN SOLUTIONbool, trueEND SOLUTION

3*7 == 21
True
type(3*7 == 21)
bool
  1. Type: int, bool, double? Value: 21, 25, true, false?BEGIN SOLUTIONbool, trueEND SOLUTION

(3*7 == 21) and (4.2 > 5.3 or 4*4 == 8*2)
True
type((3*7 == 21) and (4.2 > 5.3 or 4*4 == 8*2))
bool
  1. Type: int, bool, double? Value: 1, 0, true, false?BEGIN SOLUTIONbool, falseEND SOLUTION

True and False
False
type(True and False)
bool
  1. Type: int, bool, double? Value: 1, 0, True, False?BEGIN SOLUTIONbool, falseEND SOLUTION

True or False
True
type(True or False)
bool
  1. Type: int, bool, double? Value: 1, 0, true, false?BEGIN SOLUTIONbool, TrueEND SOLUTION

(False and False) or True
True
type((False and False) or True)
bool
  1. Type: int, bool, double? Value: 1, 0, True, False?BEGIN SOLUTIONbool, FalseEND SOLUTION

False and (False or True)
False
type(False and (False or True))
bool

Conclusion#

Congratulations, you have completed this first sheet where we explored

expressions, operations, values and

types. We also saw the basic types: int,

string, etc.

In particular, we saw that Python determines the type of a literal value based

on its syntax; thus 42 is of integer type (int), while 42. or 42.0 is

of real type (float). We also saw that the type of a value determines the

semantics of the operations that are applied to it: thus 17 / 5 calculates the

Euclidean division of the two integers, while 17. / 5 calculates their division as

real numbers.

Aside: syntax, semantics, and algorithm

These three notions – syntax, semantics, and algorithm – are essential for properly

specifying and understanding the behavior of a program. They make it possible to express

precisely how a program should be written, what it is supposed to accomplish, and

how it achieves its objective. By mastering these concepts, one can write

more correct, understandable, and maintainable code, which is fundamental for all

computer development.

  • Syntaxe (syntax): how it is written

  • Sémantique (semantics): what it does

  • Algorithme (algorithm): how it is done

Example

  • Syntax: 17 / 5

  • Semantics: calculates the integer division of 17 by 5

  • Algorithm: Euclidean division

In the next sheet, you will address some pre-existing mathematical functions

in Python.