2.basic Data and Operations
2.basic Data and Operations
operations.
Variables and conditionals.
Python’s documentation:
https://docs.python.org/3/
GOOGLE in general!!!
Talking with the
computer print(“message”)
Print a message
Let’s try it
02-01-Hello_world.py
In short...
Variables ●
●
Integer numbers: int
Float numbers: float
● Strings: str
They store the data
● Boolean: True or False
Variable. Previous example
with variables:
message = “Hello world”
print(message)
Variables in Python
Read/store a value
Let’s try it
02-02-Hello_name.py
In short...
● Print can print several values
○ print(“Hello”, name)
● Strings can be concatenated
○ message = “Hello” + ” world”
Tip
Tell the audience about Remember...
the problem through a
story, ideally a person.
● addition/subtraction: +, -
multiplication/division: *, /
Numbers
●
● exponent: **
● integer division: //
The value is used/assigned
directly Relational operators
days = 365
hours = 24
minutes = 60
seconds = 60
leap_year = (days+ 1) * hours * minutes * seconds
print(leap_year)
31622400
Operations with stings
● greater/less: <,<=,>=,>
● equal: ==
● not equal: !=
Casting between types
For example, to cast a number into a string
age = 30
age_str = str(age)
And to cast it again to int
age_int = int(age_str)
Different conversions
Tip
Tell the audience about
the problem through a
story, ideally a person.
Functions
Until now, we used two functions:
● input()
● print()
Function Many “synonyms”:
An independent piece of code ● Procedure or subprogram,
that performs a specific task mainly. Methods are also very
similar
Calls to functions
abs() abs(“a”)
abs(1,-3)
Output:
Output:
TypeError: bad operand type for abs(): 'str'
TypeError: abs() takes exactly one
argument (0 given)
TypeError: abs() takes exactly one
argument (2 given)
Parenthesis are
compulsory!
print(abs)
Output:
<built-in function abs>
def hello(name):
functions
message = hello(“Amaia”)
print(message)
Till now... Is it
raining
?
We have executed operations in order,
False True
from up to bottom. Everytime the
same will be executed.
Go to work Go to work
But most of the times... walking by bus
Assignation Comparison
● It is used to assign a value to a ● It is used to compare two
variable values (it can be the value of
● A single = is used variables as well)
● For example, ● A double = is used
● For example,
name = “Olatz”
age = 33 if name == “Olatz”:
print(“Kaixo Olatz”)
if name != “Olatz:
print(“What’s your name?”)
Booleans
The result of a comparison will be
a boolean value:
True or False
Let’s try it
02-09-booleans.py
In addition to compare equal (==)
and not equal (!=), it is very common
to compare greater and less than
with numbers ●
x<0
Less or equal than: <=
x <= 10
As done with strings, we can
● Greater than: >
write conditionals with numbers
x>7
● Greater or equal than: >=
x >= 5
Let’s try it
02-10-booleansNumbers.py
Nested conditionals
if x > 0:
if x < 10:
print(“It is a number of one digit”)
else:
print(“It has more than one digit”)
else:
print(“It is a negative number”)
Let’s try it
02-11-nestedConditionals.py
Conditionals with many options
if x > 0: if x > 0:
print("Positive") print("Positive")
else: elif x < 0:
if x < 0: print("Negative")
print("Negative") else:
else: print("Zero")
print("Zero")
Let’s try it
02-12-manyConditionals.py
Want more?
http://www.parentesis.com