04 Variables and Memory
04 Variables and Memory
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.
Variables and
Memory
04
Global Leadership School
Handong Global University
Learning Objectives
• Concept of a Variable
• When to use a variable
• Learn different types of a variable
What is a Variable?
• Used when program needs to store
• Programmer names the variable to store data
• Hardware memory is assigned when a variable is
created
• It stores data
• 2 is an integer, 3.14159 is a float.
• “Hello, World!” is a string.
• It can store different types of data.
When to use a Variable?
• When program needs to store something
• Count how many times the statement was
executed.
• Save user input.
• Save intervals when printing a number at regular
intervals
• When saving by specifying specific conditions
• When saving how many times loop iterations ran.
Variable Name
• Letters and Numbers can be used
• Variable name must start with a letter
• Allowed Variable Name
• myname, name, age, height
• price_of_tea, student_no, identification_code
• 나이, 이름, 소속기관
• Korean letter can be used.
• However, it can’t be used when used with other computer
languages
• Incorrect Variable Name
• 100_name, class, break, False
Reserved Words
• Following keywords are “Reserved Words” as
it is already used by Python grammar
• It can’t be used as Variable Name
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
Variable Example 1
>>> fred = 100
>>> print(fred)
100
>>> print(‘fred’)
fred
>>> fred = 200
>>> print(fred)
200
Variable Example 2
>>> value = 100
>>> num_coins = 15
>>> num_coins * value
1500
>>> 15 * 100
1500
>>> 15 * value
1500
Variable Example 3
>>> score = 78
>>> message = “John”
>>> print(message) >>> print(message , score)
John John 78
>>> print(“Hello!”, “John”) >>> print(“message” , “score”)
Hello John message score
>>> print(“Hello!”, message)
>>> print( “abba” * 3)
Hello John
abbaabbaabba
Variable Data Types
Characteristic Data Type Explanation
• Types of variables
• Integer
• Float
• String
• Boolean
• List
Practice Question 1
• Which of the following description does not
fit for a variable?
• It is named by the programmer
• It is allocated by hardware memory.
• It can only represent limited data types.
• Used when program needs to store
Practice Question 1 Answer
• Which of the following description does not
fit for a variable?
• It is named by the programmer
• It is allocated by hardware memory.
• It can only represent limited data types.
• Used when program needs to store
Practice Question 2
• What is the output?
• message
• ‘message’
• John
• ‘John’
Practice Question 2 Answer
• What is the output?
• message
• ‘message’
• John
• ‘John’
Thank you
04 Variables and Memory
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.