0 ratings0% found this document useful (0 votes) 86 views299 pagesLearn Python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Pa @ python
Learn
Complete
Python
In
Simple Way
1| https://www.youtube.com/durgasoftware@ python
Introductio
Python is a general purpose high level programming language.
Python was developed by Guido Van Rossam in 1989 while working at National
Research Institute at Netherlands.
But officially Python was made available to public in 1991. The official Date of Birth for
Python is : Feb 20th 1991.
Python is recommended as first programming language for beginners.
Eel: To print Helloworld
Java:
public class Helleworld
‘ sv main(String|] args)
‘ SOP("Hello world");
:
11) -#includecstdio.h>
2) void main()
3) {
4) _ print("“Hetlo world");
s))
Python:
print(*Hello World")
£22: To print the sum of 2 numbers
davai
1) public class Add
24
2) public static void main(String[] args)
4)
3) https://www.youtube.com/durgasoftwareSystem.out.printin("The Sum:"+(a+b));
11) Hinelude
2)
3) void main()
4) 4
5) inta,b;
6)
7) b=20;
8) _ printi{"The Sum:%d",(a+b));
9))
3) print{"The Sum:",{a+b))
The name Python was selected from the TV Show
“The Camplete Monty Python's Circus", which was broadcasted in BBC from 1969 to 1974.
Guido developed Python language by taking almost all programming features from
different languages
1. Functional Programming Features from C
2, Object Oriented Programming Features from C++
3. Scripting Language Features from Perl and Shell Script
4, Modular Programming Features from Modula-3
Most of syntax in Python Derived from C and ABC languages.
We can use everywhere. The most commen impartant application areas are
1) For developing Desktop Applications:
2) For developing web Applications
3) For developing database Applicatians
4) https://www.youtube.com/durgasoftware@ python
4) For Network Programming
5) For developing games
6) For Data Analysis Applications
7) For Machine Learning
8) For developing Artificial Intelligence Applications
9) Far loT
Note;
Internally Google and Youtube use Python coding.
NASA and Nework Stock Exchange Applications developed by Python.
Top Software companies like Google, Microsoft, IBM, Yahoo using Python,
Features of Python:
» Simple and easy to learn:
Python is a simple programming language. When we read Python program,we can feel
like reading english statements.
‘The syntaxes are very simple and only 30+ kerywords are available.
‘When compared with other languages, we can write programs with very less number
of lines. Hence more readability and simplicity.
We can reduce development and cost of the project.
We can use Python software without any licence and it is freeware.
Its source code is open,so that we can we can customize based on our requirement,
Eg; Jython is customized version of Python to werk with Java Applications.
Python is high level programming language and hence it is programmer friendly
language.
Being a programmer we are not required to concentrate low level activities like
memory management and security ete.
‘Once we write a Python program, it can run on any platform without rewriting ance
again.
Internally PVM is responsible to convert into machine understandable form.
5) Portability:
Python programs are portable. ie we can migrate from one platform to another
platform very easily. Python programs will provide same results on any paltform.
https://www.youtube.com/durgasoftware@ python
6) Dynamically Typed:
* In Python we are not required to declare type for variables. Whenever we are
assigning the value, based on value, type will be allocated automatically.Hence Pythan
is considered as dynamically typed language.
* But Java, € etc are Statically Typed Languages b'z we have to provide type at the
beginning only.
© This dynamic typing nature will provide more flexibility to the programmer.
7) i it
Python language supports both Pracedure oriented (like C, pascal etc) and object
oriented (like C++, Java) features. Hence we can get benefits of both like security and
reusability etc
8) Interpreted:
+ Weare not required to compile Python programs explcitly, Internally Python
interpreter will take care that compilation.
if compilation fails interpreter raised syntax errors. Once compilation success then:
PVM (Python Virtual Machine) is responsible to execute.
9) Extensible:
«We can use other language programs in Python.
* The main advantages of this approach are:
= We can use already existing legacy non-Python code
= We can improve performance of the application
10) Embedded;
We can use Python pragrams in any other language programs.
Lewe can embedd Python programs anywhere.
11) Extensive Library:
© Python has a rich inbuilt library.
® Being a programmer we can use this library directly and we are net responsible to
implement the functionality, Ete,
Limitations of Python:
1) Performance wise not up to the mark because it is interpreted language.
2) Not using for mobile Applications,
©) https://www.youtube.com/durgasoftware@ python
Flavors of Python:
1) CPython:
It is the standard flavor of Python. It can be used to work with C lanugage Applications.
2) Jython OR JPython:
Itis for Java Applications. It can run on JVM
3)
It is for C#.Net platform
4) Pypy:
The main advantage of PyPy is performance will be improved because JIT compiler is
available inside PVM.
5) RubyPython
For Ruby Platforms
6)
It is specially designed for handling large volume of data pracessing.
Python Versions:
® Python 1.0Vintroduced in Jan 1994
F Python 2.0V introduced in October 2000
® Python 3.0V introduced in December 2008
Note: Python 3 won't provide backward compatibility to Python? i.e there is no
guarantee that Python2 programs will run in Python3,
Current versions
Python 3.6.1 Python 2.7.13
7) https://www.youtube.com/durgasoftware@ python
IDENTIFIERS
+ A\Name in Python Program is called Identifier.
* Itcan be Class Name OR Function Name OR Module Name OR Variable Name.
* a=10
1, The only allowed characters in Python are
+ alphabet symbols(either lower case or upper case)
* digits(0 to 9)
* underscore symbol(_)
By mistake if we are using any other symbol like $ then we will get syntax error.
© cash=104
+ ca$h=20 X
2, Identifier should not starts with digit
© 123total X
* total1273d
3. Identifiers are case sensitive. Of course Python language is case sensitive language.
‘total=10
TOTAL=999
print{total) #10
print(TOTAL) #999
8) https://www.youtube.com/durgasoftware@ python
Identifier:
1) Alphabet Symbols (Either Upper case OR Lower case)
2) Ifldentifier is start with Underscore (_) then it indicates it is private.
3) Identifier should not start with Digits.
4) Identifiers are case sensitive.
5) We cannot use reserved words as identifiers
Eg.def= 10x
6) There is no length limit for Python identifiers. But not recommended to use too
lengthy identifiers.
7) Dollor ($) Symbol is not allowed in Python.
Q) Which of the following are valid Python identifiers?
1) 123total *
2) totaliz3 J
3) java2shared
4) cagh x
5) _abc_abe_J
6) def x
7) itx
Note:
1) If identifier starts with _ symbol then it indicates that itis private
2) Ifidentifier starts with __(Two Under Score Symbols) indicating that strongly private
identifier.
3) If the identifier starts and ends with two underscore symbals then the identifier is
language defined special name, which is also known as magic methods.
4) Ex: add
°) https://www.youtube.com/durgasoftwareIn Python some words are reserved to represent some meaning or functionality.
Such types of words are called reserved words.
There are 33 reserved words available in Python.
+ True, False, None
* and, or ,no'
+ if,elif,else
* while, for, break, continue, return, in, yield
‘© try, except, finally, raise, assert
«import, from, as, class, def, pass, global, nonlocal, lambda, del, with
Note:
1. All Reserved words in Python contain only alphabet symbols.
2. Except the following 3 reserved words, all contain only lower case alphabet symbols.
© True
© False
* None
Eg: a= true X
acTrue J
>>> import keyword
>>> keyword. kwlist
['False’, 'None’, ‘True’, ‘and’, ‘as’, ‘assert, ‘break, ‘class’, ‘continue’, ‘def’, ‘de, ‘elif, ‘else’,
‘except’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘nonlocal’, ‘not’, ‘or’,
‘pass’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield']
10) https://www.youtube.com/durgasoftware@ python
DATA TYPES
+ Data Type represents the type of data present inside a variable.
+ In Python we are not required to specify the type explicitly. Based on value provided,
the type will be assigned automatically. Hence Python is dynamically Typed Language.
Python contains the following inbuilt data types
1) Int
2) Float
3) Complex
4) Bool
5) str
6) Bytes
7) Bytearray
8) Range
5) List
10) Tuple
11)Set
12) Frozenset
13) Diet
14) None
Note; Python contains several inbuilt functions
1) type
to check the type of variable
2) id()
to get address of object
11) https://www.youtube.com/durgasoftware@ python
3) print()
to print the value
In Python everything is an Object.
1) int Data Type:
We can use int data type to represent whole numbers (integral values)
Egia=10
‘type(a} #int
Note:
+ In Python2 we have long data type to represent very large integral values.
‘+ But in Python3 there is no long type explicitly and we can represent long values also by
using int type only.
We can represent int values in the following ways
2) Decimal form
2) Binary form
3) Octal form
4) Hexa decimal form
|) Decimal Form (Base-10);
* Itis the default number system in Python
+ The allowed digits are: 0 to 9
© Ega=10
Il)Binary Form (Base-2):
+ The allowed digits are:0 & 1
Literal value should be prefixed with Ob or OB
12) https://www.youtube.com/durgasoftware@ python
Ill) Octal Form (Base-8):
+ The allowed digits are :0 to7
+ Literal value should be prefixed with Oo or 00,
© Egza=00123
IV)
+ The allowed digits are: 0 to 9, a-f {both lower and upper cases are allowed)
+ Literal value should be prefixed with Ox or OX
a= OXBeer
Note: Being a programmer we can specify literal values in decimal, binary, octal and hexa
decimal forms. But PVM will always provide values only in decimal form.
a=10
b=0010
<-0X10
d=0B10
print(a}10
print(b)&
int(c}16
print{d)2
Base Conversions
Python provide the following in-built functions for base conversions
1)bin():
‘We can use bin() to convert from any base to binary
1) >>> bin(15)
2) ‘0b1111"
3) >>> bin(0011)
4) ‘Ob1001°
5) >>> bin(0X10)
13) https://www.youtube.com/durgasoftware@ python
6) ‘ob10000'
2)oct():
We ean use oct() to canvert from any base to octal
1) >>> oet(10)
2) "0012"
3) >>> oct(0B1111)
'4) ‘O017'
5) >>> oct(0X123)
6) "00443"
3)hex():
We can use hex) to convert from any base ta hexa decimal
|1) >>> hex{100)
2) "0x64"
3) >>> hex(0B111111)
\4) ‘Oxat"
5) >>> hex{0012345)
6) 'Oxtdes*
2) Float Data Type:
We can use float data type to represent floating point values (decimal values)
Egif= 1.234
typelf) float
‘We can also represent floating point values by using exponential form
(Scientific Notation)
Egif=1.2¢3 > instead of 'e' we can use 'E’
print(f) 1200.0
‘The main advantage of exponential form is we can represent big values in less
memory.
We can represent int valuesin decimal, binary, octal and hexa decimal forms. But we
can represent float values only by using decimal form.
14) https://www.youtube.com/durgasoftware@ python
2) >>> f20B12.01
2) File “”, tine 1
3) f-0B11,01
la) a
5) Syntax€rror: invalid syntax
6)
7) >>> f00123.456
8) Syntaxerrar: invalid syntax
9)
10) >>> f=0X123.456
111) SyntaxError: invalid syntax
3) Complex Data Type:
+ Acomplex number is of the form
i
i
Real Part Imaginary Part
a+bj——
‘a’ and ‘b’ contain Intergers OR Floating Point Values.
Fai 3+5)
1045.5)
0.5+0.1j
© Inthe real part if we use int value then we can specify that either by decimal, octal,
binary or hexa decimal form.
But imaginary part shauld be specified only by using decimal farm.
1) >>> ac0B11+5)
2) >>>a
3) (+5)
4) >>>a=340B13j
5) SyntaxError: invalid syntax
Even we can perform operations on complex type values.
2) >>>a=1041.5)
2) >>> b=20+2.5]
3) >>>ezatb
14) >>> print(e)
15) https://www.youtube.com/durgasoftware@ python
5) (30+4j)
6) >>> typete}
7), 10.5,
caimag > 3.6
We can use complex type generally in scientific Applications and electrical engineering
Applications.
4) bool Data Type:
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) boot
Eg:
a=10
b=20
ceach
print(c) > True
True+True > 2
True-False > 1
5) str Data Type:
str represents String data type.
A String is a sequence of characters enclosed within single quotes or double
quotes.
16) https://www.youtube.com/durgasoftwareBy using single quotes or double quotes we cannot represent multi line string
literals.
si="durga
soft"
For this requirement we should go for triple single quotes("") or triple double
quotes(""")
"durga
soft"
si="""durga
soft"
We can also use triple quotes to use single quote or double quate in our String.
Thi sharacter™
"This i” Character”
We can embed one string in another string
“This “Python class very helpful” for java students"
Slicing of Strings:
1)
2)
3)
4)
5)
6)
slice means a piece
[] operator is called slice operator, which can be used to retrieve parts of String.
In Python Strings follows zero based index.
‘The index can be either 4ve or -ve.
+ve index means forward direction from Left to Right
~ve index means backward direction from Right to Left
5 3 4
LeTeT:fela]
oO 1 2 3 4
1) >>>s="durga™
2) >>>s{0]
3) “a
4) >>>s{l]
5)‘
6) >>>s[-t]
7) ‘at
18) >>> laa]
17) https://www.youtube.com/durgasoftware@ python
IndexError: string index out of range
1) >>>s{1:40]
2) ‘urea
3) >>>s[1:]
la)
5)
6)
7)
8)
9)
10)
Li)po>s*3
12) 'durgadurgadurga'
13)
114) >>> len(s)
15)5
Note:
1) In Python the following data types are considered as Fundamental Data types
int
complex
boo!
oot
© float
2) In Python, we can represent char values also by using str type and explici
is not available.
1) 23> e'a!
2) >>> typele)
3)
3) long Data Type is available in Python2 but not in Python3. In Python3 long values also
‘we can represent by using int type only.
4) In Python we-can present char Value also by using str Type and explicitly char Type is
not available.
18) https://www.youtube.com/durgasoftware@ python
TYPE CASTING
& We can convert one type value to another type. This conversion is called Typecasting
or Type coersion.
© The following are various inbuilt functions for type casting.
2) int()
2) float()
3) complex()
4) beol()
5) str()
@ inth:
We can use this function to convert values from other types to int
|2) >>> int(123,987}
2) 123
3) >>> int(10+5j)
4) Typeérror: can’t convert complex ta int
5) >>> int(True)
eB) 2
7) >>> int(False)
8) 0
9) >>> int("10")
10)10
12) >>> int("20.5")
12) Value€rror: invalid literat for int() with base 10: ‘10.5"
13) >>> int("ten")
18) ValueErrar: invalid literal for int() with base 10: ‘ten’
15) >>> int("0B1111")
16) ValueError: invalid literal for int() with base 10: 0B1121"
Note:
1) We can convert from any type to int except complex type.
2) if we want to convert str type to int type, compulsary str should contain only integral
value and should be specified in base-10.
19) https://www.youtube.com/durgasoftware@ python
® floatl):
We can use float() function to convert other type values to float type.
2) >>> float{10)
2) 10.0
3) >>> float(10+5))
4) TypeError: can't convert complex ta float
5) >>> float(True)
6) 1.0
7) >>> float(False)
8) 0.0
9) >>> float("10")
10) 10.0
11) >>> float("10.5")
12) 10.5
13) >>> float("ten")
14) ValueError: could not convert string to float: ‘ten’
15) >>> float("0B1111")
16) ValueError: could not convert string to float: ‘OBL114'
Note:
1) We-can convert any type value to float type except complex type.
2) Whenever we are try to convert str type ta float type compulsary str should be
either integral or floating point literal and shauld be specified only in base-10,
G complex()
We can use complex() function to convert other types to complex type.
Form-1: complex(x)
We can use this function to convert x into complex number with real part x and imaginary
part.
a)
2)
3)
4)
5) camplext"10"
6) complex{"10.5"
7) complex("ten")
8) ValueError: complex() arg [sa malformed string
20) https://www.youtube.com/durgasoftware@ python
Form-2: complex(x,y)
We can use this method to convert x and y into complex number such that x will be real
part and y will be imaginary part.
Eg: complex(10, -2) > 10-2)
complex(True, False) > 1+0j
® bool()
We cain use this function to convert other type values to bool type.
1) bool{0) > False
2) bool() > True
3) bool(10) > True
4) bool(10.5) > True
5) bool(0.178) > True
6) bool{0.0) > False
7) bool(10-2)) > True
8) bool{O+1.5)) > True
9) bool{0+0}) > False
10) bool{"True") > True
11) bool("False") > True
112) bool{"") > False
(int Gatetyee
4. O means False
2. Non-zero mean: True
[Kis float datatype
1, If total number value is zero then the recult ic Faleo
otherwise the result is True
1X is Complex datatype
1. If both real and imaginary parts are zero .i.e 0+0)
then the result is False otherwise the result Is True
Tig str dataryp
ait pty string then the result is False otherwise
the result is True
21 | https://www.youtube.com/durgasoftware@ python
® seri):
We can use this method to convert other type values to str type.
2) >>> str(10)
2) ‘10°
3) >>> str(10.5)
4) ‘105°
5) >>> str(1045))
6} '(2045))'
7) >>> str(True)
8) ‘True’
Fundamental Data Types vs Immutability
& 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-chageable behaviour is called immutability.
In Python if a new object is required, then PVM won't create abject immediately. First
it will check is 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 creates an object we are not allowed to change
content. If we are trying to change with those changes a new object will be created.
1) >>>a=10
2) >>> bet
3) spraisb
4) True
5) >>> ida)
6) 1572353952
7) >>>id{(b)
8) 1572353952
8) >>>
22) https://www.youtube.com/durgasoftwarepee asl0 >>> a=2045] >>> aTrue lurea’
pep b=10 32> b=10+5) pee beTrue >>> be'durga’
>>> dla) >raish pepaisb poraish
1572353952 False True True
>>> id{b) >>> ida) >>> dla) >>> id(a)
1572353952 15980256 1572172624 16378848
sso aisb 35> id(b) 29> id{b) >>> id(b)
True 15979944 1572172624, 16378848
6) bytes Data Type:
bytes data type represens a group of byte numbers just like an array.
1) x= [10,20,30,40]
2) b= bytes(x)
3) type(b) > bytes
a) prine(b[a}) > 10
5) print(bf-1)) > 40
6) >>> for lin b: print)
7)
8)
9)
10)
1)
Conclusion 1:
The only allowed values for byte data type are 0 to 256. By mistake if we are trying to
provide any other values then we will get value error,
Conclusion 2:
Once we creates bytes data type value, we cannot change its values,otherwise we will get
TypeError.
23) https://www.youtube.com/durgasoftware@ python
>>> x=[10,20,30,40]
>>> bebytes(x)
>>> b[0]=
Type€rror: ‘bytes’ abject does not support item assignment
bytearray Data Type:
bytearray is exactly same as bytes data type except that its elements can be
x=[10,20,30,40]
ytearray(x)
fori in b: print(i}
10
20
30
40
b[0}=100
fot iin
11)20
12)30
13)40
fez
1) p>>x=[10,256]
2) >>> b= bytearray{x)
3) Value€tror: byte must be in range(0, 256)
If we want to represent a group of values asa single entity where insertion order
required to preserve and duplicates are allowed then we should go for list data
type.
1) Insertion Order is preserved
2) Heterogeneous Objects are allowed
3) Duplicates are allowed
4) Growable in nature
5) Values should be enclosed within square brackets.
24) https://www.youtube.com/durgasoftware@ python
list=[10,10.5,'durga’ ,True,10]
print(list) # [10,10.5, durga',True, 10]
41) Hist=(10,20,30,40}
2) >>> list[0]
3) 10
4) >>>list[-1]
5) 40
6) >>>list[1:3]
7) (20, 30]
8) >>> list[0]=100
'9) >>> foriin list:print(i)
10).
11) 100
12)20
13) 30
1a)4o
list is growable in nature. i.e based on our requirement we can increase or decrease the
size.
1) >>> list=[10,20,30]
2) >>> list.append("durga")
3) >> list
4) [10, 20, 30, “durga’]
5) >>> list.remove(20)
6) >>>list
7) [10, 30, ‘durga’)
8) >>> list2clist"2
9) >>> ist2
10) [10, 30, ‘durga’, 10, 30, "durga']
Note: An ordered, mutable, heterogenous collection of eleemnts is nothing but list,
where duplicates also allowed.
25) https://www.youtube.com/durgasoftware@ python
Tuple Data Type:
tuple data type is exactly same as list data type except that it is immutable.i.e we
cannot chage values.
‘Tuple elements can be represented within parenthesis.
t=(10,20,30,40)
type(t)
t{0}=100
‘Type€rror: ‘tuple’ object does not support item assignment
>>> Lappend(“durga")
AttributeError: ‘tuple’ object has no attribute ‘append’
>>> tremove(10)
AttributeError: ‘tuple’ object has no attribute ‘remove!
Note: tuple is the read only version of list
10) Range Data Type:
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 tad
Eg:
1 = range(10)
for iin r: print{i) > 00 9
Form-2: range(10, 20)
generate numbers from 10 to 19
Eg:
r= range(10,20)
for fin print{i) 240 to 19
26) https://www.youtube.com/durgasoftware@ python
Form-3: range(10, 20, 2)
2 means increment value
Eg:
r= range(10,20,2)
for iin r: print(i) > 10,12,24,16,18
We can access elements present in the range Data Type by using index.
Eg
= range(10,20)
{0} > 10
#[15] > Indexérror: range object index out of range
We cannot modify the values of range data type
Eg
{0} = 100
TypeError: ‘range’ object does nat support item assignment
We can create a list of values with range data type
Ee:
A) >>> 1 =list{range(10))
2) >>>l
3) [0,1,2,3,4,5, 6,7, 8,9]
11) set Data Type:
@ If we want to representa group of values without duplicates where order isnot
important then we should go for set Data Type.
2) Insertion order is not preserved
2) Duplicates are not allowed
3) Heterogeneous objects are allowed
4) Index concept is not applicable
5) Itis mutable collection
6} Growable in nature
27) https://www.youtube.com/durgasoftware|1) s=€100,0,10,200,10,'durga'}
2) s#{0, 100, 'durga’, 200, 10}
|3) s[0] > TypeError: ‘set’ object does not support indexing
® set is growable in nature, based on our requirement we can increase or decrease the
size.
1) >>> s.add(60)
2) >9>5
3) {0, 100, ‘durga’, 200, 10, 60)
4) >>> s.remove(100)
5) >>>5
(8) {0, ‘durga’, 200, 10, 60)
12) frozenset Data Type:
® Itis exactly same as set except that itis immutable,
® Hence we cannot use add or remove functions.
1) >>>s=(10,20,30,40}
2) >>> fs-frozensett(s)
'3) >>> typetfs)
4)
5) >o>fs
6) frozenset({40, 10, 20, 30})
7) >> for iin fs:print{i)
B) aw
9) 40
10)10
11}20
12)30
13)
14) >>> fs.add(70)
115) AttributeError: 'frozenset' object has no attribute ‘add’
16) >>> fs.remove(10)
17) AttributeError: 'frozenset’ object has no attribute ‘remave
28) https://www.youtube.com/durgasoftware@ python
13) dict Data Type:
If we want to represent a group of values as key-value pairs then we should go for
dict data type.
@ Eg: d = {101;'durga',102;'ravi',103:'shiva'}
® Duplicate keys are not allowed but values can be duplicated. If we are trying to
insert an entry with duplicate key then old value will be replaced with new value.
>>>d={101:'durga',102:'ravi',103:'shiva’}
>>> d[101]='sunny’
ood
We can create empty dictionary as follows
d={}
We can add key-value pairs as follows
df'a'J-'apple’
10} df'b']="banana®
11) print(d)
Note: dict is mutable and the order won't be preserved.
Note:
4)
2)
3)
In general we can use bytes and bytearray data types to represent binary information
like images, video files ete
In Python2 long data type is available. But in Python3 itis not available and we can
represent long values also by using int type only.
In Python there is na char data type. Hence we can represent char values also by using
str type.
25) https://www.youtube.com/durgasoftware