07_BasicDataTypes
07_BasicDataTypes
Course Leader(s):
Prabhakar A.
Roopa G.
Jishmi Jos Choondal
Pujitha V.
Department of Computer Science and Engineering
Faculty of Engineering and Technology
Ramaiah University of Applied Sciences
1
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Objectives
2
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Topics
• int type
• float type
• complex type
• str type
• bool type
3
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Data Types
• In the real world, we use data all the time without bothering to consider what kind
of data we are using
4
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Data Types in Python
• In programming,
– A data type defines a set of values and a set of operations that can be performed on
those values
– A literal is the way a value of a data type looks to a programmer
5
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Python’s Core Data Types
• The Table previews Python’s built-in types and some of the syntax used to code their
literals
6
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The int Type
• The integers include 0, the positive whole numbers, and the negative whole
numbers
• The int class is designed to represent integer values
• Object of this type represent integers (both positive and negative)
• Cannot store a fractional part
• The call int(), can be used to construct an object of type integer based on an existing
value of another type
• It is helpful for converting other types like float and string to the type int
int(3.14) produce the value 3
int(3.99) produce the value 3
int(-3.9) produces the value -3
int(‘137’) produces the value 137
7
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The int Type - Examples
>>>15 #positive integer
15
>>> -0o15 #negative octal integer; octal constant is represented with leading 0o and
consists of numbers from 0 to 7
-13
8
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Long Integers
• Usually, in various programming languages such as C, the notion of int type and long
int type exists based on the range of integers that they can represent
– for example, int type may represent -2**31 to 2**31 - 1 and long int may represent
-2**63 to 2**63 -1
• In older versions of Python (namely Python 2.X too), the above distinction is
prevelant
• In Python 3.X, there is only int type irrespective of the number of bits required to
represent it
– For example, whatever may the number of bytes (or words) required to represent an
integer, the necessary number of bytes is allocated by Python
9
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Built in Functions on Type int
• The following mathematical functions are available on type int
10
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Built in Functions on Type int contd.
• Objects of type int can be converted to other bases using the below built in
functions
11
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The float Type
• The float data type allows to store numbers that have a decimal part
• Float constant
– Any literal constant made up only digits(with an optional leading sign) with either
a decimal point (.) or an exponentiation (e/E)
• Examples
>>>123.5
123.5
>>> 123.0
123.0
>>>12e2
1200.0
>>>123.5e2
12350.0 12
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The float Type contd.
• The float( ) is helpful for converting other types like int and string to the type float
13
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The complex Type
• The complex Type allows us to represent complex numbers
• The notation to represent complex numbers with real part x and imaginary part y
x+yj or x+yJ
• Examples
>>> 2+3J
(2+3j)
>>> (2+3j).real
2.0
>>> (2+3j).imag
3.0
14
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The complex Type contd.
• Objects of type complex can be constructed using the constructor call complex()
• The following statements are identical in function
x= 2+3j
x= complex(2,3)
• complex() is helpful for converting other types like int and string to the type float
>>> complex()
0j
>>> complex(2)
(2+0j)
>>> complex(“2+3j”)
(2+3j) 15
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The str Type
• The string type (str) allows to store sequence of characters in addition to numeric
values
• Empty string
>>> ''
''
>>> ""
''
16
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The bool Type
• The bool class is used to manipulate logical (Boolean) values
• The only two instances of that class are expressed as the literals
1. True
2. False
17
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The bool Type contd.
• The bool() is helpful for converting other types like int and string to the type bool
• Examples
>>>bool(0)
False
>>>bool(10)
True
>>>bool(-12.5)
True
>>>bool(“hello”)
True
>>>bool(“”)
False
>>>bool()
False 18
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Summary
• The basic data types of Python are int, float, complex, str and bool
• The int class is designed to represent integer values
• The float data type allows to store numbers that have a decimal part
• The complex Type allows to represent complex numbers
• The str type allow to represent a sequence of characters
• The bool class is used to manipulate logical (Boolean) values
19
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences