-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfor_loop.py
More file actions
67 lines (55 loc) · 1.71 KB
/
for_loop.py
File metadata and controls
67 lines (55 loc) · 1.71 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# to sort the list in ascending order without using inbuilt method
l = [98, 14, 62, 17, 56, 1, 5, 96]
for _ in range(len(l)-1):
for i in range(len(l)-1):
if l[i] > l[i+1]:
l[i], l[i+1] = l[i+1], l[i]
print(l)
###################################################################
# nth smallest number
l = [98, 14, 62, 17, 56, 1, 5, 96]
n = 5
for _ in range(len(l)-1):
for i in range(len(l)-1):
if l[i] < l[i+1]:
l[i], l[i+1] = l[i+1], l[i]
print(l[-n])
###################################################################
# longest word
sentence = "python is a programming language"
l = sentence.split()
for _ in range(len(l)-1):
for i in range(len(l) - 1):
if len(l[i]) > len(l[i+1]):
l[i], l[i+1] = l[i+1], l[i]
print(l[-1])
###################################################################
# longest non repeated word
sentence = "python is a programming language and programming is fun"
l = sentence.split()
max_word = ""
for word in l:
if len(word) > len(max_word) and l.count(word) == 1:
max_word = word
print(max_word)
####################################################################
# tuple of word and its count
sentence = "python is a programming language and programming is fun"
words = sentence.split()
l = []
for word in set(words):
l.append((word, words.count(word)))
print(l)
###################################################################
# factorial from 1 to 5
num = 5
fact = 1
l = []
for i in range(1, num+1):
fact = fact * i
l.append(fact)
print(l)
# list comprehension
from math import factorial
l = [factorial(i) for i in range(1, num + 1)]
print(l)