0 Replies - 358 Views - Last Post: 07 September 2010 - 08:53 AM

#1 captainhampton   User is offline

  • Jawsome++;
  • member icon

Reputation: 14
  • View blog
  • Posts: 548
  • Joined: 17-October 07

Even Or Odd (Generator Expressions)

Posted 07 September 2010 - 08:53 AM

Description: Call the function with a natural number n for a list to be printed and a subsequent "even or odd" tag next indicating the orientation of the number.Uses a feature specific to Python, Generator Expressions which are concise ways to consolidate a group of would be expressions. This prints a list of numbers and outputs whether they are even or odd.
# Print even or odd numbers using generator expressions. Generator 
# expressions are used much in the way the list comprehensions are 
# used to condense code. They yield one element at a time.
# Author: Vincent Russo

def evenOdd(n):
	even = (x for x in range(n) if x % 2 == 0)
	odd =  (y for y in range(n) if y % 2 != 0)
	
	for element in even:
		print "Even: ", element
	
	for element in odd:
		print "Odd: ", element
		
evenOdd(10)


Is This A Good Question/Topic? 0
  • +

Page 1 of 1