Skip to content

Commit f1f6905

Browse files
author
learnp
committedSep 11, 2016
reorganized directories
1 parent 2cabf70 commit f1f6905

28 files changed

+189
-33
lines changed
 

‎Basics/13_read_write_file.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
with open("C:\\data\\funny.txt","r") as f:
2+
print(f.read())
3+
4+
print(f.closed)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎area.py renamed to ‎Basics/area.py

File renamed without changes.
File renamed without changes.
File renamed without changes.

‎for.py renamed to ‎Basics/for.py

File renamed without changes.
File renamed without changes.

‎if.py renamed to ‎Basics/if.py

File renamed without changes.

‎Basics/lists

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Lists
2+
=====
3+
4+
Let us say your expense for every month are listed below,
5+
January - 2200
6+
February - 2350
7+
March - 2600
8+
April - 2130
9+
May - 2190
10+
Create a list to store these monthly expenses and using that find out,
11+
In Feb, how many dollars you spent extra compare to January?
12+
Find out your total expense in first quarter (first three months) of the year.
13+
Find out if you spent exactly 2000 dollars in any month
14+
June month just finished and your expense is 1980 dollar. Add this item to our monthly expense list
15+
You returned an item that you bought in a month of April and got a refund of 200$. Make a correction to your monthly expense list based on this.
16+
Answer:
17+
>>> exp = [2200,2350,2600,2130,2190]
18+
19+
# (a)
20+
>>> exp[1]-exp[0]
21+
150
22+
23+
# (b)
24+
>>> exp[0]+exp[1]+exp[2]
25+
7150
26+
27+
# (c)
28+
>>> 2000 in exp
29+
False # Means you didn’t spend 2000$ in any of the month
30+
31+
# (d)
32+
>>> exp.append(1980)
33+
>>> exp
34+
[2200, 2350, 2600, 2130, 2190, 1980]
35+
36+
# (e)
37+
>>> exp[3] = exp[3] - 200
38+
>>> exp[3]
39+
1930
40+
File renamed without changes.

‎test.py renamed to ‎Basics/test.py

File renamed without changes.
File renamed without changes.

‎Debugging/debugging.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def add_num(a,b):
2+
'''Return sum of two numbers'''
3+
s=a+b
4+
return s
5+
6+
n1=input('enter first number:')
7+
n1=int(n1)
8+
n2=input('enter second number:')
9+
n2=int(n2)
10+
s = add_num(n1,n2)
11+
print ('sum is: ',s);

‎Debugging/expenses.txt

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
1230
2+
2240
3+
1500
4+
1678
5+
2020
6+
1580
7+
2240
8+
1500
9+
1245
10+
2300
11+
1246
12+
3400
13+
1580
14+
2240
15+
1500
16+
3240
17+
2240
18+
1500
19+
1245
20+
2300
21+
1246
22+
3400
23+
1580
24+
2240
25+
XYGF
26+
1245
27+
2300
28+
1246
29+
3400
30+
1580
31+
2240
32+
1500
33+
3240
34+
2240
35+
1500
36+
1245
37+
2300
38+
1246
39+
3400
40+
1580
41+
2240

‎Multiprocessing/multi_proc.py

-31
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import time
2+
import multiprocessing
3+
4+
def calc_square(numbers):
5+
for n in numbers:
6+
print('square ' + str(n*n))
7+
8+
def calc_cube(numbers):
9+
for n in numbers:
10+
print('cube ' + str(n*n*n))
11+
12+
if __name__ == "__main__":
13+
arr = [2,3,8]
14+
p1 = multiprocessing.Process(target=calc_square, args=(arr,))
15+
p2 = multiprocessing.Process(target=calc_cube, args=(arr,))
16+
17+
p1.start()
18+
p2.start()
19+
20+
p1.join()
21+
p2.join()
22+
23+
print("Done!")
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import time
2+
import multiprocessing
3+
4+
def deposit(balance, lock):
5+
for i in range(100):
6+
time.sleep(0.01)
7+
lock.acquire()
8+
balance.value = balance.value + 1
9+
lock.release()
10+
11+
def withdraw(balance, lock):
12+
for i in range(100):
13+
time.sleep(0.01)
14+
lock.acquire()
15+
balance.value = balance.value - 1
16+
lock.release()
17+
18+
if __name__ == '__main__':
19+
balance = multiprocessing.Value('i', 200)
20+
lock = multiprocessing.Lock()
21+
22+
d = multiprocessing.Process(target=deposit, args=(balance,lock))
23+
w = multiprocessing.Process(target=withdraw, args=(balance,lock))
24+
25+
d.start()
26+
w.start()
27+
28+
d.join()
29+
w.join()
30+
31+
print(balance.value)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import multiprocessing
2+
3+
def calc_square(numbers, result, v):
4+
v.value = 5.67
5+
for idx, n in enumerate(numbers):
6+
result[idx] = n*n
7+
8+
if __name__ == "__main__":
9+
numbers = [2,3,5]
10+
result = multiprocessing.Array('i',3)
11+
v = multiprocessing.Value('d', 0.0)
12+
p = multiprocessing.Process(target=calc_square, args=(numbers, result, v))
13+
14+
p.start()
15+
p.join()
16+
17+
print(v.value)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import time
2+
from random import randint
3+
import threading
4+
5+
queue = []
6+
7+
def produce():
8+
for i in range(0,5):
9+
time.sleep(1)
10+
queue.append(randint(0,9))
11+
12+
def consume():
13+
while True:
14+
if len(queue) > 0:
15+
16+
17+
if "__name__"=="__main__":
18+
p = threading.Thread(target=produce)
19+
c = threading.Thread(target=consume)
20+

‎Multiprocessing/mult_threading.py renamed to ‎Multiprocessing/multthreading_introduction.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
def calc_square(numbers):
55
print("calculate square numbers")
66
for n in numbers:
7-
time.sleep(20)
7+
time.sleep(1)
88
print('square:',n*n)
99

1010
def calc_cube(numbers):
1111
print("calculate cube of numbers")
1212
for n in numbers:
13-
time.sleep(20)
13+
time.sleep(1)
1414
print('cube:',n*n*n)
1515

1616
arr = [2,3,8,9]

0 commit comments

Comments
 (0)
Please sign in to comment.