Answers for "generators in python"

19

python generator

# A generator-function is defined like a normal function, 
# but whenever it needs to generate a value, 
# it does so with the yield keyword rather than return. 
# If the body of a def contains yield, 
# the function automatically becomes a generator function.
# Python 3 example
def grepper_gen():
  yield "add"
  yield "grepper"
  yield "answer"
grepper = grepper_gen()
next(grepper)
> add
next(grepper)
> grepper
next(grepper)
> answer
Posted by: Guest on February-13-2020
2

automation in python

#you can use automation in python using following tools
#Web Automation use selenium driver
#GUI Automation use pyautogui tool

#Selenium Automation Example
#Download chrome driver for Selenium 
# https://chromedriver.chromium.org/downloads

#############Lanuch WebDriver Function - Begin #############
#Make sure the downloaded driver is in the same path where your running code
#else you will get an error.

from selenium import webdriver
# store the browser driver
chrome_browser = launch_chromebrowser('chromedriver')

def launch_chromebrowser(browsername):
    # load the browser driver
    return webdriver.Chrome(browsername)
#############Lanuch WebDriver Function - End #############
Posted by: Guest on June-16-2020
1

python generator

def count_to_ten_generator():
  for number in range(10):
    yield number
my_generator = count_to_ten_generator()
first_number = next(my_generator)
list_or_the_rest = list(my_generator)
Posted by: Guest on September-19-2020
1

python generators

# Size of generators is a huge advantage compared to list
import sys

n= 80000

# List
a=[n**2 for n in range(n)]

# Generator
# Be aware of the syntax to create generators, lika a list comprehension but with round brakets
b=(n**2 for n in range(n))

print(f"List: {sys.getsizeof(a)} bits\nGenerator: {sys.getsizeof(b)} bits")
Posted by: Guest on October-08-2020
0

generators in python

def mygenerator():
    print('First item')
    yield 10

    print('Second item')
    yield 20

    print('Last item')
    yield 30
Posted by: Guest on September-20-2021
0

python script for automation

mkdir food, food\fruits, food\fruits\apples, food\fruits\oranges, food\vegetables
Posted by: Guest on April-29-2020

Python Answers by Framework

Browse Popular Code Answers by Language