Type of data
and operators
High level programming
language: Python
Type of data and operators
In the previous exercises, you worked with variables...
Variable: a place located in the computer’s memory to store a value.
Then you can call it to retrieve the value stored.
And even though you can name it as you want, in Python you have to
follow certain rules:
a. The name can begin with a letter or with the symbol _
b. It cannot begin with a number or other symbol.
c. It cannot contains spaces.
d. A variable’s name is case sensitive.
Variables vs. constants
Variable: its value can change in a later
statement (instruction)
NOTE: You have to do all the exercises in this presentation. Either
in Spyder or in pythonanywhere.com.
Constant: it has a fixed value.
EXERCISE 1
Type of data and operators
A variable can begin with _ or has it in its
name.
A variable can’t begin with numbers or
contains symbols
A variable is case sensitive. number3 is
different from Number3
A variable can change its value. Before
_number1 had the value of 5, now it has
the value of 10
A constant is a fixed value
Type of data and operators
Reserved words in Python
Notice that the
The reserved words are special words program colors
that Python uses as functions. You can’t (purple) when you
name a variable using the name of a are using a reserved
reserved word since you will get an word to name a
error. variable.
RESERVED
WORDS in
Python
Type of data and operators
Types of errors you can find in Python
Syntax: When your program has an error in the
syntaxis, i.e. you made a mistake naming a
variable, you forgot to use an element in a
function like using parentheses in the print
function.
Logic: when in the program there is a mistake in
the order of the instructions.
Semantic: When the program has no errors either
in the syntax or in the order of the instructions,
but the program doesn’t do what you expect it to
do.
Type of data and operators
EXERCISE 2
Syntax error: The parentheses are
missing in the print function: print()
Logic error: You tried to do the addition
of 3 variables but you forgot to define the
value or the third variable (z) before.
Semantic error: You wanted to print 1
million but instead you got 100. Your
program doesn’t have a syntax error, it
remember that int number are without
worked ‘ok’, but it doesn’t do what you
commas. To print 1 million you use
expected it to do it.
print(1000000). When you use commas in
Why?
the print function, it understand three
values.
Type of data and operators
Concatenation y repetition of strings
Arithmetic operators
In the video 014-expressions, we
studied the use of arithmetic operators.
Two of them, the addition (+), and the
multiplication (*) can also be used with
strings:
+ To concatenate (join) two or more
strings.
* To repeat a string several times.
Type of data and operators
EXERCISE 3
Remember that you can use
double or single quotations marks
when you are working with strings.
The + operator is used to join
(concatenate) two or more strings.
For example, here, the strings
cheer was join with a blank space
and with the string country.
The string good_cheer is repeated
three times using the * operator.
Notice that the string is separated
each time by a space since there is
a space at the end of the string
However, pay attention that you country.
cannot mix double and single
quotations marks. Either you use “
or ‘
Type of data and operators
Operator precedence rules EXERCISE 4
What is the result of the following
P arentheses operations?
E xponentiation
M ultiplication
D ivision
A ddition
S ubtraction
why did we get those results?
Left to right
Type of data and operators
Use of the input function to ask for data, convert it to integer
Always when you use the input function,
the data entered is taken as string (text).
Therefore, if you need to work with
numbers, you need to convert the data into
an integer or a float.
Let’s see the following Exercise which asks
for a person’s name, the year he/she was
born and then shows how many years the
person has.
Type of data and operators
EXERCISE 5 Here you don’t need to convert the
data since you are asking for strings
(person’s name).
Here you must convert the data
(age) into integers since you will
work with numbers. That’s why you
got an error when you tried to do
the subtraction operation.
Here you convert the data into an integer.
Notice that you use the same variable age to
store the conversion. Remember that a
variable can change its value as you want.
Here you print the info. Notice that in a print
function you can print a lot of info, just use
commas to separate the info. And notice that you
put variables’ names without quotation marks.
Type of data and operators
EXERCISE 6
Let’s do other exercise. Using the input function and int() to convert the data to a number.
Here you convert the data into an integer.
Notice that you use the same variable
number1 to store the conversion.
Here we combine the two functions input()
and int() in one step.
Type of data and operators
Use of the input function to ask for data, convert it to integer
Remember from the video 014 –
expressions that you can you use the
function type() to know the type of data of
a variable (int, float, string).
This will help you in case you try to convert
string as names into integers as you will see
in the following exercise.
Type of data and operators
EXERCISE 7
Even though the variable number1
is a string (because the data was
enclose in quotation marks), you
can convert it into int since it
contains numbers.
Here you cannot do the conversion since the
variable number2 contains text (hello) instead of
a number.
This can happen when the user doesn't pay
attention to the instructions (Oh dumb users!!)
and enters text instead of numbers.
Type of data and operators
Comparison operators and function if
Comparison operators
Here, we have another type of
operators: comparison operators. You
use them when you want to ask a
question: Is this variable less than that
variable?
Something that you have to pay attention a lot is
regarding the comparison operator == and the =.
== is for asking a question. Is this variable equal to that
variable or value? For example, x == 2 (here you are
asking if x is equal to 2).
= is an affirmation. It is used as you have noticed to
assign a value to a variable, x = 2 (here you are claiming
that x is equal to 2)
Type of data and operators
EXERCISE 8
Pay attention. When you write the colons (:)
at the end of the function if and then when
you press Enter, the program add some
VS.
space at the beginning of the print function.
That space is called indentation. YOU HAVE
TO LEAVE THAT SPACE. DON'T ADD EXTRA
SPACES, DON'T ERASE THAT SPACE or your
function if won't work.
Type of data and operators
Logical operators
Have in mind that a condition is
Python Meaning something like a == 2 since you are
and TRUE if all the conditions are true asking if a is equal to 2.
or TRUE if at least one of the condition is
true
Notice that says if all the conditions, i.e. you can have
not negates an expression more that one condition like:
if a == 2 and b == 3 and c == 4 .... etc.
Type of data and operators
EXERCISE 9
Imagine you are doing a raffle and
you ask the user to say a number
between 0 and 10. Once the user
said it, you congratulate him/her.
Pay attention that here, the
number has to meet both
conditions: to be higher than 0 and
lesser than 10.
Notice that here, we made a
mistake. What was it? we forgot to
put the variable in the second
condition, i.e. and number <10.
Therefore we made a mistake in
the syntax:
if condition1 and condition2:
Type of data and operators
EXERCISE 10
Imagine you designing a security
system and you will allow entry to
a person called juan. The problem
is that you don't know if the person
will enter his name in lower case,
in capital letters or the first letter in
capital. So since there are some
alternatives to do it, you will take
into account some of them.
Notice that any alternative will
work (juan, Juan, JUAN), unlike
when using the and function that
you have to meet ALL the
conditions.
Type of data and operators
EXERCISE 11
Now the same exercise as the
previous but with the exception
you will restrict the entry to a
person called juan. All the people
with other name will be allowed to
enter. So in this case, you will use
the operator not.
Notice that that anyone other than
Juan, juan or JUAN will be
guaranteed access to the place