2) Python Data Structures
2) Python Data Structures
By
Dr. Yogesh Rajput
Python Data Structures
a = "Hello"
print(a)
a = "Hello, World!"
print(a[2:5])
• By leaving out the start index, the range will start at the first
character:
a = "Hello, World!"
print(a[:5])
• By leaving out the end index, the range will go to the end:
• Get the characters from position 2, and all the way to the end.
a = "Hello, World!"
print(a[2:])
age = 36
txt = "My name is Sham, and I am {}"
print(txt.format(age))
• Syntax
string.upper()
• Syntax
string.lower()
• Syntax
string.replace(oldvalue, newvalue, count)
Note: All occurrences of the specified phrase will be replaced, if
nothing else is specified (count).
• Syntax
Parameter Description
• To determine how many items a list has, use the len() function:
• Example
• List items are indexed and you can access them by referring to
the index number:
• Create a list with the names of five students and display the
length and last three elements.
• List objects have a sort() method that will sort the list.
• List objects have a sort() method that will sort the list.
• Create a list with the marks of five students and sort the list in
ascending and descending order.
Parameter Description
elmnt Required. An element of any type (string, number, object etc.)
list.insert(ind,elmnt)
Parameter Description
ind Required. The specified index
elmnt Required. An element of any type (string, number, object etc.)
list.remove(elmnt)
Parameter Description
elmnt Required. An list element
• Use the insert() method to add 'Sunday' to the list, and apply
the remove() method to eliminate 'Saturday' from the list.
• The differences between tuples and lists are, the tuples cannot
be changed unlike lists and tuples use () parentheses,
whereas lists use square brackets.
• Example:
• Tuple items are indexed, the first item has index [0], the
second item has index [1] etc.
• Create a two tuples for name and marks of five students and
print them with length.
var.count(value)
Parameter Description
value Required. Any type (string, number, list,
tuple, etc.). The value to search for.
var.index(elmnt)
Parameter Description
elmnt Required. Any type (string, number, list,
etc.). The element to search for
thisdict = {
"brand": “TATA",
"model": “Nexon",
"year": 2017,
"year": 2022
}
print(thisdict)
2. popitem()
3. del Keyword
4. clear()
• Example
Create a Set:
• Example
• To determine how many items a set has, use the len() function.
• But you can loop through the set items using a for loop, or ask
if a specified value is present in a set, by using the in keyword.
• Example
for x in thisset:
print(x)
• Create a set of ten animals and use a for loop to display each
animal's name.
• Also, check if "lion" is present in the set or not.
• Once a set is created, you cannot change its items, but you
can add new items.
• To add items from another set into the current set, use the
update() method.
thisset.update(mylist)
print(thisset)
• Create a set for days and list for months finally merge using
update().
• Example
• You can loop through the set items by using a for loop:
• Example
• Create a set for month and print the elements using for loop.
• You can use the union() method that returns a new set
containing all items from both sets, or the update() method
that inserts all the items from one set into another:
set3 = set1.union(set2)
print(set3)
• Create two sets for name and roll no and combine them using
union().
print(10 > 9)
print(10 == 9)
print(10 < 9)