0% found this document useful (0 votes)
10 views

04 Variables and Memory

The document is an educational resource from UNESCO UNITWIN that covers the concept of variables in programming, including their definition, usage, and different data types such as integers, floats, strings, booleans, and lists. It outlines the rules for naming variables, reserved words in Python, and provides examples of variable usage and operations. Additionally, it includes practice questions to reinforce understanding of the material presented.

Uploaded by

Thae Thae Aung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

04 Variables and Memory

The document is an educational resource from UNESCO UNITWIN that covers the concept of variables in programming, including their definition, usage, and different data types such as integers, floats, strings, booleans, and lists. It outlines the rules for naming variables, reserved words in Python, and provides examples of variable usage and operations. Additionally, it includes practice questions to reinforce understanding of the material presented.

Uploaded by

Thae Thae Aung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

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.
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

Integer Type int Positive number, 0, Negative number

Float Type float A number expressed as a real number,


-5.1234
String Type string Letters that are used to represent text,
“Handong Global University”
Boolean Type boolean Only True, False can be used

List type list Collections of multiple data objects


[ 1,2,3,4,5 ]
[‘apple’, ‘banana’, ‘citrus’, ‘lime’ ]
[‘kim’, 1, 10, ‘park’, 5.123]
Type of Variable, int
• Value
• –3, –2, –1, 0, 1, 2, 3, 4, 5, …
• Following are integer literal: 1, 45, 43028030
• No comma (for thousands separators) and period
(for decimal) are allowed.
• Operator
• +, –, *, /, //,**, Unary Operator–
• Rule
• When an operator is used with int type, the result
is int type
Integer Example 1

>>> price = 125 >>> price = price + 25


>>> price >>> price
125 150
>>> price * 4 >>> price * 10
500 1500
>>> price >>> price
125 150
Integer Example 2
>>> price = input(“Enter price : “) # input value = 125
>>> price * 4
‘125125125125’
>>> price = int(price)
>>> print(price)
125
>>> price * 4
500
Type of Variable, float
• Value
• Real number including decimal places.
(approximate value)
• Python stores numbers with “.” as float data type
• Number without decimal places is considered as
integer type.
• Operator
• +, –, *, /, **, Unary Operator –
• Float and integer can have different meanings.
• 1.0/2.0 is 0.5
Float Example 1
>>> price = 30.5
>>> price * 4
122.0
>>> price = price + 20
>>> print(price)
50.5
>>> print(price * 10)
505.0
>>> print(price)
50.5
Float Example 2
>>> price = input(“Enter price : “) # input value = 30.5
>>> price * 4
‘30.530.530.530.5’
>>> price = float(price)
>>> print(price)
30.5
>>> price * 4
122.0
Data Type, string
• String
• Letters surrounded with quotation marks
• Double quotation marks: “Hello World!”
• Single quotation marks: ‘Hello World!’
• Operator: +
• Concatenation is only used with strings
• Result of “ab” + “cd” is “abcd”
Data Type, string
• String is Indexed
• s = ‘abcd’
• s[0] is ‘a’, s[2]is ‘c’
• s1 = s[0] + s[3]
• ‘ad’ is stored in s1
• Extracting a part of a string
• s[1:]  ‘bcd’
• s[:2]  ‘ab’
• s[:3]  ‘abc’
• s[1:3]  ‘bc’
String Example 1
>>> s1 = “hello”
>>> s1
‘hello’
>>> s2 = ‘world’
>>> s2
‘world’
>>> s1+s2
‘helloworld’
String Example 2
>>> s1 = ‘The Brave’
>>> s1
‘The Brave’
>>> s2 = ‘The Beauty’
>>> s3 = s1 + ‘ gets ’ + s2
>>> s3
‘The Brave gets The Beauty’
>>> s3[4 : 9]
‘Brave’
Data Type, bool
• boolean or bool
• Is expressed as True or False
• Operator
• not, and, or
• Can be used when comparing integer or float
• i < j, i <= j, i >= j, i > j
• i == j, i != j
Data Type, list
• Collections of multiple data objects
• Presented in a collective form
• Can be accessed using indexes
List Example
0 1 2 3
>>> fruits = [ ‘apple’, ‘banana’, ‘blueberry’, ‘lime’]
>>> fruits[2]
‘blueberry’
>>> numbers = [ 1,2,4,7,11,16,22,29 ]
>>> print(numbers)
[ 1, 2, 4, 7, 11, 16, 22, 29 ]
>>> numbers[0]
1
Lecture Summary
• Concepts of the variable
• When to use a variable
• Used to store data

• 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 = “John”


>>> print(message)

• message
• ‘message’
• John
• ‘John’
Practice Question 2 Answer
• What is the output?

>>> message = “John”


>>> print(message)

• 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.

You might also like