-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw3.py
153 lines (130 loc) · 2.39 KB
/
w3.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import platform
x=platform.system()
print(x)
#list all names in platform module
import platform
x=dir(platform)
print(x)
#python dates
import datetime
x=datetime.datetime.now()
print(x)
#create date objects
import datetime
x=datetime.datetime(2025,2,11)
print(x)
#strtime method
import datetime
x=datetime.datetime(2025,2,11)
print(x.strftime("%d"))
#python math
x=min(5,10,25)
y=max(5,10,25)
print(x)
print(y)
#absolute numbers
x=abs(-7.25)
print(x)
#power values
x=pow(4,3)
print(x)
#the math module
import math
x=math.sqrt(81)
print(x)
import math
x=math.pi
print(x)
#json in python
import json
x='{"name":"john","age":30,"city":"newyork"}'
y=json.loads(x)
print(y["age"])
#json.dumps()
import json
x={
"name":"john",
"age":36,
"country":"Kenya"
}
y=json.dumps(x)
print(y)
#convert python objects in json strings
import json
print(json.dumps({"name":"john","age":30}))
print(json.dumps(["apple","bananas"]))
print(json.dumps(("apple","bananas")))
print(json.dumps("hello"))
print(json.dumps(43))
print(json.dumps(39.42))
print(json.dumps(True))
print(json.dumps(False))
print(json.dumps(None))
import json
x={
"name":"john",
"age":30,
"married":True,
"divorced":False,
"children":("Ann","Billy"),
"pets":None,
"cars":[
{"model":"BMW230","mpg":27.5},
{"model":"ford edge","mpg":24.1}
]
}
print(json.dumps(x,indent=4,separators=(".","=")))
#format the results using indents
import json
x={
"name":"john",
"age":30,
"married":True,
"divorced":False,
"children":("Ann","Billy"),
"pets":None,
"cars":[
{"model":"BMW230","mpg":27.5},
{"model":"ford edge","mpg":24.1}
]
}
print(json.dumps(x,indent=4,sort_keys=True))
#python Regular Expression
#RegEx module
import re
txt="The rain in Spain"
x=re.search("^The.*Spain$",txt)
if x:
print("Yes, we have a match")
else:
print("No match")
#findall() functions
import re
txt="The rain in spain"
x=re.findall("ai",txt)
print(x)
#split() function
import re
txt="The rain in Spain"
x=re.split("\s",txt)
print(x)
#sub() function
import re
txt="the rain in spain"
x=re.sub("\s","9",txt)
print(x)
#span() funtion
import re
txt="The rain in Spain"
x=re.search(r"\bS\w+",txt)
print(x.span())
#string function
import re
txt="The rain in Spain"
x=re.search(r"\bS\w+",txt)
print(x.string)
#group()
import re
txt="The rain in Spain"
x=re.search(r"\bS\w+",txt)
print(x.group())