You are on page 1of 31

Introduction to Business Analytics

Copyright © LEARNXT
Introduction to Python Programming

Arithmetic and Boolean Operators


Copyright © LEARNXT
Objectives
At the end of this topic, you will be able to understand and explain:
 Understand the arithmetic operators used in Python

 Explain type conversion of variables with examples

 Describe the functionalities of Boolean operators

Copyright © LEARNXT
Arithmetic Operators in Python
Copyright © LEARNXT
Arithmetic Operators
 Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication and division
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponent
% Modulus (Remainder from division)
%/% Square Root
round() Round

Copyright © LEARNXT
Arithmetic Operators - Example
 print(2*2)

 4

 print(2**3)

 8

 print(10%3)

 1

 print(1.0/2.0)

 0.5

Copyright © LEARNXT
Arithmetic Operations on Variables - Example
 # Arithmetic operations on two variables

 a = 20

 b=5

 print(a)

 20

 print(b)

 5
d=a-b
 c=a+b
print(d)
 print(c)
15
 25
Copyright © LEARNXT
Arithmetic Operators on Strings - Example
 d = "Learn"
mynumber = 23.54
 e="" print(round(mynumber))
 f = "Python“ 24
 g=d+e+f

 print(g)

 Learn Python

 type(g)

 Output: str

Copyright © LEARNXT
Type Conversion
Copyright © LEARNXT
Type Conversion
 The process of converting the value of one data type (integer, string, float, etc.) to another data
type is called type conversion. Python has two types of type conversion

Type Conversion

Implicit Type Explicit Type


Conversion Conversion

Copyright © LEARNXT
Type Conversion
 Python defines type conversion functions to directly convert one data type to another

int() - convert a variable to integer

float() - convert a variable to floating point

str() - convert a variable to string

bool() - convert a variable to boolean (True or False) types

Copyright © LEARNXT
Type Conversion - Examples
 print (1.0/2.0)

 0.5

 # Convert to integer type and print

 print (int(1/2))

 0

 print (int(3.1415926))

 3

Copyright © LEARNXT
Type Conversion - Examples
 # Convert to string type and print

 print (str(3.1415926))

 3.1415926

 # Convert to boolean type and print

 print (bool(1))

 True

 print (bool(0))

 False

Copyright © LEARNXT
Implicit Conversion
 In Implicit type conversion, Python automatically converts one data type to another data type.
This process doesn't need any user involvement

 Example: Python promotes the conversion of the lower data type (integer) to the higher data
type (float) to avoid data loss

Copyright © LEARNXT
Implicit Conversion - Example

 # Implicit Conversion - Example converting integer to float type

 num_int = 123

 num_flo = 1.23

 num_new = num_int + num_flo

Copyright © LEARNXT
Implicit Conversion - Example
 print("datatype of num_int:",type(num_int))

 datatype of num_int: <class 'int’>

 print("datatype of num_flo:",type(num_flo))

 datatype of num_flo: <class 'float’>

 print("Value of num_new:",num_new)

 Value of num_new: 124.23

 print("datatype of num_new:",type(num_new))

 datatype of num_new: <class 'float'>


Copyright © LEARNXT
Explicit Conversion
 Explicit Type Conversion: users convert the data type of an object to required data type

 Use predefined functions like int(), float(), str(), etc to perform explicit type conversion

 This type of conversion is also called typecasting because the user casts (changes) the data
type of the objects

 Syntax :

 <required_datatype>(expression)

 Typecasting can be done by assigning the required data type function to the expression

Copyright © LEARNXT
Explicit Conversion - Example
 Example: Addition of string and integer using explicit conversion

 num_int = 123

 num_str = "456"

 print("Data type of num_int:",type(num_int))

 Data type of num_int: <class 'int’>

 print("Data type of num_str before Type Casting:",type(num_str))

 Data type of num_str before Type Casting: <class 'str’>

Copyright © LEARNXT
Explicit Conversion - Example
 num_str = int(num_str)

 print("Data type of num_str after Type Casting:",type(num_str))

 Data type of num_str after Type Casting: <class 'int’>

 num_sum = num_int + num_str

 print("Sum of num_int and num_str:",num_sum)

 print("Data type of the sum:",type(num_sum))

 Sum of num_int and num_str: 579

 Data type of the sum: <class 'int'>

Copyright © LEARNXT
Boolean Operators
Copyright © LEARNXT
Boolean Operators
 Boolean arithmetic is the arithmetic of true and false logic

 A Boolean or logical value can either be True or False

 Boolean values can be manipulated and combined with Boolean operators

 Boolean operators in Python include and, or, not

Copyright © LEARNXT
Boolean Operators - Types

Boolean
Operators

== != (not
or and not
(equivalent) equivalent)

Copyright © LEARNXT
Boolean Operators - Example
 Two variables are assigned the Boolean values True and False. Then these Boolean values
are combined and manipulated with Boolean operators

 A = True

 B = False

 # or operator

 print(A or B)

 True

 # and operator

 print(A and B)

 False
Copyright © LEARNXT
Boolean Operators - Example
 # not operator

 print(not A)

 False

 print(not B)

 True

Copyright © LEARNXT
Boolean Operators - Example
 # equivalent operator

 print(A == B)

 False

 # not equivalent operator

 print(A != B)

 True

Copyright © LEARNXT
Boolean Operators - Summary
 Summary of Boolean arithmetic and Boolean operators:

A B not A not B A == B A != B A or B A and B


T T F F F T T T
T F F T F T T F
F T T F T F T F
F F T T T F F T

Copyright © LEARNXT
Summary
 Arithmetic operators used to perform mathematical operations like addition, subtraction,
multiplication and division: + - * / ^ % %% round()

 Type conversion is the process of converting the value of one data type (integer, string, float,
etc.) to another data type

 Two types of type conversion: Implicit and Explicit

 Python has type conversion functions to directly convert one data type to another: int(), float(),
str() and bool()

 Explicit Type Conversion: users convert the data type of an object to required data type

 A Boolean or logical value can either be True or False

Copyright © LEARNXT
Additional Resources
 McKinney, W. (2013). Python for data analysis. O'Reilly Media.

 Lutz, M. (2013). Learning Python: Powerful object-oriented programming. O'Reilly Media.

 Summerfield, M. (2010). Programming in Python 3: A complete introduction to the Python


language. Pearson Education India.

 Matthes, E. (2019). Python crash course: A hands-on, project-based introduction to


programming (2nd ed.). No Starch Press.

 Beazley, D., & Jones, B. K. (2013). Python cookbook: Recipes for mastering Python 3. O'Reilly
Media.

Copyright © LEARNXT
e-References
 Welcome to Python.org. (n.d.). Python.org. https://www.python.org

 Introduction to Python. (n.d.). W3Schools Online Web


Tutorials. https://www.w3schools.com/python/python_intro.asp

Copyright © LEARNXT 29
Any Questions?

Thank you
Copyright © LEARNXT
Copyright © LEARNXT

You might also like