Python: 1. Open a new program in IDLE and in a multiline comment, write “Python
ID: 3881080 • Letter: P
Question
Python:
1. Open a new program in IDLE and in a multiline comment, write “Python Set Practice,” your name, and the date.
2. Define Odds to be the set of odds between 1 and 199 (inclusive) using the set() and range() commands only – do not use % testing!
3. Define a set Squares that contains all of the squares of the positive integers between 1 and 14 inclusive. Find the set: Squares - Odds.
4. By creating two sets and using set operations and the len() command, determine how many integers between 1 and 1000 (inclusive) that are both perfect squares and perfect cubes.
Explanation / Answer
Question 1
# <Name> <Date>
Question 2
Odds= set([])
for i in range(1,200,2):
Odds.add(i)
Question 3
Squares= set([])
for i in range(1,14,1):
Squares.add(i*i)
print( Squares.difference(Odds) )
Question 4
Sq= set([])
Cb = set([])
for i in range(1,33,1):
Sq.add(i*i)
for i in range(1,10,1):
Cb.add(i*i)
print( Sq.intersection(Cb) )
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.