-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythoncode_all.txt
More file actions
365 lines (241 loc) · 8.17 KB
/
Pythoncode_all.txt
File metadata and controls
365 lines (241 loc) · 8.17 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#notes
#if statement
a = 12
if a < 21:
print("not allowed")
else:
print("allowed")
#elif statement
name = "kp"
if name is "pj":
print("Hi")
elif name is "kj":
print("morning")
elif name is "gh":
print ("evening")
else:
print("nothing matches")
# how to loop through for statement
foods = ["apple","banana","grapes","cucumber","corn"]
for f in foods:
print(f)
print(len(f))
foods = ["apple","banana","grapes","cucumber","corn"]
for f in foods[:2]:
print(f)
print(len(f))
# range and while loop
#create for loop without list
for x in range(10):
print(x)
for x in range(1,11):
print(x)
for x in range(1,11,2):
print(x)
for x in range(2,11,2):
print(x)
a = 5
while a < 1:
print (a)
a += 1
#for infinite while loop stop process shortcut ctrl f2
# comment block three single quotes
'''
for n in range(101):
if n is magicnumber:
print("found the magicnumber")
break
else:
print(n)
'''
print ("magicnumber" +str(9))
# make a program to find out number divisible by 4 in 1 to 100
for i in range(101):
if i % 4 is 0:
print ("divisible number by 4 is " ,i)
else:
print()
# continue if condition become true it just doesnt print very nexl line and iterate again if condition false that print next line.
# Unlike break it doesn't come out from the loop it just come out from loop without printing or executing very next
#line of code and loops over again
numberstaken =[2,5,13,14,18]
print("here are the numbers that are still available")
for n in range(1,20):
if n in numberstaken:
continue
print(n)
#same code with if else
for i in range(1,20):
if i in numberstaken:
print()
else:
print(i)
# functions
#defining function
def abc():
# logic of function
print("functions are cool")
# calling function
abc()
def bitcoin_to_usd(btc):
amount = btc * 527
print (amount)
bitcoin_to_usd(3)
# Return values
# instead of printing storing values into variable by using return statement
def allowed_voting_age (my_age):
age = my_age/2 + 7
return age
age_limit = allowed_voting_age(34)
age_limit_other = allowed_voting_age(48)
print("who can vote:",age_limit,"or older age")
print("who can vote:",age_limit_other,"or older age")
# default values for arguments Aanlyze again???
def get_gender(gender):
if gender is 'm':
gender = "male"
elif gender is 'f':
gender = "female"
else:
print(gender)
get_gender("m")
# variable scope
a = 2333
def corn():
print(a)
def fudge():
print(a)
corn()
fudge()
#ERROR
def corn():
a = 2333
print(a)
def fudge():
print(a)
corn()
fudge()
#diiferent scope for same variable
def corn():
a = 2333
print(a)
def fudge():
a =56
print(a)
corn()
fudge()
#keyword arguments
def abc(name ="kp",number = 123,item = "kite"):
print(name,number,item)
abc(name = "",item = "mgfk")
#function which can take flexible number of arguments
def add_numbers(*args):
total = 0
for a in args:
total += a
print(total)
add_numbers(2)
add_numbers(2,4,6,7)
add_numbers(36,78,23,45,79,90)
#another example
def health_calculator(age,apples_ate,cigs_smoked):
answer = (100-age) + apples_ate * 3.5 - (cigs_smoked *2)
print(answer)
data = [27,20,55]
health_calculator(data[0],data[1],data[2])
# shortcut way to get answer
health_calculator(*data)
# sets is collection of item without duplicate
groceries = {'milk','almond','juice','yoghurt','milk','juice'}
print(groceries)
# dictionary
classmates = {'don': ' cool guy','gary' : ' too much funny','aska' : ' strange but nice'}
print(classmates['don'])
for k,v in classmates.items():
print(k ,v)
# modules are other files which we just use in our program
# scenario : we are using function from file pycharm2.py in current program
import pycharm2
pycharm2.abc()
# another example
import random
x = random.randrange(1,1000)
print(x)
#to install other modules go to settings/project interpreter and click on + sign and install
# if you dont want that module click - sign and delete it
#download an image from the web
import random
import urllib.request
def download_web_image(url):
random_number = random.randrange(1,1000)
full_name = str(random_number) + ".jpg"
urllib.request.urlretrieve(url,full_name)
# error because of firewall and all but this is how we download an image from internet
download_web_image("https://www.gstatic.com/webp/gallery/4.sm.jpg")
#solution and troubleshoot
1)google for the same error look for answers.
2)look all errors from last to first sequencially.
3)error was related to handshake ans ssl certificate.so pycharm client has not proper ssl certificate.we have to provide it.
4)in internet browser by copying weblink of image ,we will get an idea that it is working fine.so that means it has all ssl certificates.
5)what we need to do in this case is download this certificate and point out pycharm to that.
6)how to download that certificate:go to that image in browser --go to more tools -- developer tools --security -- view certificate--details--here you can also make sure your content is coming from trusted source.--details -- copy to file -- save it somewhere in system--so now we have necessary ssl certificate.
7)now go to pycharm and go to files--settings--serch for python console--environment --environment variables-- give name as REQUESTS_CA_BUNDLE--value is the full path of downloaded ssl certificate--click ok --ok
8)now try to run again it should work
# how to write in files
# make that file
fw = open('sample1.txt','w')
# write in this file
fw.write('writing in text file\n')
fw.write('I like writing\n')s
# close this file after done to save resources
fw.close()
# this is the location where it by default make a text file ,if you want to make it somewhere else just specify full path.
C:\Users\Pankaj\PycharmProjects\PythonPycharm1\sample.txt
# how to read from files
fr = open('sample.txt','r')
text = fr.read()
print(text)
fr.close()
'''The syntax to open a file object in Python is:
file_object = open(“filename”, “mode”) where file_object is the variable to add the file object.
The second argument you see – mode – tells the interpreter and developer which way the file will be used.
Mode
Including a mode argument is optional because a default value of ‘r’ will be assumed if it is omitted. The ‘r’ value stands for read mode, which is just one of many.
The modes are:
‘r’ – Read mode which is used when the file is only being read
‘w’ – Write mode which is used to edit and write new information to the file (any existing files with the same name will be erased when this mode is activated)
‘a’ – Appending mode, which is used to add new data to the end of the file; that is new information is automatically amended to the end
‘r+’ – Special read and write mode, which is used to handle both actions when working with a file
'''
# downloading files from web
from urllib import request
google_url = 'https://catalog.data.gov/dataset/american-factfinder-ii'
def download_data(csv_url):
response = request.urlopen(csv_url)
csv = response.read()
csv_str = str(csv)
lines = csv_str.split("\\n")
dest_url = r'goog.csv'
fx = open(dest_url,"w")
for line in lines:
fx.write(line + "\n")
fx.close()
download_data(google_url)
#WEB CROWLER
import requests
from bs4 import BeautifulSoup
def trade_spider(max_pages):
page = 1
while page <= max_pages:
url = 'http://www.coloring.ws/coloring.html'+ str(page)
print (1)
source_code = requests.get(url)
print (2)
soup = BeautifulSoup(source_code.text,'html.parser')
print (3)
for link in soup.findAll('img'):
print (4)
href = link.get('src')
print(href)
page += 1
trade_spider(5)