Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #List less than ten - exercise 3
- '''Write a program that prints out all the elements of the list that are less than 5.'''
- lista = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
- listb = []
- #This is called a 'for loop'
- for element in lista:
- if element < 5:
- listb.append(element)
- print (listb)
- #This is called using list comprehension - to write this in one line of Python
- lista = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
- listb = [element for element in lista if element < 5]
- print (listb)
- '''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.'''
- lista = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
- listb = []
- usernumber = int(input ("Alright -- give me a number! I'll use that as the max value.: "))
- #Writing the for loop first
- for element in lista:
- if element < usernumber:
- listb.append(element)
- print (listb)
- #Using list comprehension to write it in one line
- usernumber = int(input ("Alright -- give me a number! I'll use that as the max value.: "))
- lista = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
- listb = [element for element in lista if element < usernumber]
- print (listb)
Add Comment
Please, Sign In to add comment