-->

Python 3.4.1.13 LAB: The basics of lists - the Beatles

 The basics of lists - the Beatles

3.4.1.13 LAB


If you're taking PCAP - Programming Essentials In Python , you may have encountered this question 3.4.1.13 LAB: The basics of lists - the Beatles.

Objectives

Familiarize the student with:

  • creating and modifying simple lists;
  • using methods to modify lists.

Scenario

The Beatles were one of the most popular music group of the 1960s, and the best-selling band in history. Some people consider them to be the most influential act of the rock era. Indeed, they were included in Time magazine's compilation of the 20th Century's 100 most influential people.

The band underwent many line-up changes, culminating in 1962 with the line-up of John Lennon, Paul McCartney, George Harrison, and Richard Starkey (better known as Ringo Starr).

Write a program that reflects these changes and lets you practice with the concept of lists. Your task is to:

step 1: create an empty list named beatles;

step 2: use the append() method to add the following members of the band to the list: John Lennon, Paul McCartney, and George Harrison;

step 3: use the for loop and the append() method to prompt the user to add the following members of the band to the list: Stu Sutcliffe, and Pete Best;

step 4: use the del instruction to remove Stu Sutcliffe and Pete Best from the list;

step 5: use the insert() method to add Ringo Starr to the beginning of the list.

By the way, are you a Beatles fan? (The Beatles is one of Greg's favorite bands. But wait...who's Greg...?)

Solution Code

beatles = [] #step1
print("Step 1:"beatles)

beatles.append("John Lennon"#step2
beatles.append("Paul McCartney")
beatles.append("George Harrison")
print("Step 2:"beatles)

name = 0 #step3
for i in range(2):
    name = input("Add the next beatles: ")
    beatles.append(name)
print("Step 3:"beatles)

del beatles[3#step4
del beatles[3]
print("Step 4:"beatles)

beatles.insert(0,"Ringo Starr"#step5
print("Step 5:"beatles)


# testing list legth
print("The Fab"len(beatles))

=================================================================================

Follow the Python page on the Blog to be the first to know


Post a Comment

0 Comments