CMPUT 101
Introduction to
Programming in Python
Marianne Morris
marims@ualbert
a.ca
How to use the audio
and
the slide animations
Please turn up your device speakers
together
and run the slides in presentation mode
Click on the play button of the audio
icon as soon as a new slide is up
The play button of the audio icon will
appear when you wave the mouse over
the audio icon and will look like the
following picture:
How to use the audio
and
the slide animations
Slide animations run automatically in a timed
together
sequence regardless of whether the audio is
played or not
However, the sequence of animations will time
well with the audio explanations if you play the
audio immediately after a new slide is up
Do not click the mouse and do not press the
down arrow key while the audio is playing, in
order not to skip the sequence of animations
timed with the audio
Intro to Programming
Algorithm
Step by step list of instructions to
solve a problem or complete a task
Program
Implementation of an algorithm
Programming language
Languages used in writing software
programs like Python 3
Recap
In the first lecture, we have
implemented these two simple
programs:
print("Hello, World!")
print("My program adds two
numbers: 2+3 = ", 2 + 3)
Lecture goals
In this lecture, we will learn:
Values and Data Types
Variables
Statements and expressions
Operators, operands, and precedence
Input
Reassignment
Values and Data Types
A value is like a word (string) or a
number that a program
manipulates.
The values we have seen so far are
the integer 5 (when we added 2 +
3), and the string "Hello, World!".
Values are referred to as objects.
Data types
Strings
Names or values; e.g., “Daniella”
Integers
Whole numbers: e.g., -3, 0, 78, 105, etc.
Real numbers:
Fractional values: e.g., 3.14 or -2.25, etc.
Boolean:
True or False
Statements
A statement is an instruction that
the Python interpreter can execute.
The following is an assignment
statement:
firstName = “Daniella”
This statement stores the name
“Daniella” in a variable (a physical
location in memory labelled firstname)
firstname
Assignment Statements
Assignment statements use the
assignment operator
the equal sign ( = )
In this statement: firstName =
“Daniella”
This equal sign is not for equality.
It is an operator that assigns the string to a
variable (a location in memory).
Statements
In the upcoming lectures, we will
learn many other statements
including:
While statements
For statements
If statements
Important statements
Expressions
An expression is a combination of
values, variables, operators, and
calls to functions.
e.g., print(2+3)
Expressions need to be evaluated.
The interpreter evaluates the
expression and displays the result.
Operators
Arithmetic
+, -, *, /, //, **, %
Relational
<, >, <=, >=, !=, ==
Boolean
not, and, or
Arithmetic operators in
Python
Addition +
Subtraction -
Division
Integer //
Real numbers /
Multiplication *
Remainder %
Exponent **
Order of Operations
print(2 + 3 * 5)
Is this statement going to print:
25 or 17 ?
Which operator has higher
precedence, the + or the * ?
Precedence of Arithmetic
Operators
Order of operations – Precedence
Parentheses have the highest precedence
Exponentiation has the next highest
precedence
Multiplication and division have the same
precedence, but higher than addition and
subtraction
Operators with the same precedence
(except for **) are evaluated from left-to-
right
Order of Operations
print(2 + 3 * 5)
This statement evaluates the
multiplication first: 2 + (3 * 5) = 2 +
15.
What if we want to evaluate (2 + 3)
first? We should bracket it:
print( (2 + 3) * 5)
The result is 5 * 5 = 25.
Variables
How do we store these various data
types into the computer memory?
A variable can hold one value at a time
firstName = “Daniella”
Assigns the string Daniella to a location in
memory labeled as firstName
myNumber = 3
Assigns the integer 3 to a location in
memory labeled as myNumber
Reading Input
We have seen how we can output
a string or a number using
print(…)
How do we read input from a user?
Python provides us with the input(…)
built-in function
Reading input strings
print("Enter your first name:")
firstName = input()
print("Enter your last name:")
lastName = input()
print("Your name is: ", firstName,
lastName)
Outputs of the strings
program
Enter your first name:
Mary
Enter your last name:
Joe
Your name is: Mary Joe
Reading input numbers
print("Enter a first number:")
firstNumber = input()
print("Enter a second number:")
secNumber = input()
print("The sum of the two numbers: ",
firstNumber + secNumber)
Outputs of the sum
program
Enter a first number:
3
Enter a second number:
4
The sum of the two numbers: 34
Why is the output 34 and
not 7 ?
Converting Input to Data
Types
The input(…) function is a built-in
function that reads input as a
string
Inputs must be converted to the
proper data type
We can convert the input to an
integer by using the conversion
function int(…)
Reading Input Numbers
(the correct program)
print("Enter a first number:")
firstNumber = int(input())
int
print("Enter a second number:")
secNumber = int(input())
int
print("The sum of the two numbers: ",
firstNumber + secNumber)
Converting the input to a
floating number
print("This program adds two numbers for
you:")
print("Enter your first number")
firstNum = float(input())
float
print("Enter your second number")
secNum = float(input())
float
print("Your total is:", firstNum + secNum)
Conversion Functions
int(…)
converts the data to an integer
float(…)
converts the data to a float
str(…)
converts the data to a string
Check
What’s the output of the following
statement?
print( int(55.785) )
Output:
55
The int(…) function truncates all values after the
decimal
and prints the integer value.
Variables
Locations in memory to store data
Data can be of any type
An assignment statement will
store data into a variable
number = 3
A reassignment will update this
location in memory with new data
number = 7
Variables
number = 3 Outpu
t:
print(number) 3
7
number = 7
print(number)
Variables
An assignment statement can make
two variables refer to the same
object and therefore have the same
value
They appear to be equal
But they don’t have to stay that way
Reassignment can change their
values
Variables
first = 3
Outpu
second = first t:
3 35 3
print(first, second)
first = 5
print(first, second)
Variables
After the following Answer
statements, what x is 22 and y is
15
are the values of x
and y?
x = 15
y=x
x = 22
Variables
Updating variables:
x=x+1 # increments x by 1
i=i+1 # increments i by 1
y=y–1 # decrements y by 1
j=j–1 # decrements j by 1
Common use of variable updates
as counters in programming
Variables
Updating Outpu
variables: t:
2 73 6
x=2
y=7
print(x, y)
x=x+1
y=y-1
print(x, y)
Variables
What is printed when Answ
the following er
15
statements execute?
x = 12
x=x-3
x=x+5
x=x+1
print(x)
Variable names
Cannot contain spaces
Cannot be a keyword from the
Python programming language
Cannot start with digits or contain
special characters
Examples of illegal variable names:
Section 2.5 of the interactive e-text
Printing results of arithmetic
operations using variables
quotient = 7 // 3
Output
print(quotient) s:
remainder = 7 % 3 2
1
print(remainder)
The variable quotient stores the result of the integer
division
of the two operands 7 and 3.
The variable remainder stores the remainder
resulting from
the integer division of the above two operands.
Summary
Data types (string, int, float,
boolean)
Variables
memory locations that store values
Proper vs. illegal variable names
Conversion functions
Operators and their precedence
Built-in functions (print, input)
Errors (syntax, runtime, logic)
Text readings
In this lecture, we have covered
Chapter 2 of the e-text
Read the e-text
Do the activities and exercises to
better retain the new
programming concepts