Lecture – 03
Data Types in Python
Complied by: Subash Khatiwada
Python is Dynamically Typed
01/23/2025
Language
a = 15
<type ‘int’>
a = 3.0
Python Programming
<type ‘float’>
2
01/23/2025
Python Data Types
Every value has a datatype, and variables can hold values.
Python Programming
3
01/23/2025
Numeric Data Types
There are three distinct numeric types: integers, floating point numbers, and
complex numbers
1. Integer
It contains positive or negative whole numbers (without fractions or decimals).
Example:
A=5
2. Float
It is a real number with a floating-point representation.
A = 123.3335
Python Programming
3. Complex Number
It is specified as (real part) + (imaginary part)j.
A = 2 + 3j
4
01/23/2025
Boolean Data Type
The Boolean data type is either True or False.
Built-in data types which represents one of the two values either True or False.
Example:
A = True
Python Programming
B = False
C=5>4 (True)
D = bool(1) type conversion from int to bool
5
01/23/2025
Sequence Types
Sequence data types are used to store data in containers in the Python computer
language.
1) List
2) Tuples
3) String
Python Programming
6
01/23/2025
Lists
• Lists are mutable meaning that list can be changed and modified.
• Lists are the collection of heterogenous data.
• In python, list items are separated by commas (,) and enclosed within square
backet [ ].
• Example
my_list = ["Bob Marley", 1231242.232 , "singer"]
Python Programming
7
Features of lists
Python Programming 01/23/2025
8
01/23/2025
[1] List are ordered
list is an ordered collection of items. Lists that have the same elements in
a different order are not the same.
a = ['foo', 'bar', 'baz', 'qux’]
b = ['baz', 'qux', 'bar', 'foo’]
a==b // False
a is b // False
[1, 2, 3, 4] = = [4, 1, 3, 2] // False
Python Programming
9
01/23/2025
[2] List are changeable (Mutable)
The list is changeable, meaning that we can change, add, and remove
items in a list after it has been created.
X = [12, 14, 56, 55.77]
X.append(66) //adding 66 to the end of list
X[1] = 45 //updating values of index 1
Python Programming
10
01/23/2025
[3] List can contain arbitrary items
List can contain arbitrary items and duplication is also allowed.
X = [1, 1, 1 ,1, ‘ram']
Python Programming
11
[4] Elements in list can be accessed by
01/23/2025
index
my_list = [10,20, 'Ram', 12, 24]
-5 -4 -3 -2 -1
Indices 10 20 'Ram' 12 24
0 1 2 3 4
my_list[2:4] // ['Ram', 12]
Python Programming
my_list[-4:-1] // [20, 'Ram', 12]
my_list[::-1] // [24, 12, 'Ram', 20, 10] Reversed
12
01/23/2025
[5] List can be nested
MyList[0:1] ??
MyList[2][0] ??
Python Programming
13
01/23/2025
concatenate(
extend( ) )
List Methods insert( ) count( )
List support various built-
in operations. Some of append( ) sort( )
them are following:
remove( ) index( )
pop( ) clear( )
reverse( )
Python Programming
14
01/23/2025
List Methods
fruits = ["apple", "orange", "peach"]
vegies = ["cabbage", "beans", "pumpkin"]
fruits.extend(vegies) // used to extend the list
friuts.insert(0, "grapes") // used to insert items in any index
fruits.append(“apple") // append is used to add items at the end of the list
Run Yourself …
Python Programming
15
01/23/2025
Tuples
• Tuples are used to store multiple items in a single variable.
• A tuple is a collection which is ordered and unchangeable.
• In tuple, items are separated by commas (,) and enclosed within round backet ( ).
Example:
A = ("apple", "orange", "peach")
Python Programming
16
01/23/2025
Similarities between List and Tuple
• Tuples and lists are both used to store collection of data.
• Tuples and lists both can store heterogeneous data.
• Tuples and lists are both ordered collection of items and they
both provides duplicate values inside them.
• Items of both tuples and lists can be accessed by an integer
index.
• index() and count() function can be used.
Python Programming
17
01/23/2025
Difference between List and Tuple
List Tuple
1. Lists are mutable. 1. Tuple are immutable.
2. Lists are less memory efficient 2. Tuples are more memory
than tuples. efficient than the lists.
3. Lists are enclosed in square [ ] 3. Tuples are enclosed in round ( )
brackets. brackets.
Tuple is good to use if data is not
Python Programming
4. Lists are great for changing or 4.
modifying data in a program. going to change in program.
5. Various operations such as 5. Operations such as insert,
append, extend can’t be used
append, insert, extend etc. can
with tuple data objects.
be used with list data objects.
18
01/23/2025
Strings
• In python, a string is a sequence of characters enclosed
within either single quotes (' ') or double quotes (" ").
• Tripple quotes ' ' ' are used for multiline strings.
• Python strings are "immutable" which means they cannot be
changed after they are created. (We will discuss in detail at
week 07)
Python Programming
19
01/23/2025
String Methods
Method Description
capitalize() Converts the first character to upper case
count() Returns the number of times a specified value occurs in a string
index() Searches the string for a specified value and returns the position of
where it was found
isalnum() Returns True if all characters in the string are alphanumeric
isalpha() Returns True if all characters in the string are in the alphabet
Python Programming
isdigit() Returns True if all characters in the string are digits
upper() Converts a string into upper case
lower() Converts a string into lower case
20
01/23/2025
Dictionary
• Dictionaries are used to store data values in key : value pairs.
• A dictionary is a collection which is ordered, changeable and
do not allow duplicates keys.
my_dict = {
"roll": 1,
"name": "Alice",
Python Programming
"mark": 78.55
}
print(my_dict)
21
01/23/2025
Set
• Sets are used to store multiple items in a single variable.
• Set items are unordered, changeable, and do not allow
duplicate values.
• Sets are written with curly brackets { }.
Python Programming
set_A = {"apple", "banana", "cherry"}
22
01/23/2025
Python Collections
There are four collection data types in the Python
programming language
1. List is a collection which is ordered, changeable and allows
duplicate members.
2. Tuple is a collection which is ordered and unchangeable
and allows duplicate members.
3. Set is a collection which is unordered, changeable (element
Python Programming
can’t be mutable), and unindexed. No duplicate members.
4. Dictionary is a collection which is ordered and changeable.
No duplicate keys.
23
01/23/2025
Tutorial–03: Python Datatypes
1. Write difference between list and tuples
2. Write difference between set and dictionary
Python Programming
24
END
Python Programming 01/23/2025
25