Created
September 28, 2022 06:07
-
-
Save Dbhardwaj99/7ef911338cd66242343865308ef93637 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Question 1 | |
list1 = [] | |
for i in range(20): | |
if i % 2 == 0 or i % 4 == 0: | |
list1.append(i) | |
# Question 2 | |
def square(x): | |
return x ** 2 | |
squares = list(map(square, range(1, 11))) | |
print(squares) | |
summ = 0 | |
for i in squares: | |
summ += i | |
print("Sum = ", summ) | |
# Question 3 | |
country = ["Brazil", "India", "China", "Russia", "Sri Lanka"] | |
country_name = input("Enter the name of the country : ") | |
if country_name in country: | |
print(country_name, "is a member of BRIC") | |
else: | |
print(country_name, "is not a member of BRIC") | |
# Question 4 | |
list3 = [] | |
for i in range(10): | |
list3.append(i) | |
for i in list3: | |
if i % 2 == 0: | |
list3.remove(i) | |
print(list3) | |
# Question 5 | |
list4 = [2, 34, 2, 43, 5, 623, 1223, 3243, 234, 23423, 43, 34, 43] | |
num1 = int(input("Enter the number you want to search:\n")) | |
i = 0 | |
print(num1, "has", list4.count(num1), "apperences in the list") | |
while i < len(list4): | |
if list4[i] == num1: | |
print(num1, "is found on", i, "index location in the list") | |
i += 1 | |
# Question 6 | |
word1 = ["Divyansh", "Python"] | |
word2 = ["Loves", "Programmming"] | |
list5 = [] | |
for i in word1: | |
for j in word2: | |
wordFinal = i + " " + j | |
list5.append(wordFinal) | |
print(list5) | |
# Question 8 | |
list8 = [] | |
for i in list4: | |
if i not in list8: | |
list8.append(i) | |
print(list8) | |
# Question 9 | |
starting_range = int(input("Enter the starting range:\n")) | |
ending_range = int(input("Enter the ending range:\n")) | |
steps = int(input("Enter the steps:\n")) | |
list9 = [] | |
for i in range(starting_range, ending_range, steps): | |
list9.append(i) | |
list9 = reversed(list9) | |
for i in list9: | |
print(i) | |
# Question 10 | |
import random | |
list10 = [] | |
list11 = [] | |
list12 = [] | |
for i in range(10): | |
list10.append(random.randint(0, 100)) | |
for i in list10: | |
if i % 2 == 0: | |
list11.append(i) | |
else: | |
list12.append(i) | |
print("All the even element are:\n", list11) | |
print("All the odd element are:\n", list12) | |
# Question 11 | |
list13 = [] | |
j = 1 | |
for i in range(20): | |
list13.append(j) | |
j += 2 | |
print(list13) | |
# Question 12 | |
list14 = [34, 2, 213, 45, 123, 345, 123, 24, 1234, 234] | |
list15 = [] | |
def multiple10(x): | |
return x * 10 | |
for i in list14: | |
list15.append(multiple10(i)) | |
# Question 13 | |
import functools | |
def max_ele(x, y): | |
if x > y: | |
return x | |
else: | |
return y | |
num_list = [4, 1, 8, 2, 9, 3, 0] | |
print("Largest value in the list is : ", functools.reduce(max_ele, num_list)) | |
# Question 14 | |
L = [lambda x: x * 2, lambda x: x * 3, lambda x: x * 4] | |
num2 = int(input("Enter a number:\n")) | |
for i in L: | |
print(i(num2)) | |
# Question 15 | |
celcius_temp_list = [34, 32, 36, 44, 100, 0, 23] | |
farenheit_temp_list = [] | |
for i in celcius_temp_list: | |
farenheit_temp_list.append(i * (9 / 5) + 32) | |
i = 0 | |
while i < len(celcius_temp_list): | |
print(celcius_temp_list[i], "When converted into farenheit is", farenheit_temp_list[i]) | |
# Question 16 | |
nterms = int(input("How many terms? ")) | |
list16 = [] | |
n1, n2 = 0, 1 | |
for i in range(nterms): | |
list16.append(n1) | |
nth = n1 + n2 | |
# update values | |
n1 = n2 | |
n2 = nth | |
print(list16) | |
i = 1 | |
summ = 0 | |
while i < len(list16): | |
summ = summ + list16[i] | |
i = i + 1 | |
print("sum of all the even terms are ", summ) | |
# Question 17 | |
list17 = [] | |
print("Enter a number or Enter ### to end:") | |
for i in range(100): | |
num3 = input() | |
if num3 == "###": | |
break | |
else: | |
list17.append(int(num3)) | |
list17.sort() | |
print(list17) | |
if len(list17)%2 == 0: | |
print("The median of the given list is ", (list17[int(len(list17) / 2) - 1] + list17[int(len(list17) / 2)]) / 2) | |
else: | |
print("The median of the given list is ", list17[int(len(list17) / 2)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment