Experiment 7: Lists and Tuples Operations
Datatypes in Python
Standard(i.e Built-in) Datatypes in Python
1.Numeric
2.Sequence Type (like Tuple, etc)
3.Boolean
4.Set
5.Dictionary
1. Numeric :-
In Python, there are three distinct numeric types: integers, floating point numbers, and complex numbers.. numeric data type
represent the data which has numeric value. There exists three distinct numeric types: integers, floating point numbers,
and complex numbers Numeric value can be integer, floating point number or even complex numbers.
(i) Integers – This value is represented by int class.
(ii) Float – This value is represented by float class.
(iii) Complex Numbers – Complex number is represented by complex class. It is specified as (real part) + (imaginary part) j.
For example : 2+3j
Note –
type() function is used to determine the type of data type.
Unlike Python,In C,Floating point numbers can be implemented using either float or double datatypes.
In python,Complex numbers have a real and imaginary part, which are each a floating point number. To extract these
parts from a complex number z, use z.real and z.imag
Eg-
a=5
print("Type of a: ", type(a))
b = 5.234556
print("\n Type of b: ", type(b))
c = 2 + 4.3j
print("\n Type of c: ", type(c))
print("\n Real part of c: ",c.real)
print("\n Imaginary part of c: ",c.imag)
O/P:-
Type of a: <class 'int'>
Type of b: <class 'float'>
Type of c: <class 'complex'>
Real part of c: 2.0
Imaginary part of c: 4.3
2. Boolean :-
Boolean datatype contains two built-in values, True or False
Note:-
True and False with capital ‘T’ and ‘F’ are only valid booleans otherwise python will throw an error.
Eg-
f=True
print("\n Type of f: ", type(f))
3. Set :-
In Python, Set is an unordered collection of datatypes and has no duplicate elements.
The order of elements in a set is undefined though it may consist of various elements.
-Creating Sets
Sets can be created by using 2 ways :-_
1. By using built-in set() function
2. By using a sequence by placing the sequence inside curly braces { }, separated by ‘comma’
Type of elements in a set need not be the same, various mixed-up data type values can also be passed to the set.
Note:-
We cannot access or change an element of a set using indexing or slicing as Set data type is unordered, so, indexing has no
meaning.
eg-
set1 = {1, 2, 33, 44, 6}
set1[3] =10
print(set1)
O/P:-
Traceback (most recent call last):
File "C:/Users/shash/AppData/Local/Programs/Python/Python311/Set_Datatype2.py", line 3, in <module>
set1[3]=10
TypeError: 'set' object does not support item assignment
Eg-
# Python program to demonstrate Creation of Set in Python
# Creating an empty Set
set1 = set()
print("An empty Set: ")
print(set1)
# Creating a Set with the use of a String
set1 = set("HelloWorld")
print("\nSet with the use of String: ")
print(set1)
# Creating a Set with the use of a List
set1 = set(["Hello", "World", "Hello","Hello"])
print("\nSet with the use of List: ")
print(set1)
# Creating a Set with the use of a List (but with different datatypes )
set1 = set([10, 22, 'World', 43, 'Hello', 'Hello'])
print("\nSet with the use of Mixed Values")
print(set1)
#Creating a Set with the use of Curly braces{}( separated by ‘comma’)
set1 = {1,2,3,4,5}
print("\nSet with the use of Curly braces")
print(set1)
Output:
An empty Set:
set()
Set with the use of String:
{'d', 'e', 'H', 'W', 'o', 'l', 'r'}
Set with the use of List:
{'World', 'Hello'}
Set with the use of Mixed datatype Values
{10, 43, 'World', 22, 'Hello'}
Set with the use of Curly braces
{1,2,3,4,5}
3. Sequence type (or Sequence) :-
- In Python, sequence is an ordered collection of similar or different data types. Sequences allows us to store multiple values in
an organized and efficient manner. There are several sequence types in Python –
String
List
Tuple
(i) String
In Python, Strings are arrays of bytes.A string is a collection of one or more characters inside a ( single quote/ double-quotes
/triple quote). Strings are represented by str class.
Unlike C,In python there is no character data type since a character is also a string of length one.
Creating String
Strings in Python can be created using single quote or double quotes or even triple quotes.
Eg1-
d = 'a'
print("\n Type of d: ", type(d))
e = 'jru'
print("\n Type of e: ", type(e))
O/P:- Type of d: <class 'str'>
Type of e: <class 'str'>
Note:-
-Accessing elements of String
In Python, individual characters of a String can be accessed by using the Indexing technique. In python, Indexing allows both
positive index references and negative index references( to access characters from the back of the String i.e 1 refers to the last
character, -2 refers to the second last character and so on.)
Eg2-
# Python Program to access characters of String
String1 = "Hello Worldss"
print(String1)
# Printing First character
print("\nFirst character of String is: ")
print(String1[0])
# Printing Last character
print("\nLast character of String is: ")
print(String1[-1])
O/P:-
Hello Worldss
First character of String is:
H
Last character of String is:
s
(ii) List
- Lists are just like the arrays.
- List is an ordered collection of datatypes where the items in a list need not be of the same type.
- Unlike Sets and Dictionary, Lists allow duplicate elements.
Creating a List
Lists in Python can be created by placing the sequence inside the square brackets[].
Eg-
# Python program to demonstrate Creation of List
# Creating an empty List
List = [ ]
print("An empty List: ")
print(List)
# Creating a List with the use of a String
List = ['Hello World']
print("\nList with the use of String: ")
print(List)
# Creating a List with the use of multiple values
List1 = ["Hello","World", 22.2]
print(List1[0])
print(List1[2])
O/p:-
An empty List:
[]
List with the use of String:
['Hello World']
List containing multiple values:
Hello
22.2
(iii) Tuple
-Just like Lists, tuple is also an ordered collection of Datatypes. It is represented by tuple class.
-Creating a Tuple
In Python, tuples are created by placing a sequence of values separated by ‘comma’ with or without the use of parentheses( )
for grouping of the data sequence. Tuples can contain any number of elements and of any datatype (like strings, integers, list,
etc.).
Note: Tuples can also be created with a single element, but it is a bit tricky. Having one element in the parentheses is not
sufficient, there must be a trailing ‘comma’ to make it a tuple.
Eg-
# Python program to demonstrate creation of Set
# Creating an empty tuple
Tuple1 = ()
print("Initial empty Tuple: ")
print (Tuple1)
print(type(Tuple1))
#creating a tuple with single element
Tuple1=(0,)
print("\nTuple with single element ")
print(Tuple1)
# Creating a Tuple with the use of Strings
Tuple1 = ('Hello', 'world')
print("\nTuple with the use of String: ")
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 with the use of built-in function
Tuple1 = tuple('Hello')
print("\nTuple with the use of function: ")
print(Tuple1)
Output:
Initial empty Tuple:
()
Tuple with single element
(0,)
Tuple with the use of String:
('Hello', 'world')
Tuple using List:
(1, 2, 4, 5, 6)
Tuple with the use of function:
('H', 'e', 'l', 'l', 'o')
Note –
The only difference between tuple and list is that lists are mutable But tuples are immutable in nature(i.e. tuples cannot be
modified after it is created) So,once we create a tuple we cannot make changes to it.Therefore, if we try to do so it will result in an
error.
-An Example of showing How Tuples are immutable
tuple1 = (1, 2, 33, 44, 6)
tuple1[4] = 10
print(tuple1)
O/P:-
Traceback (most recent call last):
File "C:/Users/shash/AppData/Local/Programs/Python/Python311/TupleDatatype2.py", line 3, in <module>
tuple1[4] = 10
TypeError: 'tuple' object does not support item assignment
-An example of showing how lists are mutable
list1 = [1, 2, 33, 44, 6]
list1[4] = 10
print(list1)
O/P:-
[1, 2, 33, 44, 10]