0% found this document useful (0 votes)
4 views3 pages

Overview of Data Structures in Python

The document provides an overview of four main types of data structures: lists, tuples, sets, and dictionaries. It explains the characteristics and functionalities of each type, including how to manipulate lists and dictionaries using various methods. Examples are provided to illustrate how to use these data structures effectively in programming.

Uploaded by

manish.ekbana42
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Overview of Data Structures in Python

The document provides an overview of four main types of data structures: lists, tuples, sets, and dictionaries. It explains the characteristics and functionalities of each type, including how to manipulate lists and dictionaries using various methods. Examples are provided to illustrate how to use these data structures effectively in programming.

Uploaded by

manish.ekbana42
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# A data struture is some statements that take some values and organizez them into

one thing
'''
there are four types of data structures:
the list
the tuple
the set
the dictionary

'''

'''
LISTS:
A data structure where you can list things in order and change things after
they have been writtten
'''
pets=["cat", "dog", "fish"]
print(pets)
# because lists are in order and the computer keeps track of what the order is you
can find the values like this (list[order placement])
print(pets[2])
# did you see what happened there. It gave us fish even though it;s the third ting
in telist not the second. This happened because the computer started counting (or
indexing) from zero.
# we can also reassign each element
pets[2]="lizard"
'''
We can so use functions on the list to do different things here are a few that are
useful
the pop function deletes one of the values in the list
the function will delete the value at the placement that you give it.
example_list.pop(placement)
if the placement was two the third item would have been deleted
without giving the function any placement it will delete the last value
in the list
example_list.pop()
the append function tacks on a new value at the end of the list
example_list.append("new value")
the insert function adds a new value into whatever position you want and you
also have to give what you want to input.
example_list.insert(placement, "new value")
WARNING the insert function will not work without giving it any arguments
so make sure you know where you want to put your new value
the reverse function is very simple it just reverses the order of

'''

# here are some example of everything I was talking about


[Link](1)
print(pets)
[Link]()
print(pets)
[Link]("lizard")
print(pets)
[Link](1, "cat")
print(pets)

'''
TUPLES:
A data structure where you can list things in order without being able to
change things later on
'''
# tuples are basicallyjust lists but without any of the fun features of lists
fruits = ("apples", "oranges", "bananas")
# you can still find certain values by using the position of the value
print(fruits[1])
# other tan that cant do muc with it.

'''
SETS:
A data structure where tere is just a group of things and no repeating or order
'''
people_that_I_know = {"Mason", "Manish Sir", "Rishav", "Evan 'Tornado' Graun",
"Harrison 'Hercules' Taylor"}
print(people_that_I_know)
# there is no order a list is like whenever there is a list of how may people are
trying out for the team while a set could just be the people trying out
# they are just a list of things each equally as important
# other than that listsare pretty boring

'''
DICTIONARY:
A data structure were the values are organized by keys (fancy word for other
values) instead of simple indexing
'''
ford_mustang= {
"brand" : "Ford",
"year produced" : 2012,
"model": "Mustang"
}
print("the dictionary:", ford_mustang)
print("The brand of the ford mustang is", ford_mustang["brand"])
# Dictionaries are changable and ordered but not through usual means. They are
ordered through the keys and not indexing
# Don't think that you can only use the boring datatypes like strings and integers,
you can use all of them.
year=1964
ford_mustang= {
"brand" : "Ford",
"year produced" : 2012,
"model": "Mustang",
"gasoline": True,
"colors" : ["Black", "Candy Red", "Ingot Silver", "etc" ],
"year introduced" : year
}
# You can use all the datatypes but you can also even use comprehension.
rectangle_perimeter = {x:x+x*x for x in range(11)}
print(rectangle_perimeter)
# even weirder you can also do a dictionary inside of a dictionary
student_database ={
"Michael":{"SocSt" :84, "Math":92, "ELA" :88, "Science":96},
"John" :{"SocSt" :93, "Math":92, "ELA" :94, "Science":91},
"Jack":{"SocSt" :83, "Math":82, "ELA" :88, "Science":86},
"Sakshyam":{"SocSt" :100, "Math":100, "ELA" :100, "Science":100},
"Rishav":{"SocSt" :45, "Math":37, "ELA" :58, "Science":16},
"Grant":{"SocSt" :95, "Math":83, "ELA" :99, "Science":87},
}
# Now how would you even call on this?
# let's see what happens if I only call on the name
print(student_database["Grant"])
# That gives us all his grades. While this might be useful what if I only wanted
his math grade.
print(student_database["Grant"]["Math"])
# Woohoo! that worked. I got just his math grade.
# But you can even nest it again
very_nest = {"student_database":{
"Michael":{"SocSt" :84, "Math":92, "ELA" :88, "Science":96},
"John" :{"SocSt" :93, "Math":92, "ELA" :94, "Science":91},
"Jack":{"SocSt" :83, "Math":82, "ELA" :88, "Science":86},
"Sakshyam":{"SocSt" :100, "Math":100, "ELA" :100, "Science":100},
"Rishav":{"SocSt" :45, "Math":37, "ELA" :58, "Science":16},
"Grant":{"SocSt" :95, "Math":83, "ELA" :99, "Science":87},
}}
# this is the same thing as last time just more brackets
print(very_nest["student_database"]["Grant"]["Math"])
# You can also merge dictionaries
dict_1 = {'a': 1, 'b': 2}
dict_2 = {'c': 3, 'd': 4}
merged_dict = {**dict_1, **dict_2}
# careful if they have te same key then the second value will override the original
one.
print("Merged dictionary:", merged_dict)
# I mentioned modifying the dictionary but how would you even do that?
# Adding a new key key-value pair is easy
ford_mustang["mustangs produced"] = 10000000
print("added key-value pair to ford mustang:", ford_mustang)
# What about deleting a key-value pair?
# it's simple just write del in front of the key
del ford_mustang["mustangs produced"]
# But now you wanna change something about a pre existing key-value pair?
# all you have to do is reassign the values it's like adding a new value but you
just use the same key as a pre existing one
ford_mustang["colors"] = ["Black", "Candy Red", "Ingot Silver", "Oxford White",
"Atlas Blue", "etc" ]
# You also can retrieve all the info in your dictionaries
# Retreive all items:
print(list(ford_mustang.items()))
# Retreive all keys:
print(list(ford_mustang.keys()))
# Retreive all values:
print(list(ford_mustang.values()))
# You can print out your dictionaries also by using for loops
for key, value in ford_mustang.items():
print(key, ":", value)
# There is something even more secret than just using the normal method of making a
dict. it is called the dict() constructor
me=dict(name= "Sakshyam", age = 12, hobbies = ["reading", "coding", "playing video
games"])
# 10. Dictionary Filtering
# Filter dictionary based on condition
datatypes_in_mustang = {k: v for k, v in ford_mustang.items() if isinstance(v,
vars) and not isinstance(v, bool)}
print("Filtered dictionary:", datatypes_in_mustang)

You might also like