Вы находитесь на странице: 1из 3

Unit II - Focuses on Python data structures such as strings, lists, and range sequences, as well as methods for

working with these structures. Students will be introduced to data structures and files in Python 3 and be ready for
more-advanced Python learning.

Module II – Sequence Manipulation

String Sequences Students will be able to:

 List Creation  Create Lists


 List Access  Access items in a list
 List Append  Add items to the end of a list
 List Insert  Modify and insert items into a list
 List Delete  Delete items from a list

Activity 1 List Sequences


1. Open Your IDE of Choice (Notebooks.Azzure, repl.It or Visual Studio
2. Open a New Project
3. Name it U2_M2_A1-XX (XX represents your first initial and last name I.E Mjordan).
4. Insert your program comments.
5. Insert a line comment to identify each step.
6. View this Video – Creating Lists

Concept: Creating Lists


A simple lists contains comma separated objects enclosed in square brackets

emptyList = [ ]

sampleList = [1, 1, 2, 3, 3, 3, 3, 4, 5, 5, 5, 5, 5]

List object types are not restricted so a mix of object types can be in single list

mixedList = [1, 1, "one", "two", 2.0, sampleList, emptyList, "Hello World"]

Examples
7. # [ ] review, key and run example
# define list of strings
footBones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform", "intermediate cuneiform", "medial
cuneiform"]

# display type information


print("footBones: ", type(footBones))

# print the list


print(footBones)
8. # [ ] review and run example
# define list of integers
ageSurvey = [12, 14, 12, 29, 12, 14, 12, 12, 13, 12, 14, 13, 13, 46, 13, 12, 12, 13, 13, 12, 12]
# display type information
print("ageSurvey: ", type(ageSurvey))

# print the list


print(ageSurvey)
9. # [ ] review, key and run example
# define list of mixed data type
mixedList = [1, 34, 0.999, "dog", "cat", footBones, ageSurvey]

# display type information


print("mixedList: ", type(mixedList))

# print the list


print(mixedList)

10. Create teamNames list and populate with 5 team name strings
a. Print the list
11. Create listMix and populate with integers, decimal, strings and teamNames list with a total of 10 items
a. Print the list
12. View this Video – Accessing Lists

Concept: List Access

Counting like a computer


To access a list we need to count like a computer, and that means starting with zero (0)

Lists give an index number to each list item

 first element in a list is index 0


 second element in a list is index 1

To access the first item in a list use the list name, followed by square brackets containing the index number

ageSurvey[0]

Examples
13. # [ ] review and run example
print(ftBones[0], "is the 1st bone on the list")
print(ftBones[2], "is the 3rd bone on the list")
print(ftBones[-1], "is the last bone on the list")

14. # review, key and run example


print(ftBones[1], "is connected to the",ftBones[3])

15. # [ ] review, key and run example


threeAgesSum = ageSurvey[0] + ageSurvey[1] + ageSurvey[2]
print("The first three ages total", threeAgesSum)
16. Create a list streets, and populate it with the name of 5 street name strings.
a. Display a message that there is “No Parking” on index 0 or index 4 streets (display the street names and
NOT “index 0” or “index 4”)
17. Create a list, num2Add, and populate it with 5 different numbers from 0-25 (use one list variable)
a. Calculate the sum of the numbers in one complete sentence (refer to the index positions in your code)
b. Assign a variable numSum for the total
c. Display numSum

18. FIX THE CODE (need to add a line of code and ensure it will run):
print(“ Total of check 3 & 4 = $ ”, payChecks[2] + payChecks [3]

19. Thoroughly test your code, check comments and then submit according to teacher instructions.
20. Take the activity quiz.

Вам также может понравиться