0% found this document useful (0 votes)
69 views15 pages

CLO2 - Part 1

This document discusses Python data types, variables, operators, and expressions. It introduces the primitive data types of int, float, and string. Variables are used to store data of a specific type and have naming rules. Arithmetic operators like addition, subtraction, multiplication, and division are used to perform calculations in Python based on the precedence rules. Exercises are provided to identify data types, valid variable names, evaluate expressions using operators and precedence.

Uploaded by

Hitesh Naik
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)
69 views15 pages

CLO2 - Part 1

This document discusses Python data types, variables, operators, and expressions. It introduces the primitive data types of int, float, and string. Variables are used to store data of a specific type and have naming rules. Arithmetic operators like addition, subtraction, multiplication, and division are used to perform calculations in Python based on the precedence rules. Exercises are provided to identify data types, valid variable names, evaluate expressions using operators and precedence.

Uploaded by

Hitesh Naik
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/ 15

Week 4 – Data Types, Variables, Operators and

Expressions.
(CLO2)
Learning Outcomes
Distinguish between different data types

Apply the rules for naming variables

Use arithmetic operators to perform calculation in Python.

Apply the Precedence Rules of arithmetic operators

2
1. Data Types
• In our daily life we use numbers and text to communicate
and perform certain processes.

• The primitive data types are numbers and text.

• Objects such as images and files are also part of data


types.

3
Data Types
decimal
int
26

Examples:
Numeric float 10.5
Types 0.105e2

' hello world '


string " hello world "
''’ use this for
Non – numeric multi line string '''
Types 4
True
4 bool
False
Exercise: Data Types
Identify the data types for the following

1. Total students present in the class


2. Course code of a subject
3. Room temperature
4. HCT student ID
5. Home address
6. Day of the week (Sunday, Monday, etc)
7. Price of a book
5
8. CGPA of a student
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.

● Must begin with a letter (a - z, A - B) or (_)


● Other characters can be letters, numbers or _
● Case Sensitive. Upper case is different from lower case
● Can be any (reasonable) length
● There are some reserved words which you cannot use as a variable name
because Python uses them for other things.
7

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

• Let us go back to our square


example.
• You noticed that we used 100 as
the length of the square.
• Now, we will use a variable L
instead of 100.

10

10
3. Arithmetic Operators
Operator Description Example

+ Addition Adds values on either side of the operator. Z=a + b

- Subtraction Subtracts right hand operand from left hand Z=a–b


operand.

* Multiplication Multiplies values on either side of the operator Z=a*b

/ Division Divides left hand operand by right hand operand Z=b / a

% Modulus Divides left hand operand by right hand operand Z=b % a


and returns remainder

** Exponent Performs exponential (power) calculation on Z = a**b


operators a to the power b

// Floor Returns the integral part of the quotient. 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

Variables are used to store data from one data type.

Variables naming must follow the rules.

Arithmetic operators used to do arithmetic operations.

Arithmetic operators evaluates based on operator precedence.

15

15

You might also like