Skip to content

Commit 91c0176

Browse files
# Dictionaries
1 parent 06e6492 commit 91c0176

3 files changed

Lines changed: 199 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#list is python
2+
student_present=["sunday","monday","tuesday"]
3+
print(student_present[0])
4+
student_present[0]="sunday morning"
5+
print(student_present)
6+
#length
7+
print(len(student_present))
8+
9+
#appending
10+
student_present.append("wednesday")
11+
print(student_present[-1])
12+
13+
#slicing
14+
#[sunday morning, monday, tuesday, wednesday]
15+
print(student_present[1:3]) # this will give us a new list with the elements from index 1 to index 2 (3 is not included)
16+
17+
#negativve slicing
18+
print(student_present[-3:-1]) # this will give us a new list with the elements from index -3 to index -2 (-1 is not included)
19+
20+
list1=[1,2,3]
21+
list2=list1[:] # this will give us a new list with the same elements as list1 but it will be a different list in memory
22+
print(list1)
23+
print(list2)
24+
list1.append(4)
25+
print(list1)
26+
print(list2) # this will also give us [1,2,3,4] because list1 and list2 are pointing to the same list in memory
27+
28+
29+
list1[0] = [10,20,30]
30+
print(list1)
31+
print(list2)
32+
print(list1[0][0]) # this will give us 10
33+
34+
#insert
35+
student_present.insert(1,"thursday")
36+
print(student_present)
37+
38+
39+
#extend
40+
friends_list1=["ram","shyam","hari"]
41+
friends_list2=["sita","gita","radha"]
42+
friends_list1.extend(friends_list2)
43+
print(friends_list1)
44+
print(friends_list2)
45+
46+
#remove
47+
friends_list1.remove("shyam")
48+
print(friends_list1)#does not include shyam
49+
50+
#pop
51+
print(friends_list1.pop()) # this will remove the last element from the list and return it
52+
print(friends_list1)
53+
poped_friend=friends_list1.pop(1) # this will remove the element at index 1 and return it
54+
print(friends_list1)
55+
print(poped_friend)
56+
57+
58+
#del
59+
del friends_list1[0] # this will remove the element at index 0 from the list
60+
print(friends_list1)
61+
del friends_list2 # this will delete the entire list from memory
62+
63+
# print(friends_list2) # this will give us an error because friends_list2 has been deleted from memory
64+
65+
#clear
66+
friends_list1.clear() # this will remove all the elements from the list but the list itself will still exist in memory
67+
print(friends_list1) # this will give us an empty list []
68+
69+
#copy list
70+
list3=list1.copy() # this will give us a new list with the same elements as list1 but it will be a different list in memory
71+
list3=list1[:] # this will also give us a new list with the same elements as list1 but it will be a different list in memory
72+
73+
#adding and extend
74+
list4=[1,2,3]
75+
list5=[4,5,6]
76+
list6=list4+list5 # this will give us a new list with the elements of list4 and list5
77+
print(list6)
78+
list4.extend(list5) # this will add the elements of list5 to the end of list4
79+
print(list4)
80+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#int
2+
tup1=(1, 2, 3, 4, 5)
3+
tup2=(6, 7, 8, 9, 10)
4+
print(tup1[0]) # this will give us the first element of the tuple which is 1
5+
print(tup1[-1]) # this will give us the last element of the tuple which
6+
print(tup1[0:3]) # this will give us a new tuple with the elements from index 0 to index 2 (3 is not included)
7+
print(tup1[-3:-1]) # this will give us a new tuple with the
8+
9+
tup3= tup1 + tup2 # this will give us a new tuple with the elements of tup1 and tup2
10+
tup2= tup2 * 100 # this will give us a new tuple with the elements of tup2 repeated twice
11+
print(tup3)
12+
print(tup2)
13+
14+
#unpacking a tuple
15+
tup4=(1, 2, 3)
16+
(a, b, c) = tup4 # this will unpack the elements of the tuple into the variables a, b, and c
17+
print(a) # this will give us 1
18+
print(b) # this will give us 2
19+
print(c) # this will give us 3
20+
21+
tup5=(1, 2, 3, 4, 5)
22+
(a, b, *rest) = tup5 # this will unpack the first two elements of
23+
# the tuple into the variables a and b, and the rest of the elements into the variable rest
24+
print(a) # this will give us 1
25+
print(b) # this will give us 2
26+
print(rest) # this will give us [3, 4, 5] because the rest of the elements are stored in a list
27+
28+
29+
#mutating tuple
30+
tup6=(1, 2, 3)
31+
# tup6[0] = 10 # this will give us an error because tuples are
32+
# list are mutable so we can convert the tuple to a list, mutate the list, and then convert it back to a tuple
33+
list1=list(tup6) # this will give us a new list with the same elements
34+
print(list1)
35+
list1[0] = 10 # this will mutate the list by changing the first element to 10
36+
print(list1)
37+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Dictionaries
2+
letslearnuser={
3+
"name":"saugat",
4+
"age": 22,
5+
"course":["python","django","js"]
6+
7+
8+
}
9+
print(letslearnuser)
10+
#couse python
11+
print(letslearnuser["course"][0]) # this will give us the first element of the course list which is python
12+
#length
13+
print(len(letslearnuser)) # this will give us the number of key-value pairs in the dictionary which is 3
14+
15+
16+
#modifying
17+
letslearnuser["age"] = 23 # this will modify the value of the age key to 23
18+
19+
#adding a new key-value pair
20+
letslearnuser["email"] = "saugat@example.com"
21+
print(letslearnuser)
22+
23+
#keys
24+
keys=letslearnuser.keys() # this will give us a list of all the keys in the dictionary
25+
#keys after mutating
26+
print(keys) # this will give us a list of all the keys in the dictionary after mutating it
27+
letslearnuser["name"] = "saugat shrestha" # this will modify the value of the name key to saugat shrestha
28+
letslearnuser["surname"] = "shrestha" # this will add a new key-value pair to the dictionary with the key surname and the value shrestha
29+
print(keys) # this will give us a list of all the keys in
30+
31+
#values
32+
values=letslearnuser.values() # this will give us a list of all the values in the dictionary
33+
print(values) # this will give us a list of all the values in the dictionary
34+
letslearnuser["age"] = 24 # this will modify the value of the age key
35+
print(values) # this will give us a list of all the values in the dictionary after mutating it
36+
37+
38+
# items
39+
items=letslearnuser.items() # this will give us a list of all the key-value pairs in the dictionary as tuples
40+
print(items) # this will give us a list of all the key-value pairs in the dictionary
41+
letslearnuser["course"].append("react") # this will modify the value of the course key by appending react to the list
42+
print(items) # this will give us a list of all the key-value pairs in the dictionary after mutating it
43+
44+
45+
46+
#if i want to acess the value name
47+
Items_dict=letslearnuser.items() # this will give us a list of all the key-value pairs in the dictionary as tuples
48+
print(Items_dict) # this will give us a list of all the key-value pairs in the dictionary
49+
50+
# removing items
51+
letslearnuser.pop("surname") # this will remove the key-value pair with the key surname from the dictionary
52+
print(letslearnuser)
53+
#del
54+
del letslearnuser["email"] # this will remove the key-value pair with the key email from the dictionary
55+
print(letslearnuser)
56+
57+
#clear
58+
letslearnuser.clear() # this will remove all the key-value pairs from the dictionary
59+
#del list
60+
del letslearnuser # this will delete the dictionary from memory
61+
62+
#nested dictionary
63+
nested_dict={
64+
"name":"saugat",
65+
"age": 22,
66+
"course":{
67+
"python":"beginner",
68+
"django":"intermediate",
69+
"js":"advanced"
70+
}
71+
}
72+
print(nested_dict)
73+
#acessing begainers
74+
print(nested_dict["course"]["python"]) # this will give us the value of the python key in the course dictionary which is beginner
75+
76+
#copy
77+
copy_dict=letslearnuser.copy() # this will give us a new dictionary with the same key-value pairs as letslearnuser
78+
print(copy_dict) # this will give us a new dictionary with the same key-value pairs as
79+
80+
#copy using dict constructor
81+
copy_dict2=dict(letslearnuser) # this will give us a new dictionary with the same key-value pairs as letslearnuser
82+
print(copy_dict2) # this will give us a new dictionary with the same key-value pairs

0 commit comments

Comments
 (0)