In [ ]:
squares=[0,1,4,9,16,25] #list_name = [value1,value2,value3,...]
print(squares)
print("Length is:",len(squares))
In [ ]:
print(squares[3])
In [ ]:
squares.append(36)
print(squares)
In [ ]:
squares.append("forty-nine")
print(squares)
In [ ]:
squares.extend([64,81])
print(squares)
In [ ]:
squares[7]=49
print(squares)
In [ ]:
print(squares[0:3])
In [ ]:
4 in squares
Link: https://docs.python.org/3/tutorial/datastructures.html
In [ ]:
L1 = [ 10, [20,21,22,23], 30, [40,41], 50 ]
print(L1)
In [ ]:
print(L1[0])
In [ ]:
print(L1[1])
In [ ]:
print(L1[0][0]) # L1[0] gives us the first element of the list, which is an integer(10). Since integers cannot be
# indexed like a string or list, we get an error.
In [ ]:
print(L1[1][0])
In [ ]:
n1=input("Enter first number: ")
n2=input("Enter second number: ")
In [ ]:
sum=n1+n2
print(sum)
In [ ]:
a=int(n1)
b=int(n2)
sum=a+b
print(sum)
In [ ]:
num='-3.15'
pie=float(num)
print(pie)
Getting comfortable with list operations is important. Even though the problem can be concisely solved with the
use of for loop, since loops haven't been taught yet, you can solve it by a bunch of slicing statements instead.
1. Take a string as input and print it explained by the example below. Given length of string is equal to 12.
Example
--------
Input : HelloWorlds!
Output :
HelloWorlds!
Hlools
Hlod
Hol
HWs
Ho
!sdlroWolleH
!drWle
!lWl
!rl
!oe
!W
2. Take two strings as input and print it as explained by the example below.
Total sum length of both strings is 19.
Example
-------
Input : string1 --> "DireStraits", string2 --> "TheKinks"
Output :
stiartSeriDskniKehT
sirSrDsih
stDs
srs
ss
In [ ]: