Python Challenge #1 in Python, We Can Use Lists To

Python Challenge #1 in Python, We Can Use Lists To

Python Challenge #1 In Python, we can use lists to store related items together in a single place. Two examples are: tanzanian_election_years = [1995, 2000, 2005, 2010, 2015] tanzanian_presidents = [“John Magu uli!, “Ja"aya #i"$ete!, “%en&a'in M"apa!, ( “)li *assan M$inyi!, “Julius +yerere!] 1. On a computer or tablet, write a Python program that prints out each year that Tanzania has had a general election. In Python, you can loop over the items of a list like this: or ite' in list, - do so'ething $ith ite' 2. Using your answer for #1, change it slightly so that it only prints out the years in which Tanzania has had a general election after the year 1999. 3. Sometimes we want to work with two lists at the same time. Say we are given the following list of Tanzanian vice presidents: tanzanian_.ice_presidents = [“/a'ia /uluhu!, “Moha'ed 0hari1 %ilal!, ( “2'ar )li Ju'a!, “3leopa Msuya!, “)1oud Ju'1e!] and we want to match each vice president with the president he or she served with. We can use Python’s zip() function to do this. To illustrate how zip() works, type the following into your Python interpreter (note: don’t type the “>>>”, that is printed by the Python interpreter itself): 444 ruits = [“apple!, “grape!, “1lue1erry!] 444 colors = [“green!, “purple!, “1lue!] 444 zip5 ruits, colors6 [57apple8, 7green86, 57grape8, purple86, 571lue1erry8, 71lue86] Notice how Python matched each of the fruits in our first list with its corresponding color in the second list. The zip() function itself returns a list, which we can use in our own for loops. On your computer or tablet, write a Python program that zips together the tanzanian_presidents list with the tanzanian_.ice_presidents list. Then use a for loop to print out each pair on a separate line. 4. A method is a Python function that is called on a specific value or variable. For instance, the startswith(str) function is a method that can be used to see if a Python string starts with another string; try typing the following into your Python interpreter: 444 79eo asu1uhi8:starts$ith579eo86 ;rue 444 79eo asu1uhi8:starts$ith57leo86 <alse 444 str = 7#esho asu1uhi8 444 str:starts$ith57#esho86 ;rue 444 str:starts$ith57"esho86 <alse Using the tanzanian_.ice_presidents list, write a program that prints out only those Tanzanian vice presidents whose first name is Samia or Omar. 5. We often want to sort a list before working with it. Python makes it very easy to do this. We can use the sort() method on any list to sort the list’s elements in ascending order. Try this in your Python interpreter: 444 colors = [“green!, “purple!, “1lue!] 444 colors:sort56 444 print colors [71lue8, 7green8, 7purple8] Write a Python program that first sorts the list of Tanzanian presidents, then prints out only those presidents whose name starts with the letter “J”. 6. On your computer or tablet, write a Python program that defines the following list: people = [“Ju'a!, “*a'isi!] and then add all of the names in the tanzanian_presidents and tanzanian_.ice_presidents lists to the people list. Finally, print the people list to the screen; it should now contain the two original names, plus those of the five Tanzanian presidents and the five Tanzanian vice presidents. Your program should use a Python method to do this; before you write code for this problem, read about the different methods that you can use on Python lists here: https://docs.python.org/2/tutorial/datastructures.html#more-on-lists.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    2 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us