Ticker

6/recent/ticker-posts

Python3: Datatypes | Escape Characters

Datatype Description Is Immutable Example 
Int We can use to represent the whole/integral numbers Immutable >>> a=10 
>>> type(a) 
<class 'int' >
Float We can use to represent the decimal/floating point numbers Immutable >>> b=10.5 
>>> type(b) 
<class 'float'> 
Complex We can use to represent the complex numbers Immutable >>> c=10+5j 
>>> type(c) 
<class 'complex'> 
>>> c.real 
10.0 
>>> c.imag 
5.0 
Bool We can use to represent the logical values(Only allowed values are True and False) Immutable >>> flag=True 
>>> flag=False 
>>> type(flag) 
<class 'bool'> 
StrTo represent sequence of Characters Immutable >>> s='durga' 
>>> type(s) 
<class 'str'>
>>> s="durga" 
>>> s='''Durga Software Solutions ... Ameerpet''' >>> type(s) 
<class ''str'>
bytesTo represent a sequence of byte values from 0-255 Immutable >>> list=[1,2,3,4] 
>>> b=bytes(list) 
>>> type(b) 
<class 'bytes'>
bytearrayTo represent a sequence of byte values from 0-255 Mutable >>> list=[10,20,30] 
>>> ba=bytearray(list) 
>>> type(ba) 
<class 'bytearray'> 
rangeTo represent a range of values Immutable >>> r=range(10) 
>>> r1=range(0,10) 
>>> r2=range(0,10,2) 
listTo represent an ordered collection of objects Mutable >>> l=[10,11,12,13,14,15] 
>>> type(l) 
<class 'list'> 
tupleTo represent an ordered collections of objects Immutable >>> t=(1,2,3,4,5) 
>>> type(t) 
<class 'tuple'> 
setTo represent an unordered collection of unique objects Mutable >>> s={1,2,3,4,5,6} 
>>> type(s) 
<class 'set'> 
frozensetTo represent an unordered collection of unique objects Immutable >>> s={11,2,3,'Durga',100,'Ramu'} >>> fs=frozenset(s) 
>>> type(fs) 
<class 'frozenset'> 
dictTo represent a group of key value pairs Mutable >>> d={101:'durga',102:'ramu',103:'hari'} >>> type(d) 
<class 'dict'>

None Data Type: 

None means Nothing or No value associated. 

If the value is not available, then to handle such type of cases None introduced. 

It is something like null value in Java. 

Eg: 

def m1(): 
a=10 
print(m1()) 
None 

Escape Characters:

In String literals we can use esacpe characters to associate a special meaning.

1) >>> s="durga\nsoftware" 
2) >>> print(s) 
3) durga 
4) software 
5) >>> s="durga\tsoftware" 
6) >>> print(s) 
7) durga software 
8) >>> s="This is " symbol" 
9) File "<stdin>", line 1 
10) s="This is " symbol" 
11) ^ 
12) SyntaxError: invalid syntax 
13) >>> s="This is \" symbol" 
14) >>> print(s) 
15) This is " symbol 

The following are various important escape characters in Python

1) \n==>New Line
2) \t===>Horizontal tab
3) \r ==>Carriage Return
4) \b===>Back space
5) \f===>Form Feed
6) \v==>Vertical tab
7) \'===>Single quote
8) \"===>Double quote
9) \\===>back slash symbol

Constants:

Constants concept is not applicable in Python.

But it is convention to use only uppercase characters if we don’t want to change value.

MAX_VALUE=10

It is just convention but we can change the value.

"Escape Sequence In Python With Examples"

"Remove Escape Characters From String Python"

"Escape Sequence In Python 3"

"T In Python"

"Escape Character Not Working In Python"

"Python Escape Sequence List"

"Escape Sequence In Python"

"Backslash In Python"