Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def factor(factor_number):
- for n in range(2,factor_number):
- if factor_number % n == 0: #trailing whitespace here
- yield (n)
- #slightly modified version of gcd from fractions library (Py 2.7)
- #good example of multiline acomments
- def gcd(a,b):
- #good example of multiline comments
- #NOTE: context matters here! If a multiline quote is assigned to a variable
- #OR if it is in a print statement (see bellow), it is NOT a comment
- """Calculate the Greatest Common Divisor of a and b.
- Unless b==0, the result will have the same sign as b (so that when
- b is divided by it, the result comes out positive).
- """
- while b:
- a, b = b, a%b #one extra space of indent here
- return a
- class Apricot:
- def __init__(self):
- self.mold = False
- def get(self):
- return self.mold
- def update(self):
- #unnecessary white space
- self.mold = not self.mold
- def blue(self):return 5
- def tell_me_about_these_numbers(*a):
- print("%d is the first number!" % a[0])
- print("{} / 3 is {}".format(a[0],a[0]/3.))
- myFavorate = Apricot()
- for number in a:
- print list(factor(number))
- myFavorate.update()
- print [gcd(a,b) for a,b in zip(a[:-1], a[1:])]
- print(myFavorate.get())
- tell_me_about_these_numbers(5,6,9,45,200)
- print "Let's play with scope!"
- a,b=10,9
- def randomFunction(a):
- print(a)
- randomFunction(b)
- print(a)
- for a in range(100):
- b+=a
- print(a)
- print(b)
- li=[]
- for i in range(10):
- li.append(i*2)
- print(li)
- print([i*2 for i in range(10)])
- a = c = b = d = e = f = g = h=i=j=k=l=m=n=o=p=q=r=s=t=u=v=w=x=y=z=5
- print(a)
- a-=1
- print(a)
- g=10
- print(str(10**g + 5)[::-1])#[::-1] is shorthand for reversing a string/list/...
- def blue_fish(a):
- def blue_fish(a):
- def blue_fish(a):
- return a
- a+=1
- return blue_fish(a)
- a-=1
- return blue_fish(a)
- print(blue_fish(10))
- def blue_fish(a):
- #you can redefine functions
- if a==0:
- return "0"
- return "1" + blue_fish(a-1)
- print(blue_fish(5))
- blue_fish = lambda a, b, c: a * b* c
- print(blue_fish(1,2,3))
- blue_fish = lambda *a: reduce(lambda a,b: a*b,a)
- print(blue_fish(1,2,3))
- print(max([[6,1],[5,2],[4,3],[3,4],[2,5],[1,6]],key=lambda a:a[1]))#there are keyword arguments
- print(zip(*[[1],[2],[3],[4],[5]]))
- print "Now let's test to see if you handle quotes correctly:"
- print "test \'many diffent\' \"types of \" quotes, even with \' \" trailing quotes"
- print """
- Multi line quotes are great too!
- """
- a=""" ::
- one more multi-line quote won't hurt
- """
- print a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement