# building a list
mylist = []
for item in range(0,10):
mylist.append(item*2)
mylist
Run to view results
[item*2 for item in range(0,10)]
Run to view results
# Write a function that takes a string and prints every letter in that string. use a for loop.
mystring='abcde'
def myfunc(anystring):
mylist=[]
for x in anystring:
mylist.append(x)
print(mylist)
myfunc(mystring)
Run to view results
# Rewrite this same function using list comprehension.
def myfunc(anystring):
mylist = [x for x in anystring]
print(mylist)
myfunc(mystring)
Run to view results
# Write a function that will receive any list of numbers,
# and add 5 to each element of that list. Use a for loop.
def myfunc(numlist):
if type(numlist)==list:
new_nums=[]
for n in numlist:
if type(n)==int:
new_nums.append(n+5)
else:
pass
return new_nums
else:
print('please provide a list!')
myfunc([3,4,5,6,'bear',7,2,8])
Run to view results
# Rewrite this same function using list comprehension.
def myfunc(numbers):
new_nums=[n+5 for n in numbers if type(n)==int]
return new_nums
myfunc([5,4,7,'bear',2,4])
Run to view results
# Import the numpy library.
import numpy as np
# Write a function that receives any list n and "Binarize" n so that
# any value greater than or equal to the mean of n is 1, otherwise 0.
def myfunc(n):
new_n=[]
mean_val = np.mean(n)
print(mean_val)
myfunc([4,5,6,1,2])
Run to view results
def myfunc(n):
new_n=[]
mean_val = np.mean(n)
print(mean_val)
for num in n:
if num >= mean_val:
new_n.append(1)
else:
new_n.append(0)
return new_n
myfunc([4,5,6,1,2])
Run to view results
# Rewrite this same function using list comprehension.
def myfunc(n):
mean_val = np.mean(n)
return [1 if num >= mean_val else 0 for num in n]
myfunc([4,5,6,1,2])
Run to view results
# Write a function to swap 1s to 0s and 0s to 1s. If the element is neither a 0 nor 1, make it None.
# Use a for loop.
def change_numer(num_list):
result = []
for binari in num_list:
if binari == 0:
result.append(1)
elif binari == 1:
result.append(0)
else:
result.append(None)
return result
change_numer([1,1,1,1,0,0,0,0,1,0,1,0,1,10,111])
Run to view results
# Rewrite this same function using a list comprehension.
number_list = [1,1,1,11,1,1,0,0,00,0,0,0,10]
number_list_changed = [(lambda x: 1 if x == 0 else (0 if x == 1 else None))
(binary) for binary in number_list]
print(number_list_changed)
Run to view results
# Write a function that receives a list and gets the square and square root of all non-negative numbers in the list.
# Use a for loop.
def my_funciton(n):
num_list = []
for num in n:
if num>0:
num_list.append((np.sqrt(num), num**2))
else:
None
print(num_list)
Run to view results
my_funciton([1,-2,3,4,-5,6,4])
Run to view results
# Rewrite this same function using a list comprehension.
def my_funciton(n):
return [(np.sqrt(num), num**2) for num in n if num >0]
Run to view results
my_funciton([1,-2,3,4,-5,6,4])
Run to view results
# Write a function that receives a list of Celsius temperatures and converts to Fahrenheit.
# The formula is (9/5)*temp + 32)
def myfunc(celsius):
return [((9/5)*temp + 32) for temp in celsius]
Run to view results
myfunc([0,10,20.1, 34.5])
Run to view results
Run to view results