0% found this document useful (0 votes)
82 views2 pages

Write A Simple List Comprehension That Generates The First 5 Even Numbers As A List

The document contains examples of list comprehensions and generator expressions in Python. It shows how to generate lists of even numbers, positive sine values, letter permutations, and tuples of powers. It also demonstrates creating dictionaries with list or integer values from character and day strings.

Uploaded by

bishwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views2 pages

Write A Simple List Comprehension That Generates The First 5 Even Numbers As A List

The document contains examples of list comprehensions and generator expressions in Python. It shows how to generate lists of even numbers, positive sine values, letter permutations, and tuples of powers. It also demonstrates creating dictionaries with list or integer values from character and day strings.

Uploaded by

bishwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

List comprehension - even number

PROGRAMMING
3.0 Marks

Write a simple list comprehension that generates the first 5 even numbers as a list.
For example:
Output
[2, 4, 6, 8, 10]

l=[ 2*x for x in range(1,6) ]

print(l)

Write a list comprehension that generates only positive values of


the math.sin when applied to the first 10 values between 0 to 2π
from math import sin, pi

r=[sin(i*2*pi/9) for i in range(0,10) ]

print([x for x in r if x>0])

Using a list comprehension, write all possible 2 letter permutations of the


world 'hello', do now worry about the repetitions
s="hello"

w=[x+y for x in s for y in s]

print(w)

Write all possible 2 letter permutations of the word 'hello', this time show only
the unique ones
s="hello"

w=[x+y for x in s for y in s if x!=y]

print(w)

Using a list comprehension, create a list of tuples of the form (x, x*x,
x*x*x) for the first 5 integers starting at 1.
l=[(x,x*x,x*x*x) for x in range(1,6)]
print(l)

Create a dictionary with the keys being the lowercase characters of the
English alphabet and the value being an empty list.
For example
Output
{'a': [],
'b': [],
....
}
z={x:[] for x in 'abcdefghijklmnopqrstuvwxyz'}

print(z)

Create a dictionary with the keys being the lowercase words (only the first
three characters) denoting the day of the week and the value being an integer
corresponding to each with 0 being 'mon' and 6 'sun' .
For example
Output
{'mon': 0,
'sun': 6
}
z={x:i for i, x in enumerate(('mon','tues','thurs','friday','saturday','sunday'))}

print(z)

Using a generator expression find the maximum value of math.sin when


applied to the first 100 values between 0 to 2π
from math import sin, pi

r=[sin(i*2*pi/99) for i in range(0,101) ]

print(max(r))

You might also like