UNESCO UNITWIN
Online Course
Supported by
Handong Global University
Ministry of Education, Korea
* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational materials including the video and other
relevant materials for commercial and for-profit use.
Variable Exercise
05
Global Leadership School
Handong Global University
Learning Objectives
• Learn what is Expression, Statement, and
comments
• Understand characteristic of Data Types
• Convert Data Types
• Practice using Data Types
Expression and Statement
• Expression
• Combination of value, variable, and operator.
• “Value” is considered as expression
>> 5+3
>> “Handong University”
• Statement
• Units of code that can be executed by the python
interpreter.
>> age = input(“Enter your age“)
Comments (1/2)
• In Comments,
• Used to store program author, date, and behavior
• It is not compiled along with the program
• Used to help the programmer understand the
code better
• Single-line comment
• Starts with hash (#) character
• Entire line is saved as comment
• Multi-line comments
• Begins with ’’’ , ends with ’’’
Comments (2/2)
• Python grammar does not matter in
comments
• Single-line comment
• # this code is adding two integers
• # 2100/5/6 by Joseph
Multi-line comments
’’’ this code is adding two integers
2100/5/6 by Joseph
2100/10/22 updated by John
’’’
Data Type Conversion
• Depending on the purpose, the data type
may be converted.
• Following statement is used to convert Data
Type
• int(variable name)
• float(variable name)
• str(variable name)
• Data Type Conversion is temporary
• To keep using converted type, simply assign it to
a variable.
When to convert Data Type
• Receive number using Input() statement
• As, the input data is stored as a string,
• Data Conversion is needed to do math operations.
Exercise 1
• Store your name in a variable named 'name'
and your age in a variable named 'age’.
• Then, Output the values stored in the two
variables as follows:
My name is Esther, and 19-years old.
Exercise 1 Code
Exercise 2
• Get the user's name and store it in a variable named
'name’.
• Get user’s age and store it in a variable named ‘age’.
• Then, Output the values stored in the two variables as
follows:
My name is Mattie, and 21 years old
Exercise 2 Code
Exercise 3
• Store the name of the organization in the
string variable 'affiliation'.
• Store the entrance year in the integer variable
'year'.
• Print as follows:
>>>
I entered Handong Global University in 2015.
Exercise 3 Code
affiliation = str(‘Handong Global University')
year = ‘2015’
print(”I entered", affiliation, ”in", year)
Exercise 4
• Receive two float numbers from the user and
print the sum of the two float numbers.
• Decide the type of the variables.
Exercise 4 Code
number1 = float(input(‘Enter the first float number: '))
number2 = float(input(‘Enter the second float number: '))
print(number1 + number2)
Exercise 5
• Receive an integer and print out “*” as many
as inputted integer.
Exercise 5 Code
Exercise 6
• Receive the user’s favorite number, color, and
season.
• Combine favorite color and season,
• And print out as many times as the favorite
number
• For example, if the input are 3, green, summer,
print out as follows:
• greensummergreensummergreensummer
Exercise 6 Code
num = int(input(”Favorite number: "))
color = input(”Favorite color: ")
season = input(”Favorite season: ")
cs = color + season
print(num*cs)
Lecture Summary
• Writing comments
• Data Type Conversion
• Practice using variable using exercise
• Print and input statements.
• Convert inputs to integer data type.
Practice Problem 1
• What symbol do you use to write one-line
comment?
• ‘
• #
• //
• []
Practice Problem 1 Answer
• What symbol do you use to write one-line
comment?
• ‘
• #
• //
• []
Practice Problem 2
• Choose the most appropriate expression for
the following underlined areas.
num= input(‘Enter an integer :”)
num = _____________________
print(“=“ * num)
• bool(num)
• int(num)
• float(num)
• str(num)
Practice Problem 2 Answer
• Choose the most appropriate expression for
the following underlined areas.
num= input(‘Enter an integer :”)
num = _____________________
print(“=“ * num)
• bool(num)
• int(num)
• float(num)
• str(num)
Thank you
05 Variables Exercise
Contact Info
unitwinlms@handong.edu
https://ecampus.handong.edu/
* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational
materials including the video and other relevant materials for commercial and for-profit use.
CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik.