CS1-W1-TU-FALL2021
Maíra Marques Samary
Introduction
INPUT
INPUT
OUTPUT
In a computer we see the same
!
Interaction Model
• The interaction with our software will be using the
keyboard – INPUT
• Our programs will process input data and then print
(display) results on the screen – OUTPUT.
What data can be manipulated
in a program in Python?
?
For now, just three types of data
• Integer numbers (int)
• 12345
• Decimal numbers (float)
• 3.1416
• Strings (string)
• “Hello World!”
In Python 83562
are the same as “83562” ?
?
And 342.0 and 342 ?
?
• The function print allows to print text in the output.
• It can be invoked with a single data / variable, or with
multiple data / variables separated by a comma.
Type conversion operations
• To convert a string or an integer to a float, we use the
operation float()
• To convert an integer or a float to a string, we use the
operation str()
OPERANDS
OPERANDS OPERATORS
In a calculator the operands
are only numbers
!
In Python we have distinct types
of operand…
… And many operators
!
• So far, we have “bounced” the values of
the expressions we have calculated
• We can save our calculations in
variables:
• a variable is the name given to a value
or an expression
• the value of the variable is given by the
last definition of the variable
• What appears after the # sign is
comment, Python ignores them
• Valid names of variables:
combination of letters, numbers and symbols …
..as long as the first character is not a number
• Beware of uppercase / lowercase!
What the operator % does in Python?
A. Calculate percentaje.
B. Converts an integer value in a decimal value.
C. Calculate the reminder of an integer division.
D. Converts a decimal value in an integer value.
?
Which arithmetic operators can we use to obtain the last 3
digits of the number 53218?
A. Division(\)
B. Multiplication (*)
C. Module (%)
D. Division (\) y module (%)
?
What the operator ** does?
A. Lets you know if one number is a multiple of another.
B. Allows you to multiply fractions.
C. Allows you to calculate xy (exponentiation)
D. Allows you to multiply matrixes.
?
,
Syntaxes Errors
There is more parenthesis than needed
Variable allocation to the reverse
Name Errors
Calls a variable that not been defined yet
Type Errors
It happens when we try to apply operations on
types that are not compatibles.
Value Errors
The expression is well formed but the operation is
based on values that does not correspond to the
type
Indentation Errors
The space in the instructions are not consistent.
In Python the spaces is what delimitates many
things (next class)
input()
• The input() function of Python let us ask the user
for the data he/she wants to enter in the keyboard.
• All the data entered with input() is considered a string.
• You can enter a number, but it will not be considered
a number.
• If we ask the user for a number we need to convert it
to a number, either integer or double.
How do we write a program that decides what to do?
?
if
if what?
if Condition :
Condition: Logical
expression with two possible
values as answer
True
False
if Condition :
Condition
This ”code block” is executed only
if the condition is true
When the last line is executed (“print …)”?
?
A. Only when object is ”sky”
B. Always
When the last line is executed (“print …)”?
?
A. Only when object is ”sky”
B. Always
How to decide a color if the object is different from sky?
?
else
We could use:
Condition and if block (true)
else is executed always when
the true condition does not hold
Don’t forget the “:” !!
Don’t forget the “:” !!
if else
if and else are always disjoint: only one of the two
blocks is executed, never both, since the condition
of an if can not be True and False at the same time.
How do we expand the program to recognize
other objects?
?
else if
(elif)
?
What is the color of the sky
now?
A.blue
B.white
?
What is the color of the sky
now?
A.blue
B.white
In a structure if-elif-else only one condition is
evaluated (ALWAYS)
!
if, elif y else are always disjoint: more than one
case can not be executed (block of code)!
if
eli
f
eli
f
eli
f
els
e
?
¿De qué color es el cielo ahora?
A.azul
B.blanco
?
What is the color of the sky
now?
A.blue
B.white
?
¿De qué color es el cielo ahora?
A.azul
B.blanco
?
What is the color of the sky
now?
A.blue
B.white
Structure if-elif-else they can be written (and
executed) in succession.
!
This structures (if-elif-else) also can be ”nested”
(one inside the other)
!