In this Section we are going to discuss about various data types in Python....
Mutable vs Immutable Objects
a = [5,10,15,20,25,30,35,40]
# a[2] = 15
print("a[2] = ", a[2])
# a[0:3] = [5, 10, 15]
print("a[0:3] = ", a[0:3])
# a[5:] = [30, 35, 40]
print("a[5:] = ", a[5:])
>>OUTPUT
a[2] = 15
a[0:3] = [5, 10, 15]
a[5:] = [30, 35, 40]
# t[1] = 'program'
print("t[1] = ", t[1])
# t[0:3] = (5, 'program', (1+3j))
print("t[0:3] = ", t[0:3])
# Generates error
# Tuples are immutable
t[0] = 10
In general, data types in Python can be distinguished based on whether objects of the type are mutable or immutable. The content of objects of immutable types cannot be changed after they are created.
Some immutable types:
| Some mutable types:
|
Numeric types:
- int: Integers; equivalent to C longs in Python 2.x, non-limited length in Python 3.x
- long: Long integers of non-limited length; exists only in Python 2.x
- float: Floating-Point numbers, equivalent to C doubles
- complex: Complex Numbers
Sequences:
- str: String; represented as a sequence of 8-bit characters in Python 2.x, but as a sequence of Unicode characters (in the range of U+0000 - U+10FFFF) in Python 3.x
- bytes: a sequence of integers in the range of 0-255; only available in Python 3.x
- byte array: like bytes, but mutable (see below); only available in Python 3.x
- list
- tuple
Python List
List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type.
Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ].
>>> a = [1, 2.2, 'python']
We can use the slicing operator [ ] to extract an item or a range of items from a list. Index starts form 0 in Python.
# a[2] = 15
print("a[2] = ", a[2])
# a[0:3] = [5, 10, 15]
print("a[0:3] = ", a[0:3])
# a[5:] = [30, 35, 40]
print("a[5:] = ", a[5:])
>>OUTPUT
a[2] = 15
a[0:3] = [5, 10, 15]
a[5:] = [30, 35, 40]
Lists are mutable, meaning, value of elements of a list can be altered.
>>> a = [1,2,3]
>>> a[2]=4
>>> a
[1, 2, 4]
Python Tuple
Tuple is an ordered sequence of items same as list.The only difference is that tuples are immutable. Tuples once created cannot be modified.
Tuples are used to write-protect data and are usually faster than list as it cannot change dynamically.
It is defined within parentheses () where items are separated by commas.
>>> t = (5,'program', 1+3j)
We can use the slicing operator [] to extract items but we cannot change its value.
t = (5,'program', 1+3j)# t[1] = 'program'
print("t[1] = ", t[1])
# t[0:3] = (5, 'program', (1+3j))
print("t[0:3] = ", t[0:3])
# Generates error
# Tuples are immutable
t[0] = 10
OUTPUT:
t[1] = program
t[0:3] = (5, 'program', (1+3j))
Traceback (most recent call last):
File "<stdin>", line 11, in <module>
t[0] = 10
TypeError: 'tuple' object does not support item assignment.
Python Strings
String is sequence of Unicode characters. We can use single quotes or double quotes to represent strings. Multi-line strings can be denoted using triple quotes,
'''
or """
.>>> s = "This is a string"
>>> s = '''a multiline
Like list and tuple, slicing operator [ ] can be used with string. Strings are immutable.
s = 'Hello world!'
# s[4] = 'o'
print("s[4] = ", s[4])
# s[6:11] = 'world'
print("s[6:11] = ", s[6:11])
# Generates error
# Strings are immutable in Python
s[5] ='d'
OUTPUT:
s[4] = o
s[6:11] = world
Traceback (most recent call last):
File "<stdin>", line 11, in <module>
s[5] ='d'
Type Error: 'str' object does not support item assignment.
Python String Slicing
Python Supports backward Indexing unlike others.
let string s='PYTHON'
slicing syntax is like this [starting range : ending range : step]
for forward slicing starting range < ending range
for backward slicing starting range > ending range
s[ 2: ] = 'THON'
s[ 2 : 4 ]='TH'
s[ 4 : 2 ] =' ' (will print nothing because for forward slicing starting range < ending range)
s[ -1 : -3]=' ' (same as above)
s[ -3 : -1 ]='HO'
s[ : : 2 ] = 'PTO' (step is 2)
s[ 4 : 1 : -2] ='OT'
s[ : : -1] = 'NOHTYP' (reversing the string)
This section continues.....
0 on: "Data Types In Python"