Python Variables
(COMPUTER FUNDAMENTALS AND
PROGRAMMING)
Variables
Variables
Co
in s
2
Variables
Variables are a way for us to store
information for later use in our
programs.
Variable: The treasure chest is like a variable. It can hold or store
something that you can take out and use whenever you need it.
Value: The number of coins in the box is the value of the variable.
The number inside can change as we add or take away coins.
Variable Name: You label the box to remember what
information is stored in a variable. When you use a lot of variables,
you will want to use names to keep track of them. Think of this like Co
moving boxes – you need to label one box “Kitchen” and one box in s
“Bedroom” so you know where everything is!
*Vari-able: The value of a variable can change over the
course of a program.
3
Variables
Variables
Variable
(the container)
Variable Name
Value
(label for the container)
(stored inside the container)
Coins = 3
Read from left to right:
“The variable Coins is assigned to the
integer 3” 4
Warm-up Activity
Our Variables
● age =
● name =
● song =
● food =
● number =
Literals
Constant values that are typed in the program
as a part of the source code.
• Textual Literals (String Literal)
• Numeric Literals
• Boolean Literals
• None (Special Literal)
Textual Literals
Textual Literals (String Literal)
A string literal is a sequence of
characters surrounded by quotes.
• print (“Hello NU”)
• print(name)
name = “Tharin” • print(“Tharin”)
hair = “Light Brown”
armor = “No”
Numeric Literals
Numeric Literals
Numeric literals can belong to 2
different numerical types Integer and
Float.
• print (143)
• print (3.1416)
age = 45 • print (height)
weight = 90.99999
height = 156.1
Boolean Literals
Boolean Literals
It can have any of the two values: True
or False.
• a = (1 == True)
• b = (1 == False)
• c = True + 4
male = True • d = False + 10
warrior = False
villager = True
Special Literal
Special Literal
Python contains one special literal
“None”. We use it to specify to that
field that is not created.
• enemy = None
weapon = None
artifacts = None
armor = None
Assignment
Statement
A literal is used to indicate a specific value, which
can be assigned to a variable.
– x=3.1414
– print(x)
– y=143
– print(y)
Identifiers
• An identifier is a name given to entities like class,
functions, variables, etc. It helps to differentiate
one entity from another.
• User-defined words
Rules for Identifiers
• Identifiers can be a combination of letters in lowercase (a to z) or
uppercase (A to Z) or digits (0 to 9) or an underscore _.
• Keywords cannot be used as identifiers.
• No space in between characters
• Case sensitive
eg.
studentData01 = “…”
student_Data01 = “…”
*do not start your identifier with an integer
Rules for Identifiers
• Keywords are the
reserved words in
Python.
• Used to define
the syntax and
structure of the
Python language.
• keywords are
case sensitive
Python Statement
• Instructions that a Python interpreter can execute are called
statements.
a=1
a = 1; b = 2; c = 3
• Multi-line statement
a= 3 + 5 + 2 + 1 + \
7+4+5-\
1*2
a= (3 + 5 + 2 + 1 +
7+4+5-
1*2)
Indention
• A code block (body of a function, loop etc.) starts
with indentation and ends with the first un-
indented line.
• Four whitespaces are used for indentation and is
preferred over tabs.
Comments
• Comments are for programmers for better
understanding of a program.
• It extends up to the newline character.
• Python Interpreter ignores comment.
• Use the hash (#) symbol to start writing a
comment.
– #This is a comment
– #This is another comment
Constants
• A constant is a type of variable whose value cannot be
changed.
• Usually declared and assigned on a module.
PI=3.14
GRADE=98.75
Save the above declaration to const.py
import const.py
Print(const.PI)
Print(const.GRADE)
Save the above codes to main.py
Assigning Input
• Accomplished via an assignment statement
combined with a built-in function called
input.
• When Python encounters a call to input, it
prints <prompt> (which is a string literal)
then pauses and waits for the user to type
some text and press the <Enter> key
Assigning Input
• prog = input("Enter your Program: ")
Enter your Program:
• prog
• Whatever the user types is then stored as a
string
Assigning Input
• grd= input("Enter your grade: ")
Enter your grade: 57
• grd
• type(grd)
• How can we force an input grade to be
stored as a number and not as a string?
Assigning Input
• grd = eval(input("Enter grade: "))
Enter your grade: 57
• grd
• equation = eval(input("Enter an equation: "))
Enter an equation: 5+7
• grd
Assigning Input
• age = int(input("Enter your age: "))
Enter your age: 21
• age
• grd = float(input("Enter your grade: "))
Enter your grade: 57.88
• grd
Data Type Conversion
• a=5
• float(a) ;integer to float
• str(a) ;integer to string
• b = "20"
• float(b) ; string to float
• int(b) ; string to integer
• c = 30.0
• int(c) ;float to integer
• str(c) ;float to string
Simultaneous
Assignment
• Python allows us also to assign multiple
values to multiple variables all at the same
time
– x, y = 2, 3
Expressions
• x=2+3
This is an expression that uses the addition operator
• print(x)
• print(5 * 7)
This is an expression that uses the multiplication operator
• print("5" + "7")
This is an expression that uses the addition operator but to concatenate strings
together
Expressions
x=6 • print(x*y)
y=2 • print(x**y)
print(x - y) • print(x%y)
print(x/y)
• print(abs(-x))
print(x//y)
Next Meeting
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
Assignment Operators
Identity Operators
Membership Operators
Output Formatting
Question
Activity
Create a program Sample Output:
that will take the
Name: Elon Musk
following variables Year: 4 Year
as input: Course: BS Computer Science
Name (String)
Year (Int)
Program (String)
Question?