tuomasvaltanen

Untitled

Nov 11th, 2021 (edited)
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. # break, continue, append
  2. print("Welcome!")
  3.  
  4. # NEW FILE
  5.  
  6. # break is usually used when you need to find something in the list, but only FIND ONE ITEM
  7. cities = ['Amsterdam','Helsinki', 'Rome', 'Budapest', 'Dublin',
  8.           'London', 'Bratislava', 'Moscow', 'Kiev', 'Vienna']
  9.  
  10. # a long city name is at least 7 characters long
  11. print("START")
  12.  
  13. for city in cities:
  14.     if len(city) <= 7:
  15.         print(city)
  16.         break
  17.  
  18. print("END")
  19.  
  20. # NEW FILE
  21.  
  22. # continue is used to skip a value we're not looking for
  23. cities = ['Amsterdam','Helsinki', 'Rome', 'Budapest', 'Dublin',
  24.           'London', 'Bratislava', 'Moscow', 'Kiev', 'Vienna']
  25.  
  26. # a long city name is at least 7 characters long
  27. print("START")
  28.  
  29. for city in cities:
  30.     if len(city) <= 7:
  31.         continue
  32.  
  33.     print(city)
  34.  
  35.  
  36. print("END")
  37.  
  38. # NEW FILE
  39.  
  40. # sometimes you need to filter values into separate lists, append is useful here
  41.  
  42. cities = ['Amsterdam','Helsinki', 'Rome', 'Budapest', 'Dublin',
  43.           'London', 'Bratislava', 'Moscow', 'Kiev', 'Vienna']
  44.  
  45. # initialize empty lists for the loop later
  46. short_city_names = []
  47. long_city_names = []
  48.  
  49. # a long city name is at least 7 characters long
  50. print("START")
  51.  
  52. for city in cities:
  53.     if len(city) <= 6:
  54.         short_city_names.append(city)
  55.     else:
  56.         long_city_names.append(city)
  57.  
  58. # later we can use these extra list to print content in more sophisticated way
  59. print()
  60. print("Here are the cities with a short name:")
  61.  
  62. for city in short_city_names:
  63.     print(city)
  64.  
  65. print()
  66. print("...and here are the cities with a long name:")
  67.  
  68. for city in long_city_names:
  69.     print(city)
Add Comment
Please, Sign In to add comment