Created
February 5, 2019 22:48
-
-
Save pdbartsch/d4f2ac02bc3d3c2de1a2c307afc16972 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.practicepython.org/exercise/2014/02/15/03-list-less-than-ten.html | |
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] | |
def less_than_ten(a): | |
for n in a: | |
if n < 10: | |
print(n) | |
less_than_ten(a) | |
## extras: | |
# 1. 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. | |
def less_than_ten_list(a): | |
lesslist = [] | |
for n in a: | |
if n < 10: | |
lesslist.append(n) | |
return lesslist | |
less_than_ten_list(a) | |
# 2. Write this in one line of Python. | |
print([ x for x in a if x < 10]) | |
# 3. 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. | |
num = float(input("Please enter a number. ")) | |
print([ x for x in a if x < num]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment