-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_tutorial.py
More file actions
231 lines (153 loc) · 3.28 KB
/
python_tutorial.py
File metadata and controls
231 lines (153 loc) · 3.28 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# printing statements
print("hello world")
#printing multiple statements
print("""
hello world
how are you?
wassup?""")
# VARIABLES
firstName = "shivani"
lastName = "Ghadigaonkar"
# assigning multiple value to multiple variables
fname, lname, rollno = "shivani", "Ghadigaonkar", 40
print(fname)
print(lname)
print(rollno)
# assigning same value to multiple variables
a = b = c = 30
print(a)
print(b)
print(c)
# LIST
a = [1, 2.2, 'python']
print(a)
# adding element to a list
a.append("javaScript")
print(a)
# removing elements from a list
a.remove(a[0])
print(a)
a.remove("javaScript")
print(a)
# Inserting element in a particular position
a.insert(1,"Java")
print(a)
# Extending the list
b = [1,2,3]
a.extend(b)
print(a)
# slicing
print(a[0])
print(a[1:3])
print(a[-1])
print(a[::-1])
# list of list
list1 = [[1,3],[1,4],[6]]
print(list1[2][0])
list1.append((5,6))
print(list1)
#TUPLE
t = (1,2,3)
s = (2,3,4)
print(t+s)
print(list1*3)
print(list(t))
print(tuple(list1))
# DICTIONARY
dict1 = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print("dict['Name']: ", dict1['Name'])
print("dict['Age']: ", dict1['Age'])
# updating the Dictionaries
dict1['Age'] = 8; # update existing entry
dict1['School'] = "DPS School"; # Add new entry
print("dict['Age']: ", dict1['Age'])
print("dict['School']: ", dict1['School'])
# function dict()
Dict = dict([(1, 'Geeks'), (2, 'For')])
print("\nDictionary with each item as a pair: ")
print(Dict)
# LOOPS
list4 = [1, 2, 3, 4, 5]
for num in list4:
print(num)
# sum1 = 0
# for num in list4:
# sum1 = sum1 + num
# print(sum1)
# while loop
int i = len(list4)
while(i<0):
print(list4[i])
i = i-1
# IF STATEMENT
num = 3
if num > 0:
print(num, "is a positive number.")
print("This is always printed.")
num = -1
if num > 0:
print(num, "is a positive number.")
print("This is also always printed.")
# FUNCTION
def greet(name):
"""This function greets to
the person passed in as
parameter"""
return("Hello, " + name + ". Good morning!")
# EXCEPTION HANDLING
import sys
print( "Lets fix the previous code with exception handling")
try:
number = int(input("Enter a number between 1 - 10 "))
except ValueError:
print( "Err.. numbers only")
finally:
print( "you entered number ", number)
#regEX
import re
# matches()
pattern = '^a...s$'
test_string = "abyss"
result = re.match(pattern, test_string)
if result:
print("Search successful.")
else:
print("Search unsuccessful.")
# findall()
string = 'hello 12 hi 89. Howdy 34'
pattern = '\d+'
result = re.findall(pattern, string)
print(result)
# split()
string = 'hello 12 hi 89. Howdy 34'
pattern = '\d+'
result = re.split(pattern, string)
print(result)
#sub()
# multiline string
string = 'abc 12\
de 23 \n f45 6'
pattern = '\s+'
replace = '$'
new_string = re.sub(pattern, replace, string)
print(new_string)
# subn()
# Program to remove all whitespaces
import re
# multiline string
string = 'abc 12\
de 23 \n f45 6'
# matches all whitespace characters
pattern = '\s+'
# empty string
replace = '$'
new_string = re.subn(pattern, replace, string)
print(new_string)
# search()
string = "Python is fun"
# check if 'Python' is at the beginning
match = re.search('\APython', string)
if match:
print("pattern found inside the string")
else:
print("pattern not found")