forked from Shwetha-Kalavara/Python_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprograms (1).py
More file actions
132 lines (61 loc) · 2.24 KB
/
Copy pathprograms (1).py
File metadata and controls
132 lines (61 loc) · 2.24 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# generate square numbers of given list
l = [1, 2, 3, 4]
def squares(list_):
for item in list_:
yield item ** 2
res = squares(l)
# print(list(res))
# list comprehension
sq_ = [item ** 2 for item in l]
# print(sq_)
# generator expression
square = (item ** 2 for item in l)
# print(list(square))
##############################################################################
# generate only the strings with odd length in the given list
names = ["bob", "steve", "alex", "maya", "john"]
def odd_length(list_):
for item in list_:
if len(item) % 2:
yield item
x = odd_length(names)
# print(list(x))
# generator expression
odd_ = (item for item in names if len(item) % 2)
# print(list(odd_))
#############################################################################
# generate a tuple of only numeric values in the given list
items = ["flipkart", 2021, "gmail", 1.2, [1, 2, 3], 2+3j, True]
def num_(iterable):
for item in iterable:
if isinstance(item, (int, float, complex)):
yield item
a = num_(items)
# print(tuple(a))
# generator expression
num = tuple(item for item in items if isinstance(item, (int, float, complex)))
# print(num)
##############################################################################
# generate a list -> if individual datatype, reverse it else keep it as it is
items = ["flipkart", 2021, "gmail", 1.2, [1, 2, 3], 2+3j, True]
def gen_list(list_):
for item in list_:
if isinstance(item, (int, float, complex)):
yield str(item)[::-1]
else:
yield item
d = gen_list(items)
print(list(d))
# generator expression
g = (str(item)[::-1] if isinstance(item, (int, float, complex)) else item
for item in items)
# print(list(g))
##########################################################################################
# Generating List of PI values with increasing decimal point numbers up to user defined number
import math
PI = math.pi
print(PI)
def pi_gen(num_of_decimals):
for i in range(num_of_decimals):
yield round(PI, i)
# print(list(pi_gen(4)))