Python exercise:
4-5. Odd Numbers:
• Use the range() function to make a list of
the odd numbers from 1 to 20. • Use
a for loop to print each number.
4-6. Cubes:
A number raised to the third power is called a cube. The cube
of 2 is written as 2**3 in Python.
Make a list of the first 10 cubes (that is, the cube of each
integer from 1 through 10),
Use a for loop to print out the value of each
cube.
4-7. Slicing a list:
Using the program you wrote in exercise 4-2 above, modify the
program using the range() function to print only the
first three animals in the list.
Using the program you wrote in exercise 4-2 above, modify the
program using a slice of the list of animals to print
only the first three animals in the list.
Print the message: "The last three items in the list are:" and
use a slice to print the last three items in the
list.
4-8. Sorting a list and a tuple.
Using the list of animals from exercise 4-2 above, make a copy
of the list.
Sort the animals in the copied list using
the sort() method.
Print both the original list and the alphabetized copied list to
show that the original list
has not changed.
Create a tuple of the original list.
Use the sorted() function to create a sorted tuple.
Make sure the output is a tuple.
Python exercise: 4-5. Odd Numbers: • Use the range() function to make a list of the odd numbers from 1 to 20. • Use a fo
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am