Skip to content

Commit

Permalink
reorganized directories
Browse files Browse the repository at this point in the history
  • Loading branch information
learnp committed Sep 11, 2016
1 parent 2cabf70 commit f1f6905
Show file tree
Hide file tree
Showing 28 changed files with 189 additions and 33 deletions.
4 changes: 4 additions & 0 deletions Basics/13_read_write_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
with open("C:\\data\\funny.txt","r") as f:
print(f.read())

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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions Basics/lists
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Lists
=====

Let us say your expense for every month are listed below,
January - 2200
February - 2350
March - 2600
April - 2130
May - 2190
Create a list to store these monthly expenses and using that find out,
In Feb, how many dollars you spent extra compare to January?
Find out your total expense in first quarter (first three months) of the year.
Find out if you spent exactly 2000 dollars in any month
June month just finished and your expense is 1980 dollar. Add this item to our monthly expense list
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.
Answer:
>>> exp = [2200,2350,2600,2130,2190]

# (a)
>>> exp[1]-exp[0]
150

# (b)
>>> exp[0]+exp[1]+exp[2]
7150

# (c)
>>> 2000 in exp
False # Means you didn’t spend 2000$ in any of the month

# (d)
>>> exp.append(1980)
>>> exp
[2200, 2350, 2600, 2130, 2190, 1980]

# (e)
>>> exp[3] = exp[3] - 200
>>> exp[3]
1930

File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions Debugging/debugging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def add_num(a,b):
'''Return sum of two numbers'''
s=a+b
return s

n1=input('enter first number:')
n1=int(n1)
n2=input('enter second number:')
n2=int(n2)
s = add_num(n1,n2)
print ('sum is: ',s);
41 changes: 41 additions & 0 deletions Debugging/expenses.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
1230
2240
1500
1678
2020
1580
2240
1500
1245
2300
1246
3400
1580
2240
1500
3240
2240
1500
1245
2300
1246
3400
1580
2240
XYGF
1245
2300
1246
3400
1580
2240
1500
3240
2240
1500
1245
2300
1246
3400
1580
2240
31 changes: 0 additions & 31 deletions Multiprocessing/multi_proc.py

This file was deleted.

23 changes: 23 additions & 0 deletions Multiprocessing/multiprocessing_introduction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import time
import multiprocessing

def calc_square(numbers):
for n in numbers:
print('square ' + str(n*n))

def calc_cube(numbers):
for n in numbers:
print('cube ' + str(n*n*n))

if __name__ == "__main__":
arr = [2,3,8]
p1 = multiprocessing.Process(target=calc_square, args=(arr,))
p2 = multiprocessing.Process(target=calc_cube, args=(arr,))

p1.start()
p2.start()

p1.join()
p2.join()

print("Done!")
31 changes: 31 additions & 0 deletions Multiprocessing/multiprocessing_lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import time
import multiprocessing

def deposit(balance, lock):
for i in range(100):
time.sleep(0.01)
lock.acquire()
balance.value = balance.value + 1
lock.release()

def withdraw(balance, lock):
for i in range(100):
time.sleep(0.01)
lock.acquire()
balance.value = balance.value - 1
lock.release()

if __name__ == '__main__':
balance = multiprocessing.Value('i', 200)
lock = multiprocessing.Lock()

d = multiprocessing.Process(target=deposit, args=(balance,lock))
w = multiprocessing.Process(target=withdraw, args=(balance,lock))

d.start()
w.start()

d.join()
w.join()

print(balance.value)
17 changes: 17 additions & 0 deletions Multiprocessing/multiprocessing_value_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import multiprocessing

def calc_square(numbers, result, v):
v.value = 5.67
for idx, n in enumerate(numbers):
result[idx] = n*n

if __name__ == "__main__":
numbers = [2,3,5]
result = multiprocessing.Array('i',3)
v = multiprocessing.Value('d', 0.0)
p = multiprocessing.Process(target=calc_square, args=(numbers, result, v))

p.start()
p.join()

print(v.value)
20 changes: 20 additions & 0 deletions Multiprocessing/multithreading_producer_consumer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import time
from random import randint
import threading

queue = []

def produce():
for i in range(0,5):
time.sleep(1)
queue.append(randint(0,9))

def consume():
while True:
if len(queue) > 0:


if "__name__"=="__main__":
p = threading.Thread(target=produce)
c = threading.Thread(target=consume)

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
def calc_square(numbers):
print("calculate square numbers")
for n in numbers:
time.sleep(20)
time.sleep(1)
print('square:',n*n)

def calc_cube(numbers):
print("calculate cube of numbers")
for n in numbers:
time.sleep(20)
time.sleep(1)
print('cube:',n*n*n)

arr = [2,3,8,9]
Expand Down

0 comments on commit f1f6905

Please sign in to comment.