Guest User

Untitled

a guest
Jan 21st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #List less than ten - exercise 3
  2. '''Write a program that prints out all the elements of the list that are less than 5.'''
  3.  
  4. lista = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  5. listb = []
  6.  
  7. #This is called a 'for loop'
  8. for element in lista:
  9. if element < 5:
  10. listb.append(element)
  11.  
  12. print (listb)
  13.  
  14. #This is called using list comprehension - to write this in one line of Python
  15. lista = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  16. listb = [element for element in lista if element < 5]
  17.  
  18. print (listb)
  19.  
  20. '''Ask the user for a number and return a list that contains only elements
  21. from the original list a that are smaller than that number given by the user.'''
  22. lista = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  23. listb = []
  24. usernumber = int(input ("Alright -- give me a number! I'll use that as the max value.: "))
  25.  
  26. #Writing the for loop first
  27. for element in lista:
  28. if element < usernumber:
  29. listb.append(element)
  30.  
  31. print (listb)
  32.  
  33. #Using list comprehension to write it in one line
  34. usernumber = int(input ("Alright -- give me a number! I'll use that as the max value.: "))
  35. lista = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  36. listb = [element for element in lista if element < usernumber]
  37.  
  38. print (listb)
Add Comment
Please, Sign In to add comment