Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import cycle
- myIterator = cycle(range(2))
- myIterator.next() # or next(myIterator) which works in Python 3.x. Yields 0
- myIterator.next() # or next(myIterator) which works in Python 3.x. Yields 1
- # etc.
- from itertools import cycle
- mySmallSquareIterator = cycle(i*i for i in range(10))
- # Will yield 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 0, 1, 4, ...
- >>> def alternate():
- ... while True:
- ... yield 0
- ... yield 1
- ...
- >>>
- >>> alternator = alternate()
- >>>
- >>> alternator.next()
- 0
- >>> alternator.next()
- 1
- >>> alternator.next()
- 0
- import itertools
- myfunc = itertools.cycle([0,1]).next
- myfunc() # -> returns 0
- myfunc() # -> returns 1
- myfunc() # -> returns 0
- myfunc() # -> returns 1
- count = 0 # initialize count once
- count = (count + 1) % 2
- count = 0
- for i in range(5):
- count = (count + 1) % 2
- print count
- 1
- 0
- 1
- 0
- 1
- var = not var
- def alternate():
- alternate.x=not alternate.x
- return alternate.x
- alternate.x=True #The first call to alternate will return False (0)
- mylist=[5,3]
- print(mylist[alternate()]) #5
- print(mylist[alternate()]) #3
- print(mylist[alternate()]) #5
- from itertools import cycle
- alternator = cycle((0,1))
- next(alternator) # yields 0
- next(alternator) # yields 1
- next(alternator) # yields 0
- next(alternator) # yields 1
- #... forever
- count = 1
- count = count ^ 1 # count is now 0
- count = count ^ 1 # count is now 1
- var = 1
- var = 1 - var
- value = (1, 0)[value]
- toggle_val = 1
- toggle_val = (1,0)[toggle_val]
- a, b = map(int, raw_input("Enter both number: ").split())
- flag = input("Enter the first value: ")
- length = input("Enter Number of iterations: ")
- for i in range(length):
- print flag
- if flag == a:
- flag = b;
- else:
- flag = a
- mode=["mouse",""].remove(mode)[0]
- value=["option1","option2"].remove(value)[0]
Add Comment
Please, Sign In to add comment