L3a- basic
L3a- basic
1
Basic elements in Python
• A program in Python is a sequence of definitions and
statements:
– Definitions are evaluated
– Statements are executed by Python Interpreter (Shell)
Definitions
Statements
Indentation
2
Synth elements of a program in Python
... and the instructions for printing on the screen and reading from the keyboard ?
3
Advantages of Python
• It is a very portable language, that is, it is independent of
the architecture of the machine. Python programs run on a
virtual machine.
• Its grammar is elegant. It allows writing clear, concise and
simple code, and develop a lot of use cases in an elegant
and natural way.
• Large set of functions in the standard library and lots of
external projects with specific functionalities.
• Its interface with C, C++ and Java facilitates the
connection between heterogeneous programs.
4
Synth elements of a program in Python
They are not part of the kernel, but they are a fundamental part of language:
5
Modules
matplotlib
__buildins__ Default Modules Data View
pillow
csv
pandas
xml Kernel Data analysis numpy
Read Write
json Language scipy
zipfile
time
urlib
random
Other Web interaction
BeautifulSoup
math
sys
6
Variables
7
Memory organization
Variable
name Address Storage space (1 byte) 1 byte = 8 bits
pi 1
2 3.14
3
n 4
5 2
6
7
pi = 3.14
n = 2 8 3.141592653
pi = 3.141592653 9
So many positions
as memory has
65535 the computer 8
Types
• Each variable has a type depending on the data it stores.
9
Names of variables
• In Python, a variable is just a name.
• An assignment statement associates the name on the left of = with the
object denoted by the expression on the right of =.
• A value can have one, more than one or no names associated with it:
10
Names of variables
Rules:
• Valid characters: letters (a.. z and A.. Z), digits (0-9) and underscore ( _ ).
• Any other character is not allowed.
• First character: a letter or underscore. Cannot be a digit.
• Python's reserved words must not be used.
• Case sensitive:
area ≠ Area ≠ AREA
Examples:
CircleArea_1 Circle#Area
x1 X:
ThisIdentifierIsValid ThisIsNotV@lid
11
Constants
Constants: In many languages, values that do not change during
execution are defined as constants.
PI = 3.141592
12
Comments
• A good practice to facilitate understanding the code is to add
comments.
• Text after character # is not interpreted by Phyton:
"""
Computes the area of a circle
Version 27-12-2020
"""
PI = 3.14159
radius = 11.2
area = PI*radius**2 13
Function calls
14
Data output
The standard output is usually the screen.
print() within the parenthesis we put what we want to print on the screen.
To print more than one item, we separate items with commas (,):
15
Data input
The standard input is usually the keyboard.
input() is a function that returns a value (the one read from the keyboard).
Therefore, we must assign it to a variable:
17
Expressions
An expression is essentially a formula that allows calculating a value.
We do not need to have all the elements to have an expression; a single value
is also considered an expression
3.0 + 1.5
has type float.
18
Operators and operands
Arithmetic operators
Type of
Operation Symbol Examples
operands*
Addition + int, float 5+3à8
Subtraction - int, float 5–3à2
Product * int, float 5 * 3 à 15
Division / int, float 10.0 / 3.0 à 3.333
Integer division // int, float 10 / /3 à 3
Modulus % int, float 10 % 3 à 1
Power ** int, float 10**2 à100
* If the two operands are int, the result is int (except for division, which returns float).
If any of them is a float, the result is float.
19
Assignment operators
Assignment operators
20
Precedence of operators
– Next in precedence, we have addition and subtraction, with the same priority.
– Operators with the same priority are evaluated from left to right.
21
String Type (str)
• A string is a sequence of characters.
• How is it stored?
characters
H e l l o w o r l d
indices 0 1 2 3 4 5 6 7 8 9 10 11
• Operator [n:m] returns the part of the string from the n-th character
(included) to the m-th (not included):
22
ASCII Code
The ASCII code is a Latin-based character code. It was created in 1963 by the
American Committee on Standards (source: Wikipedia).
The most interesting values for us are the letters "a" and "A", and the digit "0".
These characters have codes 97, 65 and 48 respectively.
The interest in choosing these comes from the fact that the entire lowercase
alphabet comes after letter "a", so "b" has code 98, "c" has code 99, and so on
until "z", which has code 122.
The same happens for capital letters: "B" is 66, "C" on 67, and so on until "Z",
which is 90.
23
ASCII Code
On the keyboard:
ALT + Num ® character from table
Example: ALT + 169 ® ©
Python
print(chr(65)) ® ‘A’
print(ord(“A”)) ® 65
24
Operators and strings
• In mathematics it makes no sense to operate with strings, although
sometimes the strings look like numbers (e.g. "17"):
”17” + 1
• If we open our minds:
– what could it mean to add two strings? à Concatenate them
– what could it mean to multiply a string by an int? à Repeat it
Editor
NO
Done?
YES
Py
Execution Py YES
Error?
NO
26
Understanding Error Messages
• When Python finds syntax mistakes or other errors in your code, it
gives you error messages to help you figure out what the problem is
• Usually include these types of information:
– The filename
– The line of code where Python first figured out there was an error
– The kind of error
27
Errors related to language
In programming there are two type of errors:
28