forked from Ankitkundu21/Hacktoberfest-python-code-bunch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-code-bunch.py
More file actions
50 lines (43 loc) · 1.11 KB
/
python-code-bunch.py
File metadata and controls
50 lines (43 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#Q1:SUM OF DIGITS [USING append()]
#SOLUTION :
digits=[]
sum=0
for i in range(21):
digits.append(i)
for i in digits:
sum+=i
print(digits)
print("Sum of digits is : ", sum)
#Q2: CHECK EVEN NUMBERS BETWEEN CERTAIN RANGE OF VALUE
#SOLUTION :
def check(x):
if(x % 2 == 0 or x % 4 == 0):
return x
evens=list(filter(check, range(2,30)))
print(evens)
#Q3:FIND THE SECOND LARGEST NUMBER (LIST)
#SOLUTION
digits=[]
value = (int(input("Number of element you wanna insert : ")))
for i in range(1, value + 1):
item = int(input("Enter the no-{} element: ".format(i)))
digits.append(item)
digits.sort()
print("Second largest number is : " , digits[-2])
#Q4: SUM OF DIGITS ( USING REDUCE() )
#SOLUTION :
import functools # functools is a module that contains function reduce()
def sod(x,y):
return x+y
list=[]
for i in range(0,11):
list.append(i)
print("Sum of digits value is : ")
print(functools.reduce(sod, list))
#Q5: #Swap elements based on position:
#SOLUTION :
list=[62,12,8,76]
def newlist(list):
list[1],list[2]= list[2], list[1]
return list
print(newlist(list))