1.python Language Fundamentals PDF
1.python Language Fundamentals PDF
1.python Language Fundamentals PDF
Introduction
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.
Java:
C:
1) #include<stdio.h>
2) void main()
3) {
4) print("Hello world");
5) }
Python:
print("Hello World")
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
1 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Eg2: To print the sum of 2 numbers
Java:
C:
1) #include <stdio.h>
2)
3) void main()
4) {
5) int a,b;
6) a =10;
7) b=20;
8) printf("The Sum:%d",(a+b));
9) }
Python:
1) a=10
2) b=20
3) print("The Sum:",(a+b))
Guido developed Python language by taking almost all programming features from
different languages
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
2 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
4. Modular Programming Features from Modula-3
We can use everywhere. The most common important application areas are
Note:
Features of Python:
1. 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.
Its source code is open,so that we can we can customize based on our requirement.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
3 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
3. High Level Programming language:
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 etc..
4. Platform Independent:
Once we write a Python program,it can run on any platform without rewriting once 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.
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 Python is considered
as dynamically typed language.
But Java, C 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.
Python language supports both Procedure 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:
We are 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:
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
4 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
1. We can use already existing legacy non-Python code
2. We can improve performance of the application
10. Embedded:
etc...
Limitations of Python:
1. Performance wise not up to the mark b'z it is interpreted language.
2. Not using for mobile Applications
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:
It is for Java Applications. It can run on JVM
3. IronPython:
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. AnacondaPython
It is specially designed for handling large volume of data processing.
...
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
5 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Python Versions:
Current versions
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
6 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Identifiers
A name in Python program is called identifier.
It can be class name or function name or module name or variable name.
a = 10
By mistake if we are using any other symbol like $ then we will get syntax error.
cash = 10 √
ca$h =20
123total
total123 √
3. Identifiers are case sensitive. Of course Python language is case sensitive language.
total=10
TOTAL=999
print(total) #10
print(TOTAL) #999
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
7 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Identifier:
1. Alphabet Symbols (Either Upper case OR Lower case)
6. There is no length limit for Python identifiers. But not recommended to use too lengthy
identifiers.
1) 123total
2) total123 √
3) java2share √
4) ca$h
5) _abc_abc_ √
6) def
7) if
Note:
Eg: __add__
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
8 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Reserved Words
In Python some words are reserved to represent some meaning or functionality. Such type
of words are called Reserved words.
True,False,None
and, or ,not,is
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
a=True √
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
9 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Data Types
Data Type represent 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.
10
a = 10 a
a = 20
20
a
a = 10
b = 10 10
b
1.type()
to check the type of variable
2. id()
to get address of object
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
10 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
3. print()
to print the value
We can use int data type to represent whole numbers (integral values)
Eg:
a=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.
1. Decimal form
2. Binary form
3. Octal form
4. Hexa decimal form
1. Decimal form(base-10):
2. Binary form(Base-2):
Eg: a = 0B1111
a =0B123
a=b111
3. Octal Form(Base-8):
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
11 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Eg: a=0o123
a=0o786
The allowed digits are : 0 to 9, a-f (both lower and upper cases are allowed)
Literal value should be prefixed with 0x or 0X
Eg:
a =0XFACE
a=0XBeef
a =0XBeer
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=0o10
c=0X10
d=0B10
print(a)10
print(b)8
print(c)16
print(d)2
Base Conversions
Python provide the following in-built functions for base conversions
1.bin():
Eg:
1) >>> bin(15)
2) '0b1111'
3) >>> bin(0o11)
4) '0b1001'
5) >>> bin(0X10)
6) '0b10000'
2. oct():
1) >>> oct(10)
2) '0o12'
3) >>> oct(0B1111)
4) '0o17'
5) >>> oct(0X123)
6) '0o443'
3. hex():
Eg:
1) >>> hex(100)
2) '0x64'
3) >>> hex(0B111111)
4) '0x3f'
5) >>> hex(0o12345)
6) '0x14e5'
Eg: f=1.234
type(f) float
We can also represent floating point values by using exponential form (scientific notation)
Eg: f=1.2e3
print(f) 1200.0
instead of 'e' we can use 'E'
The main advantage of exponential form is we can represent big values in less memory.
***Note:
We can represent int values in decimal, binary, octal and hexa decimal forms. But we can
represent float values only by using decimal form.
Eg:
1) >>> f=0B11.01
2) File "<stdin>", line 1
3) f=0B11.01
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
13 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
4) ^
5) SyntaxError: invalid syntax
6)
7) >>> f=0o123.456
8) SyntaxError: invalid syntax
9)
10) >>> f=0X123.456
11) SyntaxError: invalid syntax
j2 = -1
a + bj
j=
Real Part Imaginary Part
Eg:
3+5j
10+5.5j
0.5+0.1j
In the real part if we use int value then we can specify that either by decimal,octal,binary
or hexa decimal form.
But imaginary part should be specified only by using decimal form.
1) >>> a=0B11+5j
2) >>> a
3) (3+5j)
4) >>> a=3+0B11j
5) SyntaxError: invalid syntax
1) >>> a=10+1.5j
2) >>> b=20+2.5j
3) >>> c=a+b
4) >>> print(c)
5) (30+4j)
6) >>> type(c)
7) <class 'complex'>
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
14 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Note: Complex data type has some inbuilt attributes to retrieve the real part and
imaginary part
c=10.5+3.6j
c.real==>10.5
c.imag==>3.6
We can use complex type generally in scientific Applications and electrical engineering
Applications.
b=True
type(b) =>bool
Eg:
a=10
b=20
c=a<b
print(c)==>True
True+True==>2
True-False==>1
str type:
str represents String data type.
s1='durga'
s1="durga"
By using single quotes or double quotes we cannot represent multi line string literals.
s1="durga
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
15 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
soft"
For this requirement we should go for triple single quotes(''') or triple double quotes(""")
s1='''durga
soft'''
s1="""durga
soft"""
We can also use triple quotes to use single quote or double quote in our String.
Slicing of Strings:
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 +ve or -ve.
+ve index means forward direction from Left to Right
-ve index means backward direction from Right to Left
-5 -4 -3 -2 -1
d u r g a
0 1 2 3 4
1) >>> s="durga"
2) >>> s[0]
3) 'd'
4) >>> s[1]
5) 'u'
6) >>> s[-1]
7) 'a'
8) >>> s[40]
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
16 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
1) >>> s[1:40]
2) 'urga'
3) >>> s[1:]
4) 'urga'
5) >>> s[:4]
6) 'durg'
7) >>> s[:]
8) 'durga'
9) >>>
10)
11)
12) >>> s*3
13) 'durgadurgadurga'
14)
15) >>> len(s)
16) 5
Note:
1. In Python the following data types are considered as Fundamental Data types
int
float
complex
bool
str
2. In Python,we can represent char values also by using str type and explicitly char type is
not available.
Eg:
1) >>> c='a'
2) >>> type(c)
3) <class 'str'>
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.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
17 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
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.
1. int()
2. float()
3. complex()
4. bool()
5. str()
1.int():
We can use this function to convert values from other types to int
Eg:
1) >>> int(123.987)
2) 123
3) >>> int(10+5j)
4) TypeError: can't convert complex to int
5) >>> int(True)
6) 1
7) >>> int(False)
8) 0
9) >>> int("10")
10) 10
11) >>> int("10.5")
12) ValueError: invalid literal for int() with base 10: '10.5'
13) >>> int("ten")
14) ValueError: invalid literal for int() with base 10: 'ten'
15) >>> int("0B1111")
16) ValueError: invalid literal for int() with base 10: '0B1111'
Note:
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
18 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
2. float():
We can use float() function to convert other type values to float type.
1) >>> float(10)
2) 10.0
3) >>> float(10+5j)
4) TypeError: can't convert complex to 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: '0B1111'
Note:
1. We can convert any type value to float type except complex type.
2. Whenever we are trying to convert str type to float type compulsary str should be
either integral or floating point literal and should be specified only in base-10.
3.complex():
Form-1: complex(x)
We can use this function to convert x into complex number with real part x and imaginary
part 0.
Eg:
1) complex(10)==>10+0j
2) complex(10.5)===>10.5+0j
3) complex(True)==>1+0j
4) complex(False)==>0j
5) complex("10")==>10+0j
6) complex("10.5")==>10.5+0j
7) complex("ten")
8) ValueError: complex() arg is a malformed string
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
19 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
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-2j
complex(True,False)==>1+0j
4. bool():
We can use this function to convert other type values to bool type.
Eg:
1) bool(0)==>False
2) bool(1)==>True
3) bool(10)===>True
4) bool(10.5)===>True
5) bool(0.178)==>True
6) bool(0.0)==>False
7) bool(10-2j)==>True
8) bool(0+1.5j)==>True
9) bool(0+0j)==>False
10) bool("True")==>True
11) bool("False")==>True
12) bool("")==>False
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
20 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
5. str():
We can use this method to convert other type values to str type
Eg:
1) >>> str(10)
2) '10'
3) >>> str(10.5)
4) '10.5'
5) >>> str(10+5j)
6) '(10+5j)'
7) >>> str(True)
8) 'True'
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
21 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
In Python if a new object is required, then PVM wont create object 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.
Eg:
1) >>> a=10
2) >>> b=10
3) >>> a is b
4) True
5) >>> id(a)
6) 1572353952
7) >>> id(b)
8) 1572353952
9) >>>
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
22 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
bytes Data Type:
bytes data type represens a group of byte numbers just like an array.
Eg:
1) x = [10,20,30,40]
2) b = bytes(x)
3) type(b)==>bytes
4) print(b[0])==> 10
5) print(b[-1])==> 40
6) >>> for i in b : print(i)
7)
8) 10
9) 20
10) 30
11) 40
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.
Eg:
1) >>> x=[10,20,30,40]
2) >>> b=bytes(x)
3) >>> b[0]=100
4) TypeError: 'bytes' object does not support item assignment
Eg 1:
1) x=[10,20,30,40]
2) b = bytearray(x)
3) for i in b : print(i)
4) 10
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
23 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
5) 20
6) 30
7) 40
8) b[0]=100
9) for i in b: print(i)
10) 100
11) 20
12) 30
13) 40
Eg 2:
1) >>> x =[10,256]
2) >>> b = bytearray(x)
3) ValueError: byte must be in range(0, 256)
Eg:
1) list=[10,10.5,'durga',True,10]
2) print(list) # [10,10.5,'durga',True,10]
Eg:
1) list=[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) >>> for i in list:print(i)
10) ...
11) 100
12) 20
13) 30
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
24 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
14) 40
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) >>> list2=list*2
9) >>> list2
10) [10, 30, 'durga', 10, 30, 'durga']
Note: An ordered, mutable, heterogenous collection of eleemnts is nothing but list, where
duplicates also allowed.
Eg:
1) t=(10,20,30,40)
2) type(t)
3) <class 'tuple'>
4) t[0]=100
5) TypeError: 'tuple' object does not support item assignment
6) >>> t.append("durga")
7) AttributeError: 'tuple' object has no attribute 'append'
8) >>> t.remove(10)
9) AttributeError: 'tuple' object has no attribute 'remove'
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
25 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Form-1: range(10)
generate numbers from 0 to 9
Eg:
r=range(10)
for i in r : print(i) 0 to 9
Form-2: range(10,20)
r = range(10,20)
for i in r : print(i) 10 to 19
Form-3: range(10,20,2)
r = range(10,20,2)
for i in r : print(i) 10,12,14,16,18
We can access elements present in the range Data Type by using index.
r=range(10,20)
r[0]==>10
r[15]==>IndexError: range object index out of range
Eg:
r[0]=100
TypeError: 'range' object does not support item assignment
Eg:
1) >>> l = list(range(10))
2) >>> l
3) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
26 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
1. insertion order is not preserved
2. duplicates are not allowed
3. heterogeneous objects are allowed
4. index concept is not applicable
5. It is mutable collection
6. Growable in nature
Eg:
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
4)
5) set is growable in nature, based on our requirement we can increase or decrease the size.
6)
7) >>> s.add(60)
8) >>> s
9) {0, 100, 'durga', 200, 10, 60}
10) >>> s.remove(100)
11) >>> s
12) {0, 'durga', 200, 10, 60}
1) >>> s={10,20,30,40}
2) >>> fs=frozenset(s)
3) >>> type(fs)
4) <class 'frozenset'>
5) >>> fs
6) frozenset({40, 10, 20, 30})
7) >>> for i in fs:print(i)
8) ...
9) 40
10) 10
11) 20
12) 30
13)
14) >>> fs.add(70)
15) AttributeError: 'frozenset' object has no attribute 'add'
16) >>> fs.remove(10)
17) AttributeError: 'frozenset' object has no attribute 'remove'
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
27 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
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.
Eg:
1. >>> d={101:'durga',102:'ravi',103:'shiva'}
2. >>> d[101]='sunny'
3. >>> d
4. {101: 'sunny', 102: 'ravi', 103: 'shiva'}
5.
6. We can create empty dictionary as follows
7. d={ }
8. We can add key-value pairs as follows
9. d['a']='apple'
10. d['b']='banana'
11. print(d)
Note:
1. In general we can use bytes and bytearray data types to represent binary information
like images,video files etc
2. In Python2 long data type is available. But in Python3 it is not available and we can
represent long values also by using int type only.
3. In Python there is no char data type. Hence we can represent char values also by using
str type.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
28 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Summary of Datatypes in Python3
Datatype Description Is Immutable Example
Int We can use to represent the Immutable >>> a=10
whole/integral numbers >>> type(a)
<class 'int'>
Float We can use to represent the Immutable >>> b=10.5
decimal/floating point >>> type(b)
numbers <class 'float'>
Complex We can use to represent the Immutable >>> c=10+5j
complex numbers >>> type(c)
<class 'complex'>
>>> c.real
10.0
>>> c.imag
5.0
Bool We can use to represent the Immutable >>> flag=True
logical values(Only allowed >>> flag=False
values are True and False) >>> type(flag)
<class 'bool'>
Str To represent sequence of Immutable >>> s='durga'
Characters >>> type(s)
<class 'str'>
>>> s="durga"
>>> s='''Durga Software Solutions
... Ameerpet'''
>>> type(s)
<class 'str'>
bytes To represent a sequence of Immutable >>> list=[1,2,3,4]
byte values from 0-255 >>> b=bytes(list)
>>> type(b)
<class 'bytes'>
bytearray To represent a sequence of Mutable >>> list=[10,20,30]
byte values from 0-255 >>> ba=bytearray(list)
>>> type(ba)
<class 'bytearray'>
range To represent a range of Immutable >>> r=range(10)
values >>> r1=range(0,10)
>>> r2=range(0,10,2)
list To represent an ordered Mutable >>> l=[10,11,12,13,14,15]
collection of objects >>> type(l)
<class 'list'>
tuple To represent an ordered Immutable >>> t=(1,2,3,4,5)
collections of objects >>> type(t)
<class 'tuple'>
set To represent an unordered Mutable >>> s={1,2,3,4,5,6}
collection of unique objects >>> type(s)
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
29 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
<class 'set'>
frozenset To represent an unordered Immutable >>> s={11,2,3,'Durga',100,'Ramu'}
collection of unique objects >>> fs=frozenset(s)
>>> type(fs)
<class 'frozenset'>
dict To represent a group of key Mutable >>>
value pairs d={101:'durga',102:'ramu',103:'hari'}
>>> type(d)
<class 'dict'>
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
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
30 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
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
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
31 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com