PYTHON_UNIT1 & 2
PYTHON_UNIT1 & 2
Eg is on next page
See Terminal :
Showing error
In line 2
• Output :
Eg : next slide
• Negative Indexing
b = “ankita!“ b=“ankita!”
print(b[-5:-2]) output: kit eg:- print(b[2,-2])
07-11-2024 PRATEEK AGARWAL PYTHON BCC302 44
07-11-2024 PRATEEK AGARWAL PYTHON BCC302 45
Few more methods can be used in string
are :
• Split String : The split() method splits the string into substrings if
it finds instances of the separator
• Tuples are written with round brackets. Tuple items are ordered,
unchangeable, and allow duplicate values.
• Tuple items are indexed, the first item has index [0], the second
item has index [1] etc.
• To determine how many items a tuple has, use the len() function:
• But, in Python, we are also allowed to extract the values back into
variables. This is called "unpacking":
• Dictionary keys are case sensitive, the same name but different
cases of Key will be treated distinctly.
for i in range(5):
print(i, end=" ")
print()
Output:
01234
07-11-2024 PRATEEK AGARWAL PYTHON BCC302 79
Syntax of Python range() function
Syntax: range(start, stop, step)
Parameter :
start: [ optional ] start value of the sequence
stop: next value after the end value of the sequence
step: [ optional ] integer value, denoting the difference
between any two numbers in the sequence.
02468
07-11-2024 PRATEEK AGARWAL PYTHON BCC302 82
Python range() using Negative Step
If a user wants to decrement, then the user needs steps to be
a negative number.
# incremented by -2
for i in range(25, 2, -2):
print(i, end=" ")
print()
Output :
25 23 21 19 17 15 13 11 9 7 5 3
07-11-2024 PRATEEK AGARWAL PYTHON BCC302 83
Mutable and Immutable Data Types :
• Every variable in Python holds an instance of an object. There are two
types of objects in Python i.e. Mutable and Immutable objects.