You are on page 1of 14

INTRODUCTION TO

COMPUTING SCIENCE AND PROGRAMMING


Lecture 2.2: Data Types – Continue

CMPT 120, Spring 2023, Mohammad Tayebi


Class Agenda

• Today
• Last Time
• Python • In-class Exercises
• Data types • Software Engineering
• Variable • Piazza Challenges
• Operators
• Input function

• Reading
• cspy ch. 1 & 2

CMPT 120, Spring 2023, Mohammad Tayebi 2


Order of Operations

• Rules of precedence in case of having more than one operator in an expression


1. Inside of parenthesis
2. Exponentiation
3. Multiplication/division/remainder
4. Addition/subtraction
5. Assignment

• String constants
• Anything inside of single quotes or double quotes, and + for strings is concatenation

>>> print("What " + "is " + "your " + "name?")


What is your name?
CMPT 120, Spring 2023, Mohammad Tayebi 3
In-class Exercise 8

• What is the output of the following program?

>>> a = 1
>>> b = 3
>>> c = 6
>>> d = a + (b + c) * 3
>>> print(d-2)
1. 14
2. 26
3. 28
4. 33
CMPT 120, Spring 2023, Mohammad Tayebi 4
In-class Exercise 9

• What is the output of the following program?

>>> a = 2
>>> b = 3
>>> c = 1
>>> a = b + a**2 * 3 - (b%c*a)
>>> print(a)
1. 10
2. 12
3. 15
4. 23

CMPT 120, Spring 2023, Mohammad Tayebi 5


Reading User Input

• The input() function take the user's input


• Return it as a string value.
• Even if you ask for a number input() returns a string.
• Your responsibility to convert it.

name = input (“Enter your first name: ")

CMPT 120, Spring 2023, Mohammad Tayebi 6


In-class Exercise 10

• Write a program to ask the user what day of the week is today and
print the answer.

#this is a program to ask and print what the day of the week is today.
day = input ("What day of the week is today?")
print("Today is " + day)

CMPT 120, Spring 2023, Mohammad Tayebi 7


In-class Exercise 11

• Write a program to ask the user age and print it.

#this is a program to ask and print the age of users.


age = input ("How old are you? ")
print(age)

CMPT 120, Spring 2023, Mohammad Tayebi 8


In-class Exercise 12

• Write a program to ask the user first name, last name and country and
print this information in the following format:
Justin Trudeau is from Canada.

#this is a program to take user information and print it.


first_name = input ("Insert your first name: ")
last_name = input ("Insert your last name: ")
country = input ("Insert your origin country: ")
print(first_name + " " + last_name + " is from " + country + ".")

Alternative way
print(first_name, last_name, " is from " + country + ".")
CMPT 120, Spring 2023, Mohammad Tayebi 9
In-class Exercise 13

• Write a program to take number of minutes from the user and covert
it to seconds.

#this is a program to covert minute to second


minute = input("Insert the time in minute: ")
second = int(minute) * 60
print("The converted time in seconds is " + str(second))

CMPT 120, Spring 2023, Mohammad Tayebi 10


In-class Exercise 14

• Write a program to take number of minutes and hours from the user
and covert the total it to seconds.

#this is a program to covert total time in hour and minute to seconds.


hours = input("Insert the time in hour: ")
minutes = input("Insert the time in minute: ")
seconds = ((int(hours) * 60) + int(minutes)) * 60
print("The total time in seconds is " + str(seconds))

We used extra parenthesis only


to improve the code readability.
CMPT 120, Spring 2023, Mohammad Tayebi 11
In-class Exercise 15

• Write a program to take number of days, hours and minutes from the
user and convert the total time to seconds. We used extra parenthesis only
to improve the code readability.

#this is a program to covert total time in number of days, hours and


minutes to seconds.
days = input("Insert the time in day: ")
hours = input("Insert the time in hour: ")
minutes = input("Insert the time in minute: ")
seconds = (((int(days) * 24 + int(hours)) * 60) + int(minutes)) * 60
print("The total time in seconds is " + str(seconds))

CMPT 120, Spring 2023, Mohammad Tayebi 12


In-class Exercise 16

• CAD to Euro exchange rate is 0.7. Write a program to covert a value in


CAD to Euro for the user provided input.

#this is a program to covert CAD to Euro


value_cad = input("Insert the amount in CAD: ")
value_euro = float(value_cad) * 0.7
print("The converted value is " + str(value_euro) + ".")

CMPT 120, Spring 2023, Mohammad Tayebi 13


Next Lecture

Branching & Iteration


Pre-reading: cspy ch. 4 & 7

CMPT 120, Spring 2023, Mohammad Tayebi


14

You might also like