Python Notes Day05
Python Notes Day05
1. Subscription:
– Indexing
– Slicing
– Assignment
2. Range Function/Object
3. Datatypes:
– Fundamental
– Derived
4. Mutability/Immutability
5. Obejct Initialization
6. String Interning
- Before Starting let's look into, what are the Fundamental and
Derived Datatypes we have in Python:
I) Literal / Fundamental Datatypes:
1. Integer
2. Float
3. Complex
4. Boolean
5. String
6. None
.....etc......
1. Subscription(Indexing)
• Indexing is a way to access individual elements within a sequence, such as list, tuple,
or string, Using their position (index) in that sequence.
1. Positive Indexing:
• Starts from 0 for the first element, 1 for the second, and so on.
# For example:
# indexing in a string
str1 = 'Apple'
print(str1[0]) # for accessing the first character of a string
print(str1[1]) # for accessing the seconds character of a string
print(str1[4]) # accessing the last character of a string
A
p
e
# indexing in list
lst1 = [101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
print(lst1[0]) # accessing the first element of list
print(lst1[3]) # accessing the fourth element of list
print(lst1[9]) # accessing the last element of list
101
104
110
2. Negative Indexing:
• Starts from -1 for the last element, -2 for the second last, and so on.
# For example:
str2 = 'Python'
print(str2[-1]) # accessing last character of string
print(str2[-2]) # accessing second last character of string
n
o
Betty Bought Some Butter but the butter was bitter so betty bought
some better butter to make the bitter butter better!
Last Character of String: !
# List
lst1 = list(range(101,200))
print(lst1)
print('Last Element of the List: ', lst1[-1])
[101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128,
129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170,
171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198,
199]
Last Element of the List: 199
• In case we have very long values in a string or list accessing the characters/elements is
very difficult by positive indexing. In such cases we use NEGATIVE INDEXING.
Index Assignment :
• Index assignment is a powerful feature that allows you to manipulate data efficiently in
Python.
• Using index assignment we can modify the elements within sequences like lists.
• Note that you cannot perform index assignment on immutable objects like string, tuple
and sets.
# FOr example:
list1 = list(range(101, 111))
print(list1)
# Accessing the 4th index element
print('4th index element of list: ', list1[4])
# Modifying the 4th index element using index assignment
list1[4] = 501
print('After modifying: ', list1)
[101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
4th index element of list: 105
After modifying: [101, 102, 103, 104, 501, 106, 107, 108, 109, 110]
print(list1)
# midifying the last element of the list by negative indexing
list1[-1] = 200
print(list1)
[101, 102, 103, 104, 501, 106, 107, 108, 109, 110]
[101, 102, 103, 104, 501, 106, 107, 108, 109, 200]
Slicing :
• Syntax for Slicing:
– Slicing in Python takes 3 parameters
'Python'
'general'
'language'
'language'
Steps in Slicing
# For example:
abc = 'Python'
print(abc[0:6:1]) # 1 the dfault step size in python
Python
Pto
Ph
print(abc)
print(abc[-4::])
Python
thon
print(abc[-4::-1])
tyP
- Slice Assignment:
- Slice assignemnt in Python is a technique used to access and modify
a portions of a sequences, such as a lists, by specifying a rang of
indices.
# For example:
str1 = 'Sameer Shaikh'
str1[-6:]
'Shaikh'
str1[-6:] = 'Sharma'
----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[38], line 1
----> 1 str1[-6:] = 'Sharma'
[101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
[101, 102, 103, 104, 205, 206, 207, 108, 109, 110]
[ 0 1 2 3 4 5 6 7 8 9]
[101,102,103,104,105,106,107,108,109,110]
[ 0 1 2 3 4 5 6 7 8]
[101,102,103,104,205,206,108,109,110]
• So according to rule 1, here if we assign only two elemetns instead of three, the two
elements list will take the place of that three elements and the index number will get
shift to the next element.
# Examples of rule no 2
# before understanding rule no. 2 practically let's see bit about
tuples.
# To create tuple
tupl1 = (1,2,3)
print(tupl1)
print(type(tupl1))
(1, 2, 3)
<class 'tuple'>
(1, 2, 3)
<class 'tuple'>
# Another way is
tupl3 = (1,) # single element tuple
print(tupl3)
print(type(tupl3))
(1,)
<class 'tuple'>
# Can we do this
tupl4 = (1)
print(tupl4)
print(type(tupl4))
1
<class 'int'>
• In above example it is not considered as tuple, single element inside parathesis without
comma(,) is not considered as tuple, It will be considered as an Integer.
# Now, understanding rule no. 2 practically
lst_01 = list(range(101,111))
print(lst_01)
[101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
lst_01[4:7] = [205,206,207]
print(lst_01)
[101, 102, 103, 104, 205, 206, 207, 108, 109, 110]
----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[58], line 1
----> 1 lst_01[4:7] = 205
2 print(lst_01)
• We cannot do this, because according to rule no. 2 the values replacing the original
values muwt be an Iterable.
• It should not be a litral instead literal collection.