Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # break, continue, append
- print("Welcome!")
- # NEW FILE
- # break is usually used when you need to find something in the list, but only FIND ONE ITEM
- cities = ['Amsterdam','Helsinki', 'Rome', 'Budapest', 'Dublin',
- 'London', 'Bratislava', 'Moscow', 'Kiev', 'Vienna']
- # a long city name is at least 7 characters long
- print("START")
- for city in cities:
- if len(city) <= 7:
- print(city)
- break
- print("END")
- # NEW FILE
- # continue is used to skip a value we're not looking for
- cities = ['Amsterdam','Helsinki', 'Rome', 'Budapest', 'Dublin',
- 'London', 'Bratislava', 'Moscow', 'Kiev', 'Vienna']
- # a long city name is at least 7 characters long
- print("START")
- for city in cities:
- if len(city) <= 7:
- continue
- print(city)
- print("END")
- # NEW FILE
- # sometimes you need to filter values into separate lists, append is useful here
- cities = ['Amsterdam','Helsinki', 'Rome', 'Budapest', 'Dublin',
- 'London', 'Bratislava', 'Moscow', 'Kiev', 'Vienna']
- # initialize empty lists for the loop later
- short_city_names = []
- long_city_names = []
- # a long city name is at least 7 characters long
- print("START")
- for city in cities:
- if len(city) <= 6:
- short_city_names.append(city)
- else:
- long_city_names.append(city)
- # later we can use these extra list to print content in more sophisticated way
- print()
- print("Here are the cities with a short name:")
- for city in short_city_names:
- print(city)
- print()
- print("...and here are the cities with a long name:")
- for city in long_city_names:
- print(city)
Add Comment
Please, Sign In to add comment