16 Aug Wednesday Friday 15:00 Python Practice 20.08.14.
Question 1:
We have a list of following:
zoo = ['monkeys', 'tigers', 'pandas', 'penguins', 'giraffes', 'hippos', 'bears']
how to retrieve a slice of list zoo, so you can get the following result?
['monkeys', 'pandas', 'giraffes', 'bears']
Question 2:
from the same list of zoo in the question 1, how to retrieve a slice of list zoo, so you can get the following result?
['bears', 'penguins', 'monkeys']
Question 3:
Using split and join function of the list to change string
“green,red,yellow,blue,” into string:
“green light* red light* yellow light* blue light* “
Hint:
>>> vegetables = "carrots, potatoes, onions, leeks, celery"
>>> # string.split(separator) will split the string by separator, for example:
>>> vegetables = vegetables.split(", ")
>>> print(vegetables)
['carrots', 'potatoes', 'onions', 'leeks', 'celery']
>>> # string.join(list) will use string to connect every string item in the list, for example:
>>> fruits = ["avocados", "bananas", "oranges", "grapes", "mangoes"]
>>> ", ".join(fruit)
>>> 'avocados, bananas, oranges, grapes, mangoes'
more samples at: https://www.w3schools.com/python/ref_string_split.asp
No Comments