Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python Iterator

fruits = ["apples", "bananas", "cherries", "pears", "plums"]
fruitIterator = iter(fruits);
print(next(fruitIterator))
print(next(fruitIterator))
print(next(fruitIterator))
Comment

Python Iterating Through an Iterator

# define a list
my_list = [4, 7, 0, 3]
# get an iterator using iter()
my_iter = iter(my_list)
# iterate through it using next()
# Output: 4
print(next(my_iter))
# Output: 7
print(next(my_iter))
# next(obj) is same as obj.__next__()

# Output: 0
print(my_iter.__next__())

# Output: 3
print(my_iter.__next__())

# This will raise error, no items left
next(my_iter)
Comment

python define iterable

class Reverse:
    """Iterator for looping over a sequence backwards."""
    def __init__(self, data):
        self.data = data
        self.index = len(data)

    def __iter__(self):
        return self

    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index = self.index - 1
        return self.data[self.index]
Comment

python iterator


def iter(times):
    it = 1
    for i in range(times):
        its = it * 2
        it = its
    return it

while True:
    print()
    print(iter(int(input("how many iteration? : "))))
Comment

iterator in python

class Camera():
    distance = 2
    speed_limit = 20
    number_of_cars = 0

    def Calc_Speed(self):
        registration = input("Registration Plate: ")
        Speeding_List=[]
        start = float(input("Start time: "))
        end = float(input("End Time: "))
        speed = self.distance/(end-start)
        print(("Average Speed: ") + str(round(speed, 2)) + (" mph"))
        if speed > self.speed_limit:
            list3= [str(self.registration)]
            Speeding_List.append(list3)
            print("Vehicles Caught Speeding: " + str(Speeding_List))
            return(program.Counter())
        else:
            print("Vehicle Not Speeding")
            return(program.Counter())

    def Counter():
        self.number_of_cars = self.number_of_cars + 1
        print("Number Of Cars Recorded: " + str(self.number_of_cars))                                 
        return(program.Calc_Speed())



program = Camera()
print(program)
Comment

PREVIOUS NEXT
Code Example
Python :: how to store object in file python 
Python :: python self usage 
Python :: what is a thread in os 
Python :: python 3.3 release date 
Python :: how to slice string in python 
Python :: django email verification 
Python :: how to iterate tuple in python 
Python :: mad libs generator python tutorial 
Python :: print multiple strings in python 
Python :: add item to python list 
Python :: python loop 3 times 
Python :: python boto3 put_object to s3 
Python :: decoding 
Python :: show chrome devtools in selenium 
Python :: fastest sorting algorithm java 
Python :: python port forwarding 
Python :: pandas change diagonal 
Python :: pytorch get tensor dimension 
Python :: how to separate date and time in python 
Python :: how to add to end of linked list python 
Python :: how to get function help in jupyter notebook 
Python :: printing in python 
Python :: Python controller input 
Python :: random module 
Python :: whole loop in python 
Python :: empaquetado y manejo dependencias en python 
Python :: roll dice python 
Shell :: remove postgresql ubuntu 
Shell :: how to kill apache process in linux 
Shell :: install dateutil 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =