This is the first of a series of python
lectures, so it will be very simple and detailed, but the idea is that we want to create the following attending to the particular needs of the group, so suggestions and comments are welcome if there is any.
As we cannot write binary, we use a different Language!, and translators to the machine language.
One can divide the programming languages in two big families,
and the two of them have huge differences!.
Generally compiled languages are way more dificult to write than interpreted laguages.
That is because the computer must translate the codes into binary operations so that they can be performed.
But sometimes the efort worths it!.
#include<iostream>
int main(void){
std::cout<<"Hello World"<<std::endl;
return 0;
}
print("Hello world")
There are two different strategies to perform this translation
- Translate the complete file (code).
- Translate the code line by line.
This task can is the difference between the compiled and interpreted languages.
Which is which?
Python
can be used as an interpreter, in the sense that makes a translation line by line of our code, so we can run one intruction at a time in the same way we did with the shell
, to do so open python
Note : We are going to use
python3
instead of justpython
Today, the latest version of python isPython 3.7.2
, so we are not going to usepython
(which meansPython 2.7
).
The main reason for that, is because Python 2.7.8 was released on July 1, 2014.
is too old!!, meanwhile Release Python 3.7.2 was released on Dicember 24, 2018.
Invoke python
3 by typing
python3
on the terminal
2+3
5
2.0+3.0
5.0
2.+3.
5.0
It is also possible to save values in variables to operate them
a=3
b=2
print(a+b,a-b)
5 1
There are two different classes of operators on any programming languages,
- Arithmetic
- Comparison
- Logical
What do you think they are?
To understand how the operators work, we have to study a bit deeper what a variable mean.
a=2
print(a)
2
The value 2
is saved in a
.
a
has a specific physical place on the memory of the computer, so that every time we type a
the computer goes to that particular place and reads the value.
As the computer doesn't understand the number 2
but binary instead, there is a huge difference if we use
a=2
b=2.0
What do you think is the difference?
To test them, let us explore some operators such as +
, -
, *
, /
, **
a+b
4.0
a+a
4
NOTE: On different versions the python
, you get different results.
1/a
0.5
1/b
0.5
Let us use the method type
type(a)
int
type(b)
float
which means that, a
is an integer, while b
can have decimals
This have a huge impact but depends on the python
version we are using, on different languages such as C++
, you have to say which kind of variable you want, for instance
int a=2;
float b=2.0;
double c=2.0;
where float
and double
are data types that allow decimal points.
But, there are some other data types, one of great interest, they are called boolean
.
boolean
varialbes only have two different possible values True
or False
.
Depending on the language, they can be written differently, for example, in languages such as c++
and julia
, the possible values are written in lower case.
C++
bool test=true;
julia
test=true
R
test<-TRUE
While in python the first letter must be upper case,
test=True
Where do you think we can find bools?
a>b
False
a<b
False
Sometimes we will need to have a combination of conditions to satisfy on a particular problem,
For example, the set of people on our course is different if we ask for
Female and prefer dogs to cats.
Female or prefer dogs to cats.
Think about the difference.
On other languages is common to use !
,||
,&&
for negation, or and and.
python
is way simpler, it uses
!
for negation.or
for or.and
for and.a=True
b=False
a and b
False
a or b
True
a=2
b=3
how can I ask if
a
andb
are the same?
a==b
False
a==2.0
True
a>=b
False
You guys should take a look at Truth tables.
Now, we are ready to start working on a little bit more complex structures, such as control statements
if
statement¶This is used for running some part of the code, only if a condition is satisfied.
python
is based on identation rather than characters to the control statements such as other languages can.
A python
structure of an if
structure goes as
if condition:
inside
outside
if the condition results to be True
the inside part is executed, let see some examples.
if True:
print('Inside of If')
print('Outside of If')
Inside of If Outside of If
if False:
print('Inside of If')
print('Outside of If')
Outside of If
a=1
b=2
if a>b:
print('a is greater than b')
else:
print('b is greater that a')
b is greater that a
but, what if a=b?
a=1
b=1
if a>b:
print('a is greater than b')
elif a<b:
print('b is greater that a')
else:
print('b is equal to a')
b is equal to a
elif
holds for else if
for
statement¶The for
is one of the loop structures that can be used on python
, when a procedure must be repeated
Again, we have to be careful with the indentation, so that
suma=0
for i in range(101):
suma=suma+i
print(suma)
5050
The for
structure, have the huge advantage compared with languages such as C++
and fortran
, because of it allows to iterate on the compounds of a data type called list
s.
A list
is a set of things on python, the most important thing here is that one can use any kind of thing inside a list, such that
list
sstrings
int
or float
The only thing we have to consider is to make it inside of []
, let see some examples
list1=[]
print(list1)
type(list1)
[]
list
list2=[10]
print(list2,type(list2),type(list2[0]))
[10] <class 'list'> <class 'int'>
list2=[1,2,3,4,5]
print(list2*2)
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
for
¶for i in ['value1', 'value2']:
print(i)
value1 value2
list3=[1,2,3,4,5,6,7,8,9,10]
for i in list3:
print(i)
1 2 3 4 5 6 7 8 9 10
Then one can have more than one structure inside another, let see some examples
for i in range(5):
if i == 2:
print('Inside first if')
elif i ==3:
print('Inside elif')
print('Inside for on loop: ',i)
Inside for on loop: 0 Inside for on loop: 1 Inside first if Inside for on loop: 2 Inside elif Inside for on loop: 3 Inside for on loop: 4
for i in range(10):
if i%2==0:
print(i)
0 2 4 6 8
Next session, we will focus on string
s and list
s structures.