range Data Type represents a sequence of numbers. The elements present in range Data type are not modifiable. i.e range Data type is immutable.
Form-1:
range(10) generate numbers from 0 to 9
Form-2: range(10,20)
generate numbers from 10 to 19
Form-3: range(10,20,2)
2 means increment value by 2
We can access elements present in the range Data Type by using
loops
index.
Using loops:
Using Index:
We cannot modify the values of range data type
Example: r[0]=100 TypeError: 'range' object does not support item assignment We can create a list of values with range data type
Example: 1) >>> l = list(range(10)) 2) >>> l 3) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
bytes Data Type: bytes data type represents a group of byte numbers just like an array. The only allowed values in bytes data type are in range of 0- 256
example: x=[10,20,30,40,50] b=bytes(x) type(b)----> bytes
You can save numbers in bytes datatype object but only in a particular range(0,256)
If you try to save a number out of this range you will get ValueError:
In Python if you talk about range(0,256) you mean the allowed numbers are 0 to 255 , Therefore even 256 is not allowed in bytes
bytes is IMMUTABLE therefore item assignment is not allowed
If you try so, you will get TypeError
bytearray Data type:
bytearray is exactly same as bytes data type except that its elements can be modified.
Once we create an object, we cannot perform any changes in that object. If we try to make changes then with those changes a new object will be created. This non changeable behaviour is called as Immutability.
suppose i have a variable x x=10 ---> i want to store 10 in x now this object is made with 10 in it.. Further if I want to change the data to 20 x=20 I cannot do that in the original object A new object will be created in the memory and data with required changes will be stored in that new object.
We can prove this thing by using id() function id() function tells us the address of the object
If the changes are made in the same object then id(x) before changes and after changes should have remained same But that's not the case id(x) before changes and after changes comes to be different. This proves that if we try to make changes in an immutable object, then with all those changes a new object will be created and new data will be stored in that new object. See the below examples:
All Fundamental Data types are immutable. i.e once we creates an object,we cannot perform any changes in that object. If we are trying to change then with those changes a new object will be created. This non-changeable behavior is called immutability. In Python if a new object is required, then PVM wont create object immediately. First it will check is there any object available with the required content or not. If available then existing object will be reused. If it is not available then only a new object will be created. The advantage of this approach is memory utilization and performance will be improved. But the problem in this approach is,several references pointing to the same object,by using one reference if we are allowed to change the content in the existing object then the remaining references will be effected. To prevent this immutability concept is required. According to this once we create an object then we are not allowed to change content. If we are trying to change with those changes a new object will be created. Immutability is a beautiful concept to save program from data inconsistency. In Python if you want to store similar data then object sharing happens internally (only in case of fundamental data types) Suppose i want to save subject of student s1 as Python s="Python" For this data 'Python' an object will be created in the memory. Now onwards, if anywhere in the program you want to save the string 'Python' again then in that case, you will be asked to share the earlier object only. Means for similar string no new object will be created, you will be asked to go to earlier object using address of that object
This happens with all objects of fundamental datatype. In the above example: s1,s2,s3, and s4 are sharing a single object.
Now suppose student no. 4 wants to change subject s4='Java' if s4 will try to make changes in the original object then there will be inconsistency as the other students s1 s2 and s3 are also pointing to the same object but they are not changing there subject. How can you ask them to suffer because of s4?? If we allow s4 to make changes in the original object then the other three students will definitely suffer. So, we introduce IMMUTABILITY We wont allow s4 to change the data in original object
If s4 wants to change data then its better we create a new object and ask s4 to refer that. observe the below diagram:
We can use this data type to
represent boolean values.
The only allowed values for
this data type are:
True and False
Internally Python represents
True as 1
and
False as 0
b=True
type(b) ------------.>bool
Example:
a=10
b=20
c=a<b
print(c)==>True
True+True==>2
True-False==>1
str datatype:
str represents String data
type.
A String is a sequence of
characters enclosed within single quotes or double quotes.
s1='Python'
s1="Python"
By using single quotes or
double quotes we cannot represent multi line string literals.
s1="Hello Learners!!
Welcome to my YouTube
Channel"
For this requirement we
should go for triple single quotes(''') or triple double
quotes(""")
s1= '''Hello Learners!!
Welcome to my YouTube
Channel '''
s1=""" Hello
Learners!!
Welcome to my YouTube
Channel """
We can also use triple
quotes to use single quote or double quote in our String.
'''MV’s Code Guide '''
"""MV’s Code Guide"""
Indexing in Python:
In Python, Strings follows
zero based index.
The index can be either positive or negative.
Positive index means forward
direction from Left to Right
Negative index means backward direction
from Right to Left
>>> s="Python"
2) >>> s[0]
3) 'P'
4) >>> s[1]
5) 'y'
6) >>> s[-1]
7) 'n'
8) >>> s[40]
IndexError: string index out
of range
0 1 23 45
Python
-6-5 -4 -3-2 -1
Slicing of Strings:
slice means a piece
[ ] operator is called slice
operator, which can be used to retrieve parts of String. Syntax: variable_name[begin:end:step] By default begin value is 0 end value is length of string
step value is 1
If you don't specify anything then default values will be takes
Example:
s="Python"
If you don't specify begin , end and step value
s[::]
then, internally it will be considered as
s[0:length of string:1]
See more examples below:
Repetition operator in string:
we can use * multiplication operator in Python to perform repetition of strings e.g.: s="Python"*3
print(s)
Output: 'PythonPythonPython'
Length of string:
To check the length of the string we can use function len()
J J J J J J J J J J I I I I I I I I I I H H H H H H H H H H G G G G G G G G G G F F F F F F F F F F E E E E E E E E E E D D D D D D D D D D C C C C C C C C C C B B B B B B B B B B A A A A A A A A A A
Code:
1
2
3
4
5
n=int(input("Enter the number of rows: "))forjinrange(1,n+1):foriinrange(1,n+1):print(chr(65+n-j),end=" ")print()
****************************************************** ****************************************************** J I H G F E D C B A J I H G F E D C B A J I H G F E D C B A J I H G F E D C B A J I H G F E D C B A J I H G F E D C B A J I H G F E D C B A J I H G F E D C B A J I H G F E D C B A J I H G F E D C B A
Code:
1
2
3
4
5
n=int(input("Enter the number of rows: "))forjinrange(1,n+1):foriinrange(1,n+1):print(chr(65+n-i),end=" ")print()