Variables Expressions Statements vzp1VQp0
Variables Expressions Statements vzp1VQp0
Learning Outcomes:
Upon successful completion of this lesson, you will be able to:
Variables
Constants
Data Types
Comments
Summary
TUTOR IAL
Knowledge Check
Review Questions
Online Discussion
Lesson 1 of 11
Variables
Lee Kien Phoon
This short video gives a detailed explanation on Variables and the Rules for Naming them
(about 13 minutes)
>>> x = 1
>>> print(x)
1
>>> x = 99
>>> print(x)
99
x is a variable and it is being assigned a value of 1. The print statement takes the value in x
and displays it on the screen.
The next statement assigns the value of 99 to x. Again, the print statement takes the value in
x and displays it on the screen.
Study the program given below:
x=1
y=2
x=x+3
y=x
Note that we do not encourage variable names to start with an underscore _ as this is reserve
for special uses. We do not emphasize naming convention in our beginner's course. If you are
interested to know more, you can refer to PEP 8.
Knowledge Check
Determine if each of the the following variable name is valid or invalid.
nric Valid
Invalid because it starts with a
1nric
number.
nric2 Valid
I lid b it h i l
Invalid because it has a special
nric@1
character.
n_r_i_c Valid
Reserved words cannot be used as variable names. They have special meanings in Python.
Syntax errors will be encountered if these reserve words are used. The list of reserve words is
given below:
import return in
Constants
Lee Kien Phoon
Constants are xed values that does not change when the program is running. For example,
the number 12 does not change during program execution. Therefore, 12 is an constant.
There are two types of constants, numeric constants and string constants.
String constants are characters enclosed in either single (‘) or double (“) quotes.
>>> print(12)
12
>>> print(1.23)
1.23
x=x+5
The expression is x + 5. After the expression is evaluated, the result is assigned to variable x.
Hence, x = x + 5 is an assignment statement.
For a simple expression, x + 5, x and 5 are known as operands and + is an operator. The table
below shows the common operators in Python:
This short video gives a detailed explanation on Constants, Expressions & Statements (about
18 mins)
Knowledge Check
Study the codes and give the output of the print statement.
x=3*2
y = x ** 2
36
print(y)
36
y%5
print(y)
x=3*2
y = x ** 2
36
print(y)
1
y=y%5
print(y)
x = 10 / 4
y = 10 // 4 2.5
print(x) 2
print(y)
When an expression has a long series of operations, we use parenthesis to make it more
readable. Let's look at 2 examples:
result_1 = (1 + 2) / 3 * 4 - 5 + 6
result_2 = 1 * 2 ** 3 / 4 + 5 * 6
Data Types
Lee Kien Phoon
A variable can hold di erent types of data. For example, the name of an employee must be
stored as a string whereas the salary must be stored as an integer.
In Python, there are 4 basic data types and we can use type() to check on the type of the data:
Integer (int)
–
Integers are whole numbers.
>>> type(1)
<class 'int'>
String (string)
–
A string is a sequence of characters represented in the quotation marks. In python, we can use
single, double, or triple quotes to de ne a string.
>>> type('string')
<class 'str'>
Boolean (bool)
–
A boolean expression (or logical expression) evaluates to one of two states true or false. Python
provides the boolean type that can be either set to False or True.
>>> type(False)
<class 'bool'>
Knowledge Check
Study the codes and give the output of the print statement.
x=3*2
y = x ** 2
36
print(y)
36
y%5
print(y)
x=3*2
y = x ** 2
36
print(y)
1
y=y%5
print(y)
x = 10 / 4
y = 10 // 4 2.5
print(x) 2
print(y)
When an expression has a long series of operations, we use parenthesis to make it more
readable. Let's look at 2 examples:
result_1 = (1 + 2) / 3 * 4 - 5 + 6
result_2 = 1 * 2 ** 3 / 4 + 5 * 6
The operator precedence is shown in the table below:
Lesson 5 of 11
We can mix the data types in the expressions and statements. However, there are rules to be
applied:
1 + 1.0 = 2.0
Operations between Integer and oating point number yields oating point number.
1 + True = 2
When booleans are used in arithmetic operation, True is 1 and False is 0.
5 * ‘s’ = ‘sssss’
A string multiply with an integer ended up with the repeated string value.
1 / 1 = 1.0
Division of two numbers resulted in a oating point number.
5 + ‘1’
We cannot add a number and a string.
True + ‘1’
Cannot add a boolean and a string.
Knowledge Check
What is the value of the following statements?
'1' + 2 * '3'+ '1' '1331'
TypeError: unsupported
1 + '2' * 3 + '1' operand type(s) for +: 'int' and
'str'
We can use the type conversion function to change the data type of a variable. For example, to
add a string '2' to an integer 5, we need to convert the string to an integer before the
operation:
TypeError: unsupported
result = 1 + '2' * 3 + '1' operand type(s) for +: 'int'
and 'str'
The input from the keyboard is stored as string type. What can be done if we want to read
numbers with the input() function? Well, we can use the int() or oat() function to convert
the input.
Comments
Lee Kien Phoon
Comments are text notes added to the program to provide explanation about the source code,
especially when the logic is complex. It helps other programmers to understand and ease the
maintenance of complex programs.
# This is a comment
# The interpreter will skip these lines automatically
# We use comment to help us (or other programmers)
# to understand the code that we have written
Summary
Lee Kien Phoon
Knowledge Check
Lee Kien Phoon
Question 1
Which of the following are legal identi ers? If illegal, give reason.
3com
–
Cannot start with a digit.
an_apple
–
Legal
Aug1965
–
Legal
a*star
–
Cannot have special characters +, - * /.
let’s go
–
Cannot have special character ‘ and space.
__hello
–
Legal but not recommended. A variable starts with an underscore is normally used for a
speci c purpose.
rst-name
–
Cannot have special characters +, - * /.
lastName
–
Legal
class
–
Cannot use reserved word.
Mix#8
–
Cannot have special characters.
Question 2
For each of the following assignment statements, give the result if it is correct. Otherwise,
explain why it is incorrect.
Question 3
Write the statement to
create two variables named width and length. Assign the value of 4 to the variable
width and value of 3 to the variable length.
calculate area of rectangle using the variable width and length. Reassigned the value to
variable width.
print(width)
A #3
p t( dt )
Answer #3
print(length)
Question 4
Given the following program, what value and type will be stored in result after each of the
following statements is being executed.
When 5 is divided by 4, it gives a oating point number of 1.25. The value remains unchanged when
multiply by 1 (True).
10 and it is an 'int'
5 + 4 + 1 = 10
Lesson 10 of 11
Review Questions
Lee Kien Phoon
There are 5 questions in this review. You need to score 80% to pass the review.
Question
01/05
False
rst_name
rstName
rst name
halt!
2MoreToGo
_width
length
Question
02/05
03/05
04/05
05/05
x=9
y=2
result = x ** y + x // 4
print(result)
20
22.25
83
83.25
Lesson 11 of 11
Online Discussion
Lee Kien Phoon
1 cup of butter
2 cups of our
The recipe produces 48 cookies with this amount of ingredients. Write a program that asks
the user for the number of cookies he wants to make, and display the number of cups of each
ingredient needed for the speci ed number of cookies.
What are the potential pitfalls of the suggested solution, and what enhancements can you
suggest to improve your program?