Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. def factor(factor_number):
  2.  
  3. for n in range(2,factor_number):
  4. if factor_number % n == 0: #trailing whitespace here
  5. yield (n)
  6.  
  7. #slightly modified version of gcd from fractions library (Py 2.7)
  8. #good example of multiline acomments
  9. def gcd(a,b):
  10. #good example of multiline comments
  11. #NOTE: context matters here! If a multiline quote is assigned to a variable
  12. #OR if it is in a print statement (see bellow), it is NOT a comment
  13. """Calculate the Greatest Common Divisor of a and b.
  14.  
  15. Unless b==0, the result will have the same sign as b (so that when
  16. b is divided by it, the result comes out positive).
  17. """
  18. while b:
  19. a, b = b, a%b #one extra space of indent here
  20. return a
  21.  
  22. class Apricot:
  23. def __init__(self):
  24. self.mold = False
  25. def get(self):
  26. return self.mold
  27. def update(self):
  28. #unnecessary white space
  29.  
  30.  
  31.  
  32. self.mold = not self.mold
  33. def blue(self):return 5
  34.  
  35. def tell_me_about_these_numbers(*a):
  36. print("%d is the first number!" % a[0])
  37. print("{} / 3 is {}".format(a[0],a[0]/3.))
  38. myFavorate = Apricot()
  39. for number in a:
  40. print list(factor(number))
  41. myFavorate.update()
  42. print [gcd(a,b) for a,b in zip(a[:-1], a[1:])]
  43. print(myFavorate.get())
  44.  
  45. tell_me_about_these_numbers(5,6,9,45,200)
  46.  
  47.  
  48. print "Let's play with scope!"
  49. a,b=10,9
  50. def randomFunction(a):
  51. print(a)
  52. randomFunction(b)
  53. print(a)
  54. for a in range(100):
  55. b+=a
  56. print(a)
  57. print(b)
  58. li=[]
  59. for i in range(10):
  60. li.append(i*2)
  61. print(li)
  62. print([i*2 for i in range(10)])
  63. 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
  64. print(a)
  65. a-=1
  66. print(a)
  67. g=10
  68. print(str(10**g + 5)[::-1])#[::-1] is shorthand for reversing a string/list/...
  69. def blue_fish(a):
  70. def blue_fish(a):
  71. def blue_fish(a):
  72. return a
  73. a+=1
  74. return blue_fish(a)
  75. a-=1
  76. return blue_fish(a)
  77. print(blue_fish(10))
  78. def blue_fish(a):
  79. #you can redefine functions
  80. if a==0:
  81. return "0"
  82. return "1" + blue_fish(a-1)
  83. print(blue_fish(5))
  84. blue_fish = lambda a, b, c: a * b* c
  85. print(blue_fish(1,2,3))
  86. blue_fish = lambda *a: reduce(lambda a,b: a*b,a)
  87. print(blue_fish(1,2,3))
  88. print(max([[6,1],[5,2],[4,3],[3,4],[2,5],[1,6]],key=lambda a:a[1]))#there are keyword arguments
  89. print(zip(*[[1],[2],[3],[4],[5]]))
  90.  
  91. print "Now let's test to see if you handle quotes correctly:"
  92. print "test \'many diffent\' \"types of \" quotes, even with \' \" trailing quotes"
  93. print """
  94.  
  95. Multi line quotes are great too!
  96.  
  97. """
  98. a=""" ::
  99. one more multi-line quote won't hurt
  100. """
  101. print a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement