Professional Documents
Culture Documents
Week 4 - Data Types, Variables, Operators and Expressions
Week 4 - Data Types, Variables, Operators and Expressions
Expressions.
(CLO2)
Learning Outcomes
Distinguish between different data types
2
1. Data Types
• In our daily life we use numbers and text to communicate
and perform certain processes.
3
Data Types
decimal
int
26
Examples:
Numeric float 10.5
Types 0.105e2
5
2. Variables
Variables are used to store data from one data type.
For example, the variable fname is used to store the first name of a person.
The first name of a person is of type text or string.
fname age
6
6
2. Rules for naming a Variable
Technical definition of a variable: A variable is a memory location where a programmer
can store a value.
7
Exercise: Variables
Identify valid variable names from the following
1. max_price
2. Total students
3. US$
4. student_id
5. as
6. discount20
7. 1st_student
8. ICT-2013 8
8
Using Variable
• What if you want to change the size of the square from 100 to 50 dots?
• Solution 1:
• Replace all 100 by 50
• Solution 2:
• Use a variable instead of a value!
• How?
9
Using Variable
10
10
3. Arithmetic Operators
Operator Description Example
+ Addition Adds values on either side of the operator. Z=a+b
11
11
Exercise: Operators and Expressions.
Convert the following mathematical expression to Python
expression
1 2
4
3
12
12
4. Precedence Rules
1. ( ) x = 18 / 2 * 3 + 2
2. ** 9 * 3
3. *, /, %, // 27 + 2
4. +, -
29
13
13
Exercise: Precedence Rules.
Evaluate the following expressions using the operator
precedence.
1. 12 + 8 / 4 * 3
2. (12 + 8) / 4 * 3
3. 11 // 3 + 3 ** (4 - 2) * 2
14
14
Summary
Primitive Data types: int, float, string
15
15