Data types and Operations analytics data

HashanSWimalasiri 0 views 34 slides Sep 17, 2025
Slide 1
Slide 1 of 34
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34

About This Presentation

Analytics


Slide Content

Lecture 3 Data Types & Operators DA5120 – Programming for Business Analytics Supun Gothama

Data Types

Introductions Data in Python can be of different types We will learn 3 basic types for now Numbers Strings Booleans

Numbers Four numeric types Integers (or “plain integers”) up to → int Long integers → long Floating-point (real) numbers → float Complex numbers → complex Integers can usually hold a value up to up to Long integers can have an unlimited size value  

Numbers We can use Python’s type() function to check the data type of a variable >>> b = 97 >>> type(b) <type 'int’> >>> type(95.2) <type 'float'>

Strings Strings are sequences of characters Anything inside quotes is considered a string in Python Can use single or double quotes >>> s1 = "This is a string" >>> s2 = 'This is also a string' >>> type(s1) <type 'str'>

Booleans The Boolean data types can only have 2 possible values False True Behave according to Boolean algebra >>> x = True >>> type(x) <type 'bool'>

Expressions & Operators

Expressions Expressions combines literals, variables and operators Eg : 3 + 5 Produce a new value

Expressions Can be arbitrarily complicated 3 + 8 * 5 + 4 – 7 / 100 Can be used in different places x = 3 + 7 / 5 print(3 + 7 / 5)

Operators An operator is a symbol that tells Python interpreter to perform specific mathematical, relational or logical operation and produce a final result Operands are the values participate in an operation

Types of Operators We will learn the following types of operators in Python Arithmetic Operators Comparison (Relational) Operators Assignment Operators Logical Operators

Arithmetic Operators You can add (+), subtract (-), multiply (*), and divide (/) number in Python >>> 2 + 3 5 >>> 3 - 2 1 >>> 2 * 3 6 >>> 3 / 2 1.5

Arithmetic Operators Exponent (**) operator performs exponential (power) calculation on numbers >>> 3 ** 2 9 >>> 3 ** 3 27 >>> 3 ** 4 81

Arithmetic Operators Modulus (%) operator divides left hand operand by right hand operand and returns remainder >>> 7 % 4 3 >>> 8 % 6 2 >>> 10 % 5

String Operators Concatenation (+) operator joins two strings >>> " Abc " + " Xyz " ' AbcXyz ' Repeat (*) operator repeats a string specified number of times >>> " Abc " * 3 ' AbcAbcAbc '

Comparison Operators These operators compare the values on either sides and give a Boolean result Equal (==) operator check whether the values of two operands are equal >>> 5 == 4 False >>> 6 == 6 True

Comparison Operators Not equal (!= or <> ) operator check whether the values of two operands are not equal >>> 5 != 4 True >>> 6 != 6 False >>> 10 <> 26 True

Comparison Operators Greater than (>) operator check whether value of left operand is greater than the value of right operand >>> 5 > 4 True >>> 6 > 6 False Less than (<) operator check whether value of left operand is less than the value of right operand >>> 5 < 4 False

Comparison Operators Python also has operators for Greater than or equal (>=) Less than or equal (<=) >>> 6 >= 6 True >>> 4 <= 9 True >>> 4 >= 9 False

Logical Operators Logical operators are used to combine Boolean value expressions and give a Boolean result and operator returns True if both operands are true >>> True and True True >>> True and False False >>> False and False False

Logical Operators or operator returns True if at least one of the operands is true >>> True or True True >>> True or False True >>> False or False False

Logical Operators not operator inverts the Boolean value of the operand >>> not False True >>> not True False A truth table can be used to summarize the results of these operators

Assignment Operators Assignment operators are used to assign values to variables We already know the assign (=) operator Add and assign (+=) operator adds right operand to the left operand and assign the result to left operand Eg : x += y is equivalent to x = x + y

Assignment Operators Similarly other arithmetic operations also can be used as ‘and assign’ Operator Example Equivalent to += x += 5 x = x + 5 -= x -= 5 x = x - 5 *= x *= 5 x = x * 5 /= x /= 5 x = x / 5 %= x %= 5 x = x % 5 **= x **= 5 x = x ** 5

Assignment Operators x = 10 x += 4 print(x) 14 y = 20 y *= 5 print(y) 100

Operators with Expressions Operands for these operators does not have to be simple values Operands can be any valid expressions >>> 5 - 2 < 5 + 3 True >>> 2 < 3 and 4 > 1 True >>> not 2 < 3 False

Operator Precedence Expressions can be ambiguous Eg : a * 3 + b / x can be a * (3 + b) / x (a * 3) + (b / x) (a * 3 + b) / x To avoid this kind of ambiguity, python has a precedence (priority) for operators

Operator Precedence ** Exponentiation + , - Unary plus and minus ( eg : +5, -5) * , / , % Multiply, divide, modulo + , - Addition and subtraction <= , < , > , >= Comparison operators < > , == , != Equality operators = , %= , /= , //= , -= , += , *= , **= Assignments not , or , and Logical operators

Operator Precedence More than one operator exists in the same level These operators have the same precedence In that case almost all the operators are evaluated in left-to-right order (associativity) in expression Example Multiplication and division have the same precedence Left operator is evaluated first 5 / 2 * 10 → 25.0

Operator Precedence Exponent operator ** has right-to-left associativity in Python 2 ** 3 ** 2 → 512 Operator precedence can be changed using parentheses () as in mathematics (10 - 4) * 2 → 12 40 / (2 * 10) → 2.0

Evaluate the following expressions. Activity 4 + 3 % 5 x = 10 x *= 2 ** 3 2 * 3 > 6 and 10 < 20

Evaluate the following expressions. Activity 4 + 3 % 5 → 7 x = 10 x *= 2 ** 3 → 80 2 * 3 > 6 and 10 < 20 → False

Summary ☑ Data types ☑ Expressions ☑ Operators ☑ Operator Precedence
Tags