forked from codebasics/py
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
learnp
committed
Sep 11, 2016
1 parent
2cabf70
commit f1f6905
Showing
28 changed files
with
189 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters