Skip to content

Commit 00f1e4f

Browse files
committed
Pytalk files for this evening -ipynb warning changed inotebook LF to CRLF
1 parent 36f5c48 commit 00f1e4f

33 files changed

+1362
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
z= 3
3+
4+
f = lambda x, y : (x+y)*z
5+
print(f(2,3))
6+
7+
8+
fl = [lambda x,y: x*y, lambda x,y: x-y]
9+
res = fl[1](4,5)
10+
print(res)
11+
12+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import math
2+
3+
class Circle:
4+
color = "red"
5+
6+
7+
def __init__(self,diameter):
8+
self.diameter = diameter
9+
self.area = self.get_area(diameter)
10+
11+
def grow(self,factor=2):
12+
self.diameter = self.diameter * factor
13+
return self.diameter
14+
15+
def get_area(self,diameter):
16+
self.area = math.pi *(diameter/2)**2
17+
return self.area
18+
19+
class Point:
20+
color = "red"
21+
size = 4
22+
23+
def __init__(self,x,y):
24+
self.x = x
25+
self.y = y
26+
27+
def get_color(self):
28+
return self.color
29+
30+
31+
#p1 = Point (3,4)
32+
#print (p1.x, p1.get_color())
33+
34+
c1= Circle(4)
35+
36+
print("diameter: ",c1.diameter)
37+
print("area: ",c1.area)
38+
39+
print("c1 growth ", c1.grow(5))
40+
print("c1 new diameter: ", c1.diameter)
41+
#c1=Circle(c1.diameter)
42+
print("area: ,",c1.get_area(c1.diameter))
43+
44+
if isinstance(c1,Circle):
45+
print("true {}, is a {}".format(c1,Circle))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python
2+
3+
# this is super cool way to get the absolute path to the current file as a string in a list!
4+
5+
import sys
6+
7+
print(sys.argv)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
from class notes session 6:
3+
4+
Function arguments in variables
5+
function arguments are really just
6+
7+
a tuple (positional arguments)
8+
a dict (keyword arguments)
9+
def f(x, y, w=0, h=0):
10+
print("position: {}, {} -- shape: {}, {}".format(x, y, w, h))
11+
12+
position = (3,4)
13+
size = {'h': 10, 'w': 20}
14+
15+
!!!!!!!!!!!!!!!!!!!! notice the * and ** below : one * for tuple items, and ** for values from a dictionary !!!!!!!
16+
17+
>>> f(*position, **size)
18+
position: 3, 4 -- shape: 20, 10
19+
Function parameters in variables
20+
You can also pull the parameters out in the function as a tuple and a dict:
21+
22+
def f(*args, **kwargs):
23+
print("the positional arguments are:", args)
24+
print("the keyword arguments are:", kwargs)
25+
26+
In [389]: f(2, 3, this=5, that=7)
27+
the positional arguments are: (2, 3)
28+
the keyword arguments are: {'this': 5, 'that': 7}
29+
This can be very powerful...
30+
31+
Passing a dict to str.format()
32+
Now that you know that keyword args are really a dict, you know how this nifty trick works:
33+
34+
The string format() method takes keyword arguments:
35+
36+
In [24]: "My name is {first} {last}".format(last="Barker", first="Chris")
37+
Out[24]: 'My name is Chris Barker'
38+
Build a dict of the keys and values:
39+
40+
In [25]: d = {"last":"Barker", "first":"Chris"}
41+
And pass to format()``with ``**
42+
43+
In [26]: "My name is {first} {last}".format(**d)
44+
Out[26]: 'My name is Chris Barker'
45+
46+
"""
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
#copy from example file - I have not done anything here. Not sure what to do
3+
4+
"""
5+
Examples from: http://codingbat.com
6+
7+
Put here so we can write unit tests for them ourselves
8+
"""
9+
10+
# Python > Warmup-1 > sleep_in
11+
12+
13+
def sleep_in(weekday, vacation):
14+
if vacation is True:
15+
return True
16+
elif weekday is False:
17+
return True
18+
else:
19+
return False
20+
21+
22+
def sumdouble(a, b):
23+
if a == b:
24+
return (a + b) * 2
25+
else:
26+
return(a + b)
27+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#list:
2+
y = [x*3 for x in [3,6,9,12]]
3+
print(y)
4+
5+
#set
6+
7+
y = {x**2 for x in [3,-3,6,9,12]}
8+
print(y)
9+
10+
# dictionary
11+
12+
y = {x: "this_%x is : "%x*3 for x in [3,6,9,12]}
13+
print(y)
8.58 KB
Binary file not shown.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
#map play
3+
4+
l = [5,10,15,20]
5+
6+
fx = lambda x: 2*x
7+
8+
res = map(fx,l)
9+
10+
for x in res:
11+
print(x)
12+
13+
d={"spring":("happy yellow", "bright pink")}
14+
# "midnight" : ("dark blue", "shadow black"),
15+
#"neon" : ("electric orange", "blinding silver")}
16+
17+
#fx2 = lambda args: "{} and {} make good colors".format(enumerate(args))
18+
fx2 = lambda args: "{} and {} make good colors".format(args["spring"][0],args["spring"][1])
19+
print(fx2(d))
8.58 KB
Binary file not shown.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
this is line: 0
2+
I am king
3+
this is line: 1
4+
I am king
5+
this is line: 2
6+
I am king
7+
this is line: 3
8+
I am king
9+
this is line: 4
10+
I am king
11+
this is line: 5
12+
I am king
13+
this is line: 6
14+
I am king
15+
this is line: 7
16+
I am king
17+
this is line: 8
18+
I am king
19+
this is line: 9
20+
I am king

0 commit comments

Comments
 (0)