Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("***** abs *****")
- x = -3
- y = abs(x)
- # abs: Absolute value of a numeric type
- print(y)
- # ---------------- OUTPUT ----------------
- # 3
- print("***** multiply *****")
- # True will be repeated 5 times
- x = [True] * 5
- print(x)
- # to check all items are True
- print(all(x))
- # append False to array and check again
- x.append(False)
- print(x)
- print(all(x))
- # check if any is True
- print(any(x))
- # ---------------- OUTPUT ----------------
- # ***** multiply *****
- # [True, True, True, True, True]
- # True
- # [True, True, True, True, True, False]
- # False
- # True
- print("***** enumerate *****")
- # used to loop over a list with a counter
- for pos, letter in enumerate("python"):
- print(str(pos) + "=>" + letter)
- # ---------------- OUTPUT ----------------
- # ***** enumerate *****
- # 0=>p
- # 1=>y
- # 2=>t
- # 3=>h
- # 4=>o
- # 5=>n
- print("***** eval *****")
- # eval interprets a string as code => allows to run a string as python code
- x = 1
- print(eval("x + 3"))
- # ---------------- OUTPUT ----------------
- # ***** eval *****
- # 4
- print("***** filter array *****")
- ages = [6, 8, 12, 19, 7, 39, 24, 65]
- def older(x):
- return x > 19
- for item in filter(older, ages):
- print(item)
- # ---------------- OUTPUT ----------------
- # ***** filter array *****
- # 39
- # 24
- # 65
- print("***** map *****")
- # use map to apply function to each item in a list
- def square(x):
- return x * x
- for item in map(square, range(1,10)):
- print("square %d" % item)
- # ---------------- OUTPUT ----------------
- # ***** map *****
- # square 1
- # square 4
- # square 9
- # square 16
- # square 25
- # square 36
- # square 49
- # square 64
- # square 81
- print("***** pow *****")
- # power of a number
- x = 3
- y = 4
- print(pow(x, y))
- # ---------------- OUTPUT ----------------
- # ***** pow *****
- # 81
- print("***** zip *****")
- # to create tuples from 2 arrays
- x = [1, 2, 3, 4]
- y = ["a", "b", "c", "d"]
- z = zip(x, y)
- for a in z:
- print(a)
- # combine zip and dict to create dictionary from 2 arrays
- d = dict(zip(x,y))
- print(d)
- # ---------------- OUTPUT ----------------
- # ***** zip *****
- # (1, 'a')
- # (2, 'b')
- # (3, 'c')
- # (4, 'd')
- # {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
Add Comment
Please, Sign In to add comment