Skip to content

Commit b0bbca1

Browse files
author
Sahil
committed
Update lists, sets, tuples, dict
1 parent 6908f5f commit b0bbca1

File tree

5 files changed

+450
-1
lines changed

5 files changed

+450
-1
lines changed

day3/dictionaries.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# dict allows us to store data in (key, value) pairs
2+
# themselves are mutable but can contain only immutable types as keys, like sets
3+
# very useful for accessing data based on a key, quickly
4+
5+
# creating empty dict
6+
day_mapping = {}
7+
day_mapping = dict()
8+
9+
# dict with items (key, value) pairs
10+
day_mapping = {
11+
"Monday": 1,
12+
"Tuesday": 2,
13+
"Wednesday": 3,
14+
"Thursday": 4,
15+
"Friday": 5,
16+
"Saturday": 6,
17+
"Sunday": 0
18+
}
19+
print(len(day_mapping)) # 7
20+
21+
# items can be accessed based on the key, not the index
22+
print(day_mapping["Wednesday"]) # 3
23+
print(day_mapping.get("Sunday"))
24+
print(day_mapping.get("sunday")) # None, because the key is not present
25+
26+
# updating value for a particular key
27+
day_mapping["Sunday"] = 7
28+
# adding another key
29+
day_mapping["Default"] = 0
30+
31+
# keys, values and items
32+
print(day_mapping.keys())
33+
print(day_mapping.values())
34+
print(day_mapping.items())
35+
36+
# removing a particular key
37+
del(day_mapping["Default"])
38+
print(day_mapping.keys())

day3/lists.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# lists: container objects used to store related items together
2+
# let us say you were to input n numbers from user and find their sum, average, etc.
3+
# also you need the ability to sort the numbers etc
4+
# so you need some data type to store them easily, take input and print some required data
5+
6+
number_list = []
7+
number_list = [10, 4, 1, 5]
8+
print(type(number_list))
9+
10+
# 0-based indexing to access elements in the list
11+
# order is retained in the list!
12+
first_num = number_list[0]
13+
last_num = number_list[-1]
14+
print(f"First: {first_num}, Last: {last_num}")
15+
16+
# common methods on lists
17+
print(f"Number list: {number_list}")
18+
19+
num_count = len(number_list)
20+
print(f"Count: {num_count}")
21+
22+
number_list.append(7)
23+
print(f"Appended 7: {number_list}")
24+
25+
number_list.insert(1, 6)
26+
print(f"Insert 6 at index 1: {number_list}")
27+
28+
number_list.pop()
29+
print(f"Removed the last element: {number_list}")
30+
31+
# lists are mutable, can change the elements in-place
32+
# sorting lists, sorted method returns a new list
33+
ordered_num_list = sorted(number_list)
34+
print(f"Ordered number list: {ordered_num_list}")
35+
print(f"Original number list: {number_list}")
36+
37+
# sorting in place, descending order
38+
number_list.sort(reverse=True)
39+
print(f"Original number list: {number_list}")
40+
# reversing a list in-place
41+
number_list.reverse()
42+
print(f"Reversed number list: {number_list}")
43+
44+
# extending one list into another
45+
l1 = [1, 2, 3, 4]
46+
l2 = [5, 6, 7, 10]
47+
l1.extend(l2)
48+
print(l1)
49+
print(l2)
50+
51+
# checking if an item is present in a list (in keyword)
52+
print(5 in l1)
53+
print(8 in l2)
54+
55+
# split and join
56+
input_str = "1 2 3 4 5 6"
57+
input_list = input_str.split()
58+
print(input_list)
59+
60+
print(','.join(input_list))

day3/sets.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# sets help to store data in an unsorted way
2+
# an item can only be present once in a set, i.e. no duplicates
3+
# membership test is very fast
4+
# other powerful operations provided are: union, difference and intersection
5+
6+
prime_set = set()
7+
prime_set.add(1)
8+
prime_set.add(2)
9+
prime_set.add(3)
10+
prime_set.add(11)
11+
# order is not preserved in a set
12+
print(f"Prime set: {prime_set}")
13+
14+
# remove an item from a set
15+
prime_set.discard(1)
16+
print(f"Prime set: {prime_set}")
17+
18+
# x = prime_set[0] # cannot access set items using an index
19+
# prime_set.sort() # cannot sort items in a set
20+
# sorted method can still be used as that will convert set to a list
21+
22+
# single element set
23+
s = {1}
24+
print(s)
25+
print(type(s))
26+
27+
s = {}
28+
print(type(s)) # <class 'dict'>
29+
# to create empty set, rather use set constructor
30+
s = set()
31+
print(type(s)) # <class 'set'>
32+
33+
prime_set = {2, 3, 5, 5, 3, 7}
34+
print(prime_set) # duplicate values not present
35+
36+
# sets internally use hashing to store the elements and check if it is present or not
37+
# a numerical value is associated with any item in the set, try using hash() method
38+
# hash() function works only on immutable data types, so you cannot create a set of mutable types
39+
40+
names = {"scaler", "interviewbit", "scaler"}
41+
print(names)
42+
# both the following hash values will come out to be same! although values can change
43+
# depending on when the program is run
44+
print(hash("scaler"))
45+
print(hash("scaler"))
46+
47+
# set_of_lists = {[1, 2, 3], [1, 2, 3, 4]} # TypeError: unhashable type: 'list'
48+
49+
# we can convert a list into a set using the set constructor
50+
names = ["scaler", "interviewbit", "scaler"]
51+
names_set = set(names)
52+
print(names_set)
53+
54+
# update is used to update set with another sequence
55+
prime_set.update(names_set)
56+
print(prime_set)
57+
prime_set.update("scaler")
58+
# since string is a sequence, each of its characters will get added to the set!
59+
print(prime_set)
60+
61+
# union (|), intesection (&), difference (^)

day3/tuples.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# tuples are used to keep track of related but different items
2+
# they are immutable, you cannot change the items in it once created!
3+
# a use-case could be to store a row in a spreadsheet, given that
4+
# we need read-only snapshot of data
5+
6+
point = ()
7+
# point = collection of x, y and z coordinates!
8+
point = (3, 4, 5)
9+
print(point)
10+
print(type(point))
11+
12+
# checking for existence of an item
13+
print(3 in point)
14+
print(6 in point)
15+
16+
# access item using index as well
17+
print(f"x = {point[0]}, y = {point[1]}, z = {point[2]}")
18+
19+
# point[0] = 10 # TypeError: 'tuple' object does not support this assignment
20+
21+
# to create single item tuple, we need to give a comma after the item
22+
single_element = (1)
23+
print(type(single_element)) # <class 'int'>
24+
25+
single_element = (1, )
26+
print(single_element)
27+
print(type(single_element)) # <class 'tuple'>
28+
29+
# unpacking tuple data
30+
x, y, z = point
31+
print(x, y, z)
32+
33+
# functions can as well return multiple values with the help of tuples

0 commit comments

Comments
 (0)