# 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
mystring[0]
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
def test_func(list_ex):
if isinstance(list_ex, list):
new_list = []
for i in list_ex:
new_list.append(i + 5)
return new_list
else:
print("Not a list")
test_func([0,1,2,3,4,5])
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)
for i in n:
if i >= mean_val:
print(1)
else:
print(0)
print(f"Mean Val: {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 swap(list_ex):
new_list = []
for i in list_ex:
if i == 1:
new_list.append(0)
elif i == 0:
new_list.append(1)
else:
new_list.append('none')
return new_list
swap(range(1,10))
Run to view results
def swap(list_ex):
return [0 if i == 1 else 1 if i == 0 else 'none' for i in list_ex]
print(swap(range(1,10)))
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 long_func(list_ex):
list_new = []
for i in list_ex:
if i >= 0:
list_new.append((i**2, np.sqrt(i)))
return list_new
long_func([3,4,1,0])
Run to view results
# Rewrite this same function using a list comprehension.
def long_func(list_ex):
return [(i**2, np.sqrt(i)) for i in list_ex if i >= 0]
print(long_func([3, 4, 1, 0]))
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