Lists
Lists
my_list = [ 1, 2.5, 'a' ] >>> list('a') # Function creates a list of characters from the string in the argument ['a']
list( )
>>> bashar_list = list('Bashar')
[ 'B','a','s','h','a','r' ]
Split & Join
split( )
>>> my_sentence = "Lorem ipsum dolor sit amet consectetuer adipiscing elit"
>>> sentence_list = my_sentence.split()
>>> sentence_list
[ 'Lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetuer', 'adipiscing', 'elit', ]
join( list_of_strings )
>>> ' '.join(sentence_list)
"Lorem ipsum dolor sit amet consectetuer adipiscing elit"
Shopping List Project
[shopping_list.py]:
shopping_list = list( ) print("What should we pick up at the store?") print("Enter 'DONE' to stop adding items.") # Use "while True:" to always create an infinite loop while True: new_item = input("> ") if new_item == 'DONE': break shopping_list.append(new_item) print("Added! List has { } items.".format( len(shopping_list)) ) continue print("Here's your list: ") for item in shopping_list: print("- " + item)