Lesson 10 Slides
Lesson 10 Slides
Lesson 10
Variables and data types
SEDIN
IT Education Services Center
2
Mathematical operators
Symbol Operation Description
+ Addition Adds two numbers
- Subtraction Subtracts two numbers
* Multiplication Multiplies two numbers
Divides two numbers and returns a floating-
/ Division
point number
Floor division (aka Divides two numbers and rounds down to an
//
integer division) integer (*)
(*) when the result is positive, the decimal part is simply deleted while if the result
is negative it is rounded down to the nearest whole.
4
Mathematical expressions
In mathematical expressions, the operations enclosed in parentheses are
executed first and then the operator with the highest precedence is applied
first:
• Exponentiation
• Multiplication and division
• Addition and subtraction
PEMDAS (Parentheses, Exponentiation, Multiplication, Division, Addition, Subtraction)
>>> (6 - 3)* (2 + 7) / 3
9.0
Note: numbers in Python are always
>>> 6 - 3 * 2 + 7 / 3
expressed with the Anglo-Saxon notation
2.3333333333333335
5
Order of operations
Operators with the same priority are evaluated from left to right, except
exponentiation, which runs from right to left
Example:
>>>2**3**2
???
Formatting numbers
The format function allows number formatting. It has two arguments: the
number to be formatted and the format specifier: format (value, format_spec)
Examples:
>>> print(format(10000/7, '.2f'))
1428.57
>>> print(format(10000/7, ',.2f'))
1,428.57
>>> print(format(10000/7, ',.2%'))
142,857.14%
• .2 indicates that we want to round off the number to the second decimal place
• f specifies that the number that you want to obtain is a floating-point
• the “,” (comma) adds the thousands separator
• the % symbol is used to format in percentage
7
Built-in functions
• These are the “default” functions, existing in Python standard installation
• In this lesson we will see print, format, help, input
• The standard syntax of a function in Python provides the name and
arguments in parentheses, separated by commas when they are more than
one: fname (arg1, arg2, …)
8
print function
Let's start by understanding the syntax of a function and the IDLE color codes
Purple for built-
in functions
Green for
strings
Blue for
output
Use of Function
separators syntax
9
They can be used interchangeably, except where the string contains quotes or
quotation marks, so the other character must be used to delimit it
Examples:
10
Operations on strings
Normally it is not possible to perform operations on strings, even if their
content resembles a number
Example:
>>> print ('Python' * 4)
PythonPythonPythonPython
11
Escape codes
• Escape codes are formed by a backslash (\) and other characters, placed
within a string
• They are not displayed in the output but give specific commands
• E.g.: the code \n is used to wrap text, \t aligns to the next tab, \ alone wraps
the code
help function
Shows information about a function, a data item, or a form
13
Examples:
14
Variables
This is a fundamental concept in all programming languages:
Examples:
last_name = 'Snow'
age = 25
15
Example:
16
Assignment statements
An assignment statement creates a new variable and gives it a value:
>>> surname_professor = 'Wilson'
>>> number_students = 86
>>> g = 9.81
The variable name is always on the left, while on the right there must be the
assigned value
Variable names
Variable names:
• must be short and meaningful
• must start with a letter or the underscore character (_), followed by letters,
numbers or underscores
• cannot contain spaces or special characters, such as @, &, $, €, § etc.
• cannot consist of reserved Python words (keywords), such as True, not, or, return,
while, and, if or function names
Reassignment of variables
• Variables can refer to different values during the execution of a program
• When you assign a value to a variable, this refers to the value until a
different value is assigned
• Reassignment is the operation of replacing one value with another
Example:
>>> a = 1
>>> print(a) Assignment
1
>>> a = 2
>>> print(a) Reassignment
2
19
Updating variables
One of the most common forms of reassignment is an update, where the new
value of the variable depends on the previous one
Example:
>>> a = 1
>>> a = a + 1
>>> print(a)
2
Multiple assignment
Python allows multiple assignment of variables (also called unpacking),
which allows acting on multiple variables directly on the same line of code:
>>> a, b, z = 1, 2, 3
>>> print(a)
1
>>> print(b)
2
>>> print(z)
3
21
Data types
• When a given value is assigned to a variable, it is typed, that is, it assumes a
certain type of data depending on the assigned value
• The data type determines the behavior of some operations, while others can
only be performed on values of a specific data type
• To know which type of data a value belongs to, you can use the type
function
'SEDIN', 'Python',
String str Used to represent text
'Python3', 'Web 2.0',
23
Note that:
• if the conversion is impossible, an error message appears. E.g.: int ('Python')
• int function does not round the fraction part, but chops it off
input function
• The input function asks the user to enter some data (i.e. an input)
• Once the user has typed a value and pressed the Enter key, he reads the
data entered from the keyboard and returns this data to the program as
a string
• It can be used to assign a value to a variable
Example:
26
Example:
>>> value = input('Enter the desired value: ')
Enter the desired value: 127
>>> value >>> value = int(input('Enter the desired value: '))
'127' Enter the desired value: 127
>>> value = value + 1 >>> value
Traceback (most recent call last): 127
File "<pyshell#16>", line 1, in <module> >>> value = value + 1
value = value + 1 >>> value
TypeError: must be str, not int 128
27
• The shell is useful to start working with Python and to immediately check
the operation of a command
• The editor allows writing a program in Python, that must be saved as a file
to be run
28
Writing a program
To write a new program choose
File/New File from the shell menu:
The output
is shown
in the shell
32
Alongside individual
lines of code
33
When we write a more complex program in the editor, the error could only
occur when we run the program, preventing it from
running. At that point it is necessary to look for the
bug inside the code: therefore we talk about
debugging
34
Book references
Learning Python:
Chapters 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3, 4
Assignment:
Exercises of lesson 10