Chapter 3

3-6

Print a new set of invitation messages, one for each person in your list.

persons = ['ujala','taqi','malik'] 
new_persons = ['shah','asad','unknown'] 
persons.extend(new_persons)

print(f"Hello {persons[0]} you are invited. Please come with you other team member {persons[1]}. Hello {persons[2]} you are also cordially invited.")
Hello ujala you are invited. Please come with you other team member taqi. Hello malik you are also cordially invited.

Exercise 3-7 Shrinking Guest List:

  • Start with your program from Exercise 3-6. Add a new line that prints a message saying that you can invite only two people for dinner.
# Here it will remove last guest from the list

persons.pop() 
f"{persons}"

# Lets remove multiple guests since we have only 2 guests allowed now.

persons.remove("taqi") 
f"{persons}"

# lets remove all guests except first 2

persons = persons[:2] 
f"{persons}" f"We have problem due to table availablity. We can invite only {','.join(persons)} now"
"['ujala', 'malik']We have problem due to table availablity. We can invite only ujala,malik now"
  • Use pop() to remove guests from your list one at a time until only two names remain in your list. Each time you pop a name from your list, print a message to that person letting them know you’re sorry you can’t invite them to dinner.
# we have already completed this exercise above through indexing. Now we will print required msg 
persons = ['ujala','taqi','malik'] 
f"{persons.pop().title()}, we are sorry we can't invite you"
"Malik, we are sorry we can't invite you"
  • Print a message to each of the two people still on your list, letting them know they’re still invited.
persons = persons.pop() 
f"{','.join(persons).title()}, you are still invited"
'T,A,Q,I, you are still invited'
  • Use del to remove the last two names from your list, so you have an empty list. Print your list to make sure you actually have an empty list at the end of your program.
persons = ['ujala','taqi','malik'] 
del persons[:]

persons
[]

Exercise 3-8 Seeing the World:

Think of at least five places in the world you’d like to visit.

  • Store the location in a list and make sure list in not alphabatical order
locations = ['lahore','islamabad','karachi','fsd','rawalpindi']

# printing list as raw
locations
['lahore', 'islamabad', 'karachi', 'fsd', 'rawalpindi']
  • Use sorted() to print in alphabateical order
sorted(locations)
['fsd', 'islamabad', 'karachi', 'lahore', 'rawalpindi']
  • Show list is in original order
locations
['lahore', 'islamabad', 'karachi', 'fsd', 'rawalpindi']
  • Use reverse to change the order. Print the list to show that its order has changed.
locations.sort(reverse=True)

locations
['rawalpindi', 'lahore', 'karachi', 'islamabad', 'fsd']
  • USe reverse to change the order again and print list to show that list is back to its original order
locations.reverse()

locations
['fsd', 'islamabad', 'karachi', 'lahore', 'rawalpindi']
  • use sort to change list to its alpha.. order
locations.sort()

locations
['fsd', 'islamabad', 'karachi', 'lahore', 'rawalpindi']
  • use sort to change list to its reverse alpha order
locations.sort(reverse=True)

locations
['rawalpindi', 'lahore', 'karachi', 'islamabad', 'fsd']

Exercise 3-9 Dinner guests

persons = ['ujala','taqi','malik'] 

f"There are {len(persons)} coming to our dinner."
'There are 3 coming to our dinner.'

Exercise 3-10

Use all functions used in chapter 3 for a self generated list.

mountain1 = "K2"
mountain2 = "Nanga parbat"
mountain3 = "Turkish mir"
mountain4 = "margala"
mountain5 = "hindu kush"

mountains_in_pakistan = [mountain1,mountain2,mountain3,mountain4,mountain5]

mountains_in_pakistan
['K2', 'Nanga parbat', 'Turkish mir', 'margala', 'hindu kush']
  • Applying append
mountains_in_pakistan.append(mountain1)

mountains_in_pakistan 
['K2', 'Nanga parbat', 'Turkish mir', 'margala', 'hindu kush', 'K2']
  • extending list
mountain6 = ['lake mountain']
mountains_in_pakistan.extend(mountain6)

mountains_in_pakistan 
['K2',
 'Nanga parbat',
 'Turkish mir',
 'margala',
 'hindu kush',
 'K2',
 'lake mountain']
  • popping list
mountains_in_pakistan.pop()

mountains_in_pakistan 
['K2', 'Nanga parbat', 'Turkish mir', 'margala', 'hindu kush', 'K2']
  • popping by vallue
mountains_in_pakistan.pop(3)

mountains_in_pakistan 
['K2', 'Nanga parbat', 'Turkish mir', 'hindu kush', 'K2']
  • sorting
mountains_in_pakistan.sort()

mountains_in_pakistan 
['K2', 'K2', 'Nanga parbat', 'Turkish mir', 'hindu kush']
  • sorted
sorted(mountains_in_pakistan)

mountains_in_pakistan 
['K2', 'K2', 'Nanga parbat', 'Turkish mir', 'hindu kush']
  • reversing
mountains_in_pakistan.reverse()

mountains_in_pakistan 
['hindu kush', 'Turkish mir', 'Nanga parbat', 'K2', 'K2']
  • finding length of list
len(mountains_in_pakistan)
5
  • copying the list
copy_of_mountains_in_pakistan = mountains_in_pakistan.copy()

copy_of_mountains_in_pakistan
['hindu kush', 'Turkish mir', 'Nanga parbat', 'K2', 'K2']
  • Inserting new mountain
mountains_in_pakistan.insert(3, "karakoram range")

mountains_in_pakistan
['hindu kush', 'Turkish mir', 'Nanga parbat', 'karakoram range', 'K2', 'K2']
f"{','.join(mountains_in_pakistan)} are all the moutain / mountain range in pakistan"
'hindu kush,Turkish mir,Nanga parbat,karakoram range,K2,K2 are all the moutain / mountain range in pakistan'

End of Chapter 3