8000 new exercises · htudu/python_exercise@16c0fc4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 16c0fc4

Browse files
author
puja kumari
committed
new exercises
1 parent 6a67e02 commit 16c0fc4

14 files changed

+131
-3
lines changed

puja/dict_of_list.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# using two list, create dictionary.
2+
fruits = ['Banana', 'kiwi', 'Pomegranate', 'Apple']
3+
nutrition = ['calories', 'immunity', 'fiber', 'Vitamin']
4+
5+
# creating dict using zip method
6+
def dict_of_list(lst1,lst2):
7+
d = dict(zip(lst1,lst2))
8+
return d
9+
10+
print(f"new dictionary of two list : {dict_of_list(fruits, nutrition)}")
11+
12+
# creating dict using lambda
13+
res = dict(map(lambda i,j : (i,j) , fruits,nutrition))
14+
print(res)
15+
16+
# creating dict using for loop
17+
def listdictionary(lst1, lst2):
18+
19+
for i in range(len(lst1)):
20+
# print(i)
21+
result = {lst1[i] : lst2[i]}
22+
print(f"new dictionary of list : {result}")
23+
24+
listdictionary(fruits,nutrition)
25+
26+
tmp = {}
27+
for i in range(len(fruits)):
28+
tmp[fruits[i]] = nutrition[i]
29+
# tmp["banana"] = "calories"
30+
31+
print(tmp)

puja/find_5th_itm_in_list.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Generate 10 random list, print the 5th item of the lists of all
12
import random
23
def random_generator():
34
random_1 = []
@@ -24,7 +25,7 @@ def random_generator():
2425
random_10.append(x)
2526
return random_1, random_2, random_3, random_4, random_5, random_6, random_7, random_8, random_9, random_10
2627
xx = random_generator()
27-
print(xx)
28+
# print(xx)
2829

2930

3031
def generate_list(size):
@@ -35,6 +36,8 @@ def generate_list(size):
3536
return tmp
3637

3738
for i in range(10):
38-
x = random.randint(5, 15)
39+
# x = random.randint(5,11)
40+
# ==== fixed length of list is 10
41+
x = 10
3942
tmp = generate_list(x)
40-
print(f"list {i} - {tmp}")
43+
print(f"list {i} - {tmp} - 5th item {tmp[4]}")

puja/find_indexs_of_item.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# find the indexs of the items in a list
2+
list1 = [2, 5, 1, 7, 9, 4, 3, 12, 16, 20 ]
3+
x = 12
4+
def index_of_items(lst):
5+
for i, a in enumerate(list1):
6+
print(f" index of item {a} : {i}")
7+
8+
def index_of_selected_item(lst, itm):
9+
x = lst.index(itm)
10+
print(f"index of item in list : {x}")
11+
12+
def index_of_item(ls):
13+
for i, a in enumerate(list1):
14+
if a == 7:
15+
print(f" index of item : {i}")
16+
17+
index_of_items(list1)
18+
index_of_selected_item(list1, x)
19+
index_of_item(list1)

puja/no_of_item_in_list.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# How to find the number of items in a list.
2+
list1 = [6, 8, 12, 15, 3, 18, 7]
3+
4+
# 1st approach
5+
print(f"total number of items in list is : {len(list1)}")
6+
7+
# 2nd approach
8+
count = 0
9+
for i in list1:
10+
count = count+1
11+
print(f"total number of items in list is : {count}")

puja/product_of_list.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# find the product of the list of list and store in new list
2+
3+
ls = [[2,8],[6,10],[3,2,5],[4,8,2]]
4+
def product_of_lst(lst):
5+
new_list = []
6+
for i in lst:
7+
product = 1
8+
for j in i:
9+
product = product * j
10+
new_list.append(product)
11+
return new_list
12+
# return product
13+
14+
print(product_of_lst(ls))
15+

puja/random_function_generator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Generate random number in a list using function
12
import random
23
def random_generator(n):
34
list_of_random = []

puja/random_length_of_list.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Generate 10 random length of list
2+
import random
3+
def random_generator(n):
4+
list_of_random = []
5+
for i in range(n):
6+
x = random.randint(1,100)
7+
list_of_random.append(x)
8+
return list_of_random
9+
10+
for i in range(10):
11+
# random number of list and size of list is also random
12+
x = random.randint(5, 15)
13+
xx = random_generator(x)
14+
print(f"list - {i} : {xx}")

puja/random_no.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Generate random number in a list
12
import random
23
list_of_random = []
34

puja/remove_list_item.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# remove an element from the list
12
list1 = [1, 2, 5, 8, 3, 6]
23
t = 5
34

puja/reverse_of_random_number.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Given list of 10 item , find the reverse of list
12
import random
23
def random_generator():
34
list_of_random = []

0 commit comments

Comments
 (0)
0