l=[7,4,2,6]
for num in l:
print(num)
7 4 2 6
for num in l:
print(num*2)
14 8 4 12
def mean(sequence):
total=0
for element in sequence:
total=total+element
return total/len(sequence)
short=[5,5,5,5]
mean(short)
5.0
short=[-5,0,5]
mean(short)
0.0
x=5
type(x)
int
type(5)
int
type(7)
int
type(36)
int
type(5.8)
float
type([1,2,3])
list
type('hello world')
str
'hello'+'world'
'helloworld'
'hello'+' '+'world'
'hello world'
a='hello'
b='world'
c=' '
a+c+b
'hello world'
len('hello world')
11
len([1,2,3])
3
len(['hello','world'])
2
len(5)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_6819/4108502181.py in <module> ----> 1 len(5) TypeError: object of type 'int' has no len()
2+'hello'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_6819/2596768515.py in <module> ----> 1 2+'hello' TypeError: unsupported operand type(s) for +: 'int' and 'str'
volume=[4.0,2.0,3.0,5.5]
result=[]
for element in volume:
result=result+[element*2]
result
[8.0, 4.0, 6.0, 11.0]
result=[]
result
[]
result=result+[4.0]
result
[4.0]
result=result+[2.0]
result
[4.0, 2.0, 2.0]
result=result+[3.0]
result
[4.0, 2.0, 2.0, 3, 0, 5.5, 3.0]
result=result+[5.5]
result
[4.0, 2.0, 2.0, 3, 0, 5.5, 3.0, 5.5]
result=[]
result=result+[4.0]
words='hello','world'
double(words)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_6819/2412218211.py in <module> ----> 1 double(words) NameError: name 'double' is not defined