Lecture 07: Basic Data Types
Course Leader: Jishmi Jos Choondal
Department of Computer Science and Engineering
Faculty of Engineering and Technology
M. S. Ramaiah University of Applied Sciences
1
Objectives
• At the end of this lecture, student will be able to
– explain the basic data types of Python
2
Topics
• int type
• float type
• complex type
• str type
• bool type
3
Data Types
• In the real world, we use data all the time without bothering to consider what kind
of data we are using
• For example, consider this sentence:
“In 2007, Micaela paid $120,000 for her house at 24 East Maple Street.”
• This sentence includes at least four pieces of data
– Name
– Date
– Price
– Address
4
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
• The programmer can use a literal in a program to mention a data value
• When Python interpreter evaluates a literal, the value it returns is simply that literal
• Table shows sample literals of several Python data types
5
Python’s Core Data Types
• The Table previews Python’s built-in types and some of the syntax used to code their
literals
6
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
The int Type - Examples
>>>15 #positive integer
15
>>> -15 #negative integer
-15
>>> -0o15 #negative octal integer; octal constant is represented with leading 0o and
consists of numbers from 0 to 7
-13
>>>0x12abc4 #hexadecimal integer; hexadecimal constant is represented with leading 0X or
0x and consists of digits and alphabets (a-f) in upper case or lower case
1223620
8
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
The int Type - Examples
10
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 11
The float Type contd.
• The float( ) is helpful for converting other types like int and string to the type float
float(2) produces the floating point value 2.0
float(‘3.14’) produces the floating point value 3.14
12
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
13
The complex Type contd.
14
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
The String Type
• The string type (str) allows to store sequence of characters in addition to numeric
values
• The string literals can be enclosed in single, double or triple quotes
• The following statements display the same output Hello
>>>print('Hello')
>>>print("Hello")
>>>print('''Hello''')
• Empty string
>>> ''
''
>>> ""
''
16
The String Type
• String literals may be delimited using either single or double quotes.
All the characters between the opening delimiter and matching
closing delimiter are part of the string:
17
The String Type
18
Numeric
# Python program to
# demonstrate numeric value
a=5
print("Type of a: ", type(a))
b = 5.0
print("\nType of b: ", type(b))
c = 2 + 4j
print("\nType of c: ", type(c))
19
String
String1 = 'Welcome to RUAS'
print("String with the use of Single Quotes: ")
print(String1)
# Creating a String
# with double Quotes
String1 = "I'm a Student"
print("\nString with the use of Double Quotes: ")
print(String1)
print(type(String1))
# Creating a String
# with triple Quotes
String1 = '''I'm a Student at RUAS"'''
print("\nString with the use of Triple Quotes: ")
print(String1)
print(type(String1))
# Creating String with triple
# Quotes allows multiple lines
String1 = ''‘Student
For Life'''
print("\nCreating a multiline String: ")
print(String1) 20
String
# Python Program to Access
# characters of String
String1 = “Hello"
In Python, individual characters of a String can be
print("Initial String: ")
accessed by using the method of Indexing.
print(String1)
Indexing allows negative address references to
# Printing First character
access characters from the back of the String, e.g.
print("\nFirst character of String is: ")
-1 refers to the last character, -2 refers to the
print(String1[0])
second last character and so on.
# Printing Last character
print("\nLast character of String is: ")
print(String1[-1])
21
List
• Lists are just like dynamic sized arrays, declared in other languages
(vector in C++ and ArrayList in Java). Lists need not be homogeneous
always which makes it the most powerful tool in Python.
• The main characteristics of lists are:
• The list is a datatype available in Python which can be written as a list of
comma-separated values (items) between square brackets.
• List are mutable .i.e it can be converted into another data type and can store
any data element in it.
• List can store any type of element.
22
List Example
# Python3 program to demonstrate
# List
# Creating a List
List = []
print("Blank List: ")
print(List)
# Creating a List of numbers
List = [10, 20, 14]
print("\nList of numbers: ")
print(List)
List items ??
# Creating a List of strings and accessing
# using index
List = [“Eggs", “Bread", “Butter"]
print("\nList Items: ")
print(List[0]) 23
Tuple
• Tuple is an immutable sequence in python.
• It cannot be changed or replaced since it is immutable.
• It is defined under parenthesis().
• Tuples can store any type of element.
24
Tuple Example
# Creating an empty Tuple
Tuple1 = ()
print("Initial empty Tuple: ")
print (Tuple1)
# Creating a Tuple with
# the use of list
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))
#Creating a Tuple Tuple with the use of function ??
#with the use of built-in function
Tuple1 = tuple(‘RUAS')
print("\nTuple with the use of function: ")
print(Tuple1)
25
Set
Set is an unordered collection of data type that is iterable, mutable, and has no
duplicate elements. The major advantage of using a set, as opposed to a list, is
that it has a highly optimized method for checking whether a specific element is
contained in the set.
The main characteristics of set are –
• Sets are an unordered collection of elements or unintended collection of
items In python.
• Here the order in which the elements are added into the set is not fixed, it can
change frequently.
• It is defined under curly braces{}
• Sets are mutable, however, only immutable objects can be stored in it.
26
Set Example
# Python3 program to demonstrate
# Set in Python
# Creating a Set
set1 = set() Initial blank Set:
print("Initial blank Set: ") Set()
print(set1)
# Creating a Set with Set with use of an object:
# the use of Constructor {‘a’, ‘R’, ‘m’, ‘i’, ‘h’}
# (Using object to Store String)
String = ‘Ramaiah' Set with use of list:
set1 = set(String) {‘Bread’, ‘Butter’}
print("\nSet with the use of an Object: " )
print(set1)
# Creating a Set with
# the use of a List
set1 = set([“Bread", “Butter", “Butter"])
print("\nSet with the use of List: ")
print(set1)
27
Dictionary
• Dictionary holds key:value pair.
• Key-Value is provided in the dictionary to make it more optimized.
Dict = {1: ‘Bread', 2: ‘Butter', 3: ‘Jam'}
print(Dict)
28
The type Function
a = (“This", “is", “RUAS")
b = [“This", “is", “RUAS"]
c = {“This": 1, “is":2, “RUAS":3}
d = "Hello World"
e = 10.23
f = 11.22
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))
29
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
• Numbers evaluate to False if zero, and True if nonzero
• Sequences and other container types, such as strings and lists, evaluate to False if
empty and True if nonempty
30
The bool Type
31
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
32