PPS Notes (Unit 2) gcoea
PPS Notes (Unit 2) gcoea
Data Structure: A data structure is a storage that is used to store and organize data. It is a
way of arranging data on a computer so that it can be accessed and updated efficiently.
A data structure is not only used for organizing the data. It is also used for processing,
retrieving, and storing data.
List in Python:
Python Lists are just like dynamically sized arrays, declared in other languages (vector in
C++ and ArrayList in Java). In simple language, a list is a collection of things, enclosed in
[ ] and separated by commas.
The list is a sequence data type which is used to store the collection of data. Tuples and String
are other types of sequence data types.
Lists are the simplest containers that are an integral part of the Python language. Lists need
not be homogeneous always which makes it the most powerful tool in Python. A single list
may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and
hence, they can be altered even after their creation.
List elements can be accessed by the assigned index. In python starting index of the list,
sequence is 0 and the ending index is (if N elements are there) N-1.
Creating a List in Python
Lists in Python can be created by just placing the sequence inside the square brackets[].
Unlike Sets, a list doesn’t need a built-in function for its creation of a list.
Example 1: Creating a list in Python
# Creating a List
List = []
print("Blank List: ")
print(List)
Output
Blank List:
[]
List of numbers:
List Items:
Geeks
Geeks
Output
List with the use of Numbers:
[1, 2, 4, 4, 3, 3, 3, 6, 5]
# Creating a List
List1 = []
print(len(List1))
Output
0
3
Output
Accessing a element from the list
Geeks
Geeks
Output
Accessing a element from a Multi-Dimensional list
For
Geeks
Negative indexing
In Python, negative sequence indexes represent positions from the end of the List. Instead
of having to compute the offset as in List[len(List)-3], it is enough to just write List[-3].
Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the
second-last item, etc.
Output
Accessing element using negative indexing
Geeks
For
Tuple in Python:
Python Tuple is a collection of Python Programming objects much like a list. The sequence
of values stored in a tuple can be of any type, and they are indexed by integers. Values of a
tuple are syntactically separated by ‘commas‘. Although it is not necessary, it is more
common to define a tuple by closing the sequence of values in parentheses. This helps in
understanding the Python tuples more easily.
Creating a Tuple
In Python Programming, tuples are created by placing a sequence of values separated by
‘comma’ with or without the use of parentheses for grouping the data sequence.
Python Program to Demonstrate the Addition of Elements in a Tuple.
# Creating a Tuple
# with the use of string
Tuple1 = ('Geeks', 'For')
print("\nTuple with the use of String: ")
print(Tuple1)
Output:
Initial empty Tuple:
()
# Creating a Tuple
# with Mixed Datatype
Tuple1 = (5, 'Welcome', 7, 'Geeks')
print("\nTuple with Mixed Datatypes: ")
print(Tuple1)
# Creating a Tuple
# with nested tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('python', 'geek')
Tuple3 = (Tuple1, Tuple2)
print("\nTuple with nested tuples: ")
print(Tuple3)
# Creating a Tuple
# with repetition
Tuple1 = ('Geeks',) * 3
print("\nTuple with repetition: ")
print(Tuple1)
Output:
Tuple with Mixed Datatypes:
(5, 'Welcome', 7, 'Geeks')
Concatenation of Tuples
Concatenation of tuple is the process of joining two or more Tuples. Concatenation is done
by the use of ‘+’ operator. Concatenation of tuples is done always from the end of the
original tuple. Other arithmetic operations do not apply on Tuples.
Note- Only the same datatypes can be combined with concatenation, an error arises if a list
and a tuple are combined.
# Concatenation of tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('Geeks', 'For', 'Geeks')
Tuple2:
('Geeks', 'For', 'Geeks')
Output
Tuple with the use of String:
('Geeks', 'For')
Dictionary in Python:
A Python dictionary is a data structure that stores the value in key: value pairs.
Example: Here, The data is stored in key: value pairs in dictionaries, which makes it easier
to find values.
Python
Dict = { 1: 'Geeks', 2: 'For', 3: 'Geeks'}
print(Dict)
Output
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
Output
Dict = {}
print("Empty Dictionary: ")
print(Dict)
Output
Empty Dictionary:
{}
Dictionary with the use of dict():
Dict = {}
print("Empty Dictionary: ")
print(Dict)
Dict[0] = 'Geeks'
Dict[2] = 'For'
Dict[3] = 1
print("\nDictionary after adding 3 elements: ")
print(Dict)
Output
Empty Dictionary:
{}