Lists in Python (TBC)
Lists in Python (TBC)
CLASS 11
CBSE
Characteristics of DATA TYPES
Immutable Mutable
The variable names are stored as ref. In the same memory address, new value
to a value-object. Each time we can be stored multiple times.
change the value, the variable’s
memory address change.
DATA TYPES
Booleans (IM) Subtype of plain integers. It takes only two values True (1) or
False (0)
Floating point numbers Double precision floating point numbers (15 digit precision)
(IM)
Complex Numbers A pair of floating point numbers A + Bj , the real and imaginary
(IM) part.
z.real gives the real part and
z.imag gives the imaginary part as a float, not as a complex value
DATA TYPES
Strings (IM) Sequence of any type of known characters and each character can be
individually accessed using index
Lists (M) Group of comma-separated values of any data type between square
brackets. Index starts from 0 and so on
E.g., a = [5,2,9,4] or [„h‟, „k‟, „s‟] or [8 , 3.45 , „A‟ , „W‟, „Miyu‟]
Sets (M) Similar to lists, but between the curly brackets. It can take values
of different types but cannot contain mutable elements and
duplicate entries.
E.g., a = {5, 3, 8, 4}
0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
L1 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
L1[2] = 30
Traversing a List
0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
#program #backend
L1 = [„a‟, „e‟, „I‟, „o‟, „u‟] for 0 in L1[o] > print(0) > a
for i in L1: for 1 in L1[1] > print(1) > e
print(i) ----------
a e i o u
-5 -4 -3 -2 -1
1) Using ‘in’ operator inside for loop
List1 = [10,2,4,5]
sum=0
for i in List1:
sum = sum +1
print(“the elements in the list are”, List1)
print(“the sum is: “, sum)
2) Using range() function
#program to traverse list using while loop O<5 -- L1[0] ---p index=0+1=1
L1 = [„p‟, „y‟, „t‟, „h‟, ‟o‟] 1<5 -- L1[1] ---y index=1+1 =2
index = 0 --------------
4<5 ----L1[4] ---0 index=4+1=5
while index < len(L1):
5<5 ----- abort
print(L1(index))
index += 1
Lists are objects, they are mutable / modifiable. If we assign the elements of one
list to another list, both shall refer to the same object.
>>>Print(subjectcodes[2][0])
[„maths‟]