05 Variables Exercise
05 Variables Exercise
Online Course
Supported by
* 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:
>>>
I entered Handong Global University in 2015.
Exercise 3 Code
year = ‘2015’
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.
cs = color + season
print(num*cs)
Lecture Summary
• Writing comments
• Data Type Conversion
• 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
[email protected]
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.