Guest User

Untitled

a guest
Nov 20th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. """
  2. Take a list, say for example this one:
  3.  
  4. a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  5. and write a program that prints out all the elements of the list that are less than 5.
  6.  
  7.  
  8.  
  9.  
  10. Extras:
  11.  
  12. Instead of printing the elements one by one, make a new list that has all the elements less than 5 from this list in it and print out this new list.
  13.  
  14. XWrite this in one line of Python.X
  15.  
  16. Ask the user for a number and return a list that contains only elements from the original list a that are smaller than that number given by the user.
  17. """
  18.  
  19.  
  20. list_1 = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  21.  
  22. for num in list_1:
  23. if num < 5:
  24. print (num)
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. #Extra 1
  34.  
  35. """
  36. Following is the syntax for append() method:
  37.  
  38. list.append(obj)
  39. Parameters
  40. obj -- This is the object to be appended in the list.
  41. """
  42.  
  43. list_2 = []
  44. for num in list_1:
  45. if num < 5:
  46. list_2.append (num)
  47. print (list_2)
  48.  
  49.  
  50. #Extra 2
  51. #XXX
  52.  
  53.  
  54. #Extra 3
  55.  
  56. the_number = int(input("Please enter a number: "))
  57.  
  58. list_3 = []
  59. for x in list_1:
  60. if x < the_number:
  61. list_3.append (x)
  62. print (list_3)
Add Comment
Please, Sign In to add comment