Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb4f7d28

Browse files
committed
2024/03/24 - Updating Demo python files
1 parent8f7928f commitb4f7d28

File tree

4 files changed

+152
-17
lines changed

4 files changed

+152
-17
lines changed

‎concepts/PythonDemo01_Variables.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ def _inside_func() -> None:
5353
print("ID of b ",id(b))
5454
b=100
5555
print("after assigning new value ")
56+
print(f"Value of a:{a} and b:{b}")
5657
print("ID of a ",id(a))
5758
print("ID of b ",id(b))

‎concepts/PythonDemo02_DataTypes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,15 @@ def _set_data_type_demo() -> None:
144144

145145

146146
_set_data_type_demo()
147+
148+
149+
def_list_comprehension_demo():
150+
int_list= [10,20,30,40,11,13,23,24]
151+
odd_number_squares_list= [n*nforninint_listifn%2!=0]
152+
print("Input list:",int_list)
153+
print("Output list:",odd_number_squares_list)
154+
155+
156+
_list_comprehension_demo()
157+
158+

‎concepts/PythonDemo10_Abstraction.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,45 @@
44

55

66
classShape(ABC):
7+
8+
def__init__(self,shape_name=''):
9+
self.shapeName=shape_name
10+
711
defdraw(self):
812
pass
913

10-
defshow(self,shape_name=''):
11-
print(f"This is{shape_name} shape")
14+
defshow(self):
15+
print(f"This is{self.shapeName} shape")
1216

1317

1418
classTriangleShape(Shape):
1519

16-
def__init__(self,shape_name="Triangle"):
17-
self.shapeName=shape_name
20+
def__init__(self,a=0,b=0,c=0):
21+
super().__init__("Triangle")
22+
self.side1,self.side2,self.side3=a,b,c
1823

19-
defdraw(self,a=0,b=0,c=0):
20-
print("Triagle[side1={}, side2={}, side3={}]".format(a,b,c))
24+
defdraw(self):
25+
print("{}[side1={}, side2={}, side3={}]"
26+
.format(self.shapeName,self.side1,self.side2,self.side3))
2127
print("*"*50)
2228

2329

2430
classRectangleShape(Shape):
2531

26-
def__init__(self,shape_name="Rectangle"):
27-
self.shapeName=shape_name
32+
def__init__(self,length=0,breadth=0):
33+
super().__init__("Rectangle")
34+
self.length_,self.breadth_=length,breadth
2835

29-
defdraw(self,length=0,breadth=0):
30-
print("Rectangle[length={}, breadth={}".format(length,breadth))
36+
defdraw(self, ):
37+
print("{}[length={}, breadth={}"
38+
.format(self.shapeName,self.length_,self.breadth_))
3139
print("*"*50)
3240

3341

34-
s1=TriangleShape()
35-
s2=RectangleShape()
42+
s1=TriangleShape(10,20,30)
43+
s2=RectangleShape(15,25)
3644

3745
forsin [s1,s2]:
38-
s.show(s.shapeName)
39-
ifisinstance(s,TriangleShape):
40-
s.draw(10,20,30)
41-
else:
42-
s.draw(15,25)
46+
s.show()
47+
s.draw()
4348

‎concepts/PythonDemo15_RegEx.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# This file provides demo about regular expressions
2+
3+
importre
4+
5+
6+
defsimpleSearch():
7+
# create re object using compile method
8+
pattern="Hello"
9+
input_str="Hello, This is Hello demo!!"
10+
11+
print(f"Input:{input_str}")
12+
print(f"Search Pattern:{pattern}")
13+
14+
pattern_obj=re.compile(pattern)
15+
match_obj=pattern_obj.search(input_str)
16+
print("Is pattern matched in input string: ",bool(match_obj))
17+
ifbool(match_obj):
18+
print("Overall result",match_obj)
19+
print("Starting position of matched pattern: ",match_obj.start())
20+
print("End position of matched pattern: ",match_obj.end())
21+
print("Group : ",match_obj.group())
22+
23+
24+
defmatchDemo():
25+
pattern="This"
26+
input_str="Hello, This is regex demo !!"
27+
print("Result: ",re.match(pattern,input_str))
28+
29+
30+
deffullMatchDemo():
31+
pattern="[a-zA-Z0-9]*@{1}[a-z]{1,5}\\.[a-z]{1,3}"
32+
input_str="rohitphadtare39@gmail.com"
33+
print("Result: ",re.fullmatch(pattern,input_str))
34+
35+
36+
deffindAllDemo():
37+
pattern="[pP]{1}[a-z]*"
38+
input_str="Pooja Rohit phadtare"
39+
print("Result: ",re.findall(pattern,input_str))
40+
41+
42+
deffindItrDemo():
43+
pattern="[pP]{1}[a-z]*"
44+
input_str="Pooja Rohit Phadtare"
45+
print("Result: ")
46+
cnt=1
47+
foriinre.finditer(pattern,input_str):
48+
print(f"{cnt} match occur in input:{input_str} "
49+
f"at position "
50+
f"{i.start()} and group is{i.group()}")
51+
cnt+=1
52+
53+
54+
defsplitDemo():
55+
pattern=" "
56+
input_str="This is split demo"
57+
obj=re.compile(pattern)
58+
print("Split result with no max splits : ")
59+
print(obj.split(input_str))
60+
61+
print("Split result with no 1 splits : ")
62+
print(obj.split(input_str,1))
63+
64+
65+
defsubDemo():
66+
pattern="split"
67+
input_str="This is split demo !!"
68+
obj=re.compile(pattern)
69+
print(f"Input string:{input_str}")
70+
print(f"Output string:{obj.sub('substitute',input_str)}")
71+
72+
73+
defsubN_Demo():
74+
pattern="Java"
75+
input_str="This is Java REG EX demo!! Java is OOP language"
76+
obj=re.compile(pattern)
77+
print(f"Input string:{input_str}")
78+
output=obj.subn('python',input_str)
79+
print(f"Output:{output}")
80+
print(f"Output string:{output[0]}")
81+
print(f"Number of replaced instances:{output[1]}")
82+
83+
84+
defescapeDemo():
85+
pattern="[RA01.*@TST"
86+
input_str="[RA01.*@TST"
87+
pattern_with_escape=re.escape(pattern)
88+
print(f"Input:{input_str}")
89+
print(f"patten:{pattern}")
90+
print(f"escape patten:{pattern_with_escape}")
91+
print("Result: ",re.search(pattern,input_str))
92+
93+
94+
defmain():
95+
option=4
96+
ifoption==1:
97+
simpleSearch()
98+
elifoption==2:
99+
matchDemo()
100+
elifoption==3:
101+
fullMatchDemo()
102+
elifoption==4:
103+
findAllDemo()
104+
elifoption==5:
105+
findItrDemo()
106+
elifoption==6:
107+
splitDemo()
108+
elifoption==7:
109+
subDemo()
110+
elifoption==8:
111+
subN_Demo()
112+
elifoption==9:
113+
escapeDemo()
114+
115+
116+
if__name__=="__main__":
117+
main()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp