---- define a function ----
def double(sequence): #this functions creates a sequence of elements
result = [] #the result is an array of elements
for element in sequence: #this is pretty literal, for each of the elements inside the sequence
result = result + [element*2] #the result-array is the given array with its elements*2
return result
double([2, 3, 5])
[4, 6, 10]
double([-53])
[-106]
double([334253, 235, 653, (-23*5)])
[668506, 470, 1306, -230]
double([])
[]
double() #it's gonna be an error
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_4283/4249856066.py in <module> ----> 1 double() #it's gonna be an error TypeError: double() missing 1 required positional argument: 'sequence'
----check your expectations----
def tip():
print('check your expectations')
tip()
check your expectations
x = [1,2,3]
double(x)
[2, 4, 6]
x
[1, 2, 3]
this is a function which take all the elements of an array and multiply them by 2, withouth modifying the original array
def double(sequence): result = [] for element in sequence: result = result + [element*2] return result
double(9)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_4283/3171148046.py in <module> ----> 1 double(9) /tmp/ipykernel_4283/2190915392.py in double(sequence) 1 def double(sequence): 2 result = [] ----> 3 for element in sequence: 4 result = result + [element*2] 5 return result TypeError: 'int' object is not iterable
double([7,2,4],[4])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_4283/2869834229.py in <module> ----> 1 double([7,2,4],[4]) TypeError: double() takes 1 positional argument but 2 were given
---------- FREE PROJECT 5-1 ----------
def triple(sequence):
result=[]
for element in sequence:
result=result+[element*3]
return result
triple([1,2,4])
[3, 6, 12]
def repeat(sequence): #standardization of symbols into commodity
result=[]
for element in sequence:
result = result + ['1$']
return result
repeat([3,6])
['1$', '1$']
repeat([1,2,4])
['1$', '1$', '1$']
def addone(sequence):
result = []
for a in sequence:
result = result + [a+1]
return result
addone([3,7,5])
[4, 8, 6]
addone([0])
[1]
addone([])
[]
def total(sequence):
tot = 0
for element in sequence:
tot = tot + element
return tot
total([4, 5, 6, 7])
22