-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.py
executable file
·286 lines (239 loc) · 5.75 KB
/
hello.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/local/bin/python3
print('Hello World')
a,b = 55,1
#Assigning zero to a and 1 to b
#In python you can assign multiple values at the
#same time.
if a<b :
#THis is how you write conditional statments.
print('a({}) is lees than be{}'.format(a,b))
#This ({}) shows the value of a and be in
#The print stamtent
else:
print('a({}) is NOT less than b({})'.format(a,b))
#One line condional sttatments
print("one" if a<b else "Two")
c,d = 0,1
#while loops
while d<50:
print(d)
c,d = d,c+d
#opening a file
fh = open("lines.txt")
#for loops
for line in fh.readlines():
print(line)
#Defining the end of the new line
# to be only space not another new line
fh1 = open('lines.txt')
for line in fh1.readlines():
print(line, end='')
# C type for loop
for e in range (4,7):
print('e value is ({})'.format(e))
#Defining Functions
# Be carefull about the : after def,for,if
# use the exact False and True.
#
def isprime(n):
if n==1:
print('1 is special')
return False
for x in range(2, n):
if n%x ==0:
print(n,'is No prime')
return False
else:
print(n,'is Prime')
return True
for n in range(1,20):
isprime(n)
# working with Generator functions
# I have to revisit this part again
def isprimgen(n):
if n==1:
return False
for x in range(2,n):
if n%x == 0:
return False
else:
return True
def primes(n=1):
while(True):
#yield is like a return, it returns a value but the next time that the
# function get called is continue after the yeild.
if isprimgen(n):
yield n
print('yield is called on ',n)
n +=1
print("NOT YIELD ",n)
for n in primes():
if n>100:break
print('The actual printed ',n)
#Object oriented programming in python
class fibonachi():
def __init__(self,a,b):
self.a = a
self.b = b
def series(self):
while (True):
yield(self.b)
self.a,self.b = self.b,self.a+self.b
f = fibonachi(0,1)
#f.series call the series function in calss fibonachi
for r in f.series():
if r>100:break
print (r,end='\n')
#class inheritance
class AnimalActions():
def quack(self): return self.strings['quacks']
def feathers(self):return self.strings['featehrs']
def bark(self): return self.strings['bark']
def fur(self):return self.strings['fur']
def testing(self): print('Testing')
class Duck(AnimalActions):
#we can access the parent class functions by
# using super keyword.
quacks = 'Quaaaaaak'
featehrs = 'The duck has gray and white feathers'
def testing(self):
super().testing()
print ("Additional testing")
donald = Duck()
donald.testing()
#print(donald.quack())
#raising custom exception
def readfile(filename):
if filename.endswith('.txt'):
opfile= open(filename)
return opfile.readlines()
else:
raise ValueError('File extension should be .txt')
#Error handling in python
try:
for line in readfile('lines.doc'):
print(line)
except IOError as e:
print("Something is wrong({})".format(e))
except ValueError as e:
print('File extension is not right({})'.format(e))
print('After badness')
# working with numbers
h = 42
print(type(h), h)
h=42.0
print(type(h),h)
h = 42/9
print(type(h),h)
h = 42//9
print(type(h),h)
h = round(42/9)
print(type(h),h)
h = round(42/9,2)
print(type(h),h)
h = divmod(42,9)
print(h)
# working with strings
# srings are imutable objects
s = "This is a \n string"
print(s)
k = r"This is a string \n with charachter scaping"
print(k)
# string formatting
n = 54
# This fromat is for python 3, better to use this one
s = "This is a {} string ".format(n)
print(s)
#In python 2 we use this format, but dont use it
s = "This is a %s string"%n
print(s)
# if we have couple of lines of text we should use triple quote
s='''\
This is a string with
line and
lines of
text
'''
print(s)
#working with tuples,and it is immutable
x =(2,5,7,8)
print(type(x),x)
#Working with lists, and it is mutable
x= [3,4,8,9]
# To append to the end of the list
x.append(5)
# to inser at the middle of the list.
x.insert(1,1000)
print(type(x),x)
print('Print 0 and 1 and 2 items in list')
print(x[0:3])
print('This one gives me every second elemnt')
print(x[0:3:2])
#Strings are another type of sequense and it is immutable
x= 'string'
print(type(x),x[2])
#slicing the string
#The way slices work on python is that they don't return the last element
print(x[2:4])
# All these sequence types can be used as iterators
x =(2,5,7,8)
for i in x:
print(i)
x= 'string'
for i in x:
print(i)
# dictionaries in python
d = {'one':1,'two':2,'three':3}
print(type(d),d)
for i in d:
print(i,d[i])
print('\n')
# if we want to get the sorted dictionry when iterating we should use sorted function then they will be in
# alphebetical order by the keys
for i in sorted(d.keys()):
print(i,d[i])
#Another way to define dictionary
d = dict(
one = 1,
two = 2,
three = 3,
four = 4,
five = 5
)
# you can add a new item to the dictionary:
d['seven'] = 7
for i in sorted(d.keys()):
print(i,d[i])
# useing case statments in python
choices = dict(
one = 1,
two = 2,
three = 3,
four = 4,
five = 5
)
v = 'three'
print(v,":",choices[v])
# in the case that the choise does not exist or we do want to have a defult choise
v = 'Seven'
print(choices.get(v,'Default result'))
# Iterating using enumerators
fh = open('lines.txt')
for index,line in enumerate(fh.readlines()):
print('Index is:',index, ' Line is : ',line)
#Passing aribitury arguments to the functions
#*args means arbitury arguments,this type of argument is tuple
def test(this,that,other,*args):
print(this,that,other)
for n in args:
print(n)
test(1,234,545,6,3,5,6,7)
#working with strings
print('this is a string'.upper())
print('this is a string'.capitalize())
print('This Is a String'.swapcase())
print('this is a string'.find('is'))
print('this is a string'.replace('this','that'))
print('this is a string '.rstrip())
print('this is a string\n'.rstrip('\n'))
print('this is a string '.strip())