0% found this document useful (0 votes)
16 views

Python Week3 Notes

Python CSE

Uploaded by

KDP King
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Python Week3 Notes

Python CSE

Uploaded by

KDP King
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Loyola Institute of Technology

and Management

Python Training Program

Dr.Vajrala Venkata Reddy


Training & Placement Officer

PYTHON TRAINING MANUAL, WEEK-3 1


II. Sequence Category Data Types
 The Purpose of Sequence Category Data Types is "To Store Sequence of Values".
 We have 4 Data types of Sequence Category. They are

1. str
2. bytes
3. bytearray
4. range

1. str
===========================================
Index
----------------
 What is str
 Definition of str
 Notations of str
 Types of strs
 Syntax for storing str data
 Memory Management of str data
+Ve Indexing
-Ve Indexing
 Operations on str data
Indexing
Slicing Operation
 Programming Examples
=====================================================================
Properties
=====================================================================
 'str' is one of the pre-defined class and treated as Sequence Data Type.
 The purpose of str data type is that "To store String data or text data or Alphanumeric data
PYTHON TRAINING MANUAL, WEEK-3 2
or numeric data or special symbols within double Quotes or single quotes or tipple double
quotes and triple single quotes. "
 str is a collection of Characters or Alphanumeric data or numeric data or any type of data
enclosed within double Quotes or single quotes or tripple double quotes and tripple single
quotes. "
----------------------------
Types of Str data
-----------------------------
 In Python Programming, we have two types of Str Data. They are
1. Single Line String Data
2. Multi Line String Data
-----------------------------------------
1. Single Line String Data:
-----------------------------------------
Syntax1:- varname=" Single Line String Data "
(OR)
Syntax2:- varname=' Single Line String Data '
 With the help double Quotes (" ") and single Quotes (' ') we can store single line str data
only but not possible to store multi line string data.
--------------------------------------------------------------------------------------------------------------------
2. Multi Line String Data:
-----------------------------------------
Syntax1:- varname=" " " String Data1
String Data2
------------------
String data-n " " "

(OR)

Syntax2:- varname=' ' ' String Data1


String Data2
PYTHON TRAINING MANUAL, WEEK-3 3
------------------
String data-n ' ' '

 With the help triple double Quotes (" " " " " ") and Tripple single Quotes (' ' ' ' ' ') we
can store single line str data and multi-line string data.
--------------------------------------------------------------------------------------------------------------------
Examples:
--------------------------------------------------------------------------------------------------------------------
>>> s1="Python"
>>> print(s1,type(s1))---------------------------Python <class 'str'>
>>> s2='Python'
>>> print(s2,type(s2))--------------------------Python <class 'str'>
>>> s3="A"
>>> print(s3,type(s3))--------------------------A <class 'str'>
>>> s4='A'
>>> print(s4,type(s4))-------------------------A <class 'str'>
>>> s1="123456"
>>> print(s1,type(s1))-------------------------123456 <class 'str'>
>>> s2="Python3.11"
>>> print(s2,type(s2))------------------------Python3.11 <class 'str'>
>>> s3="123$456_abc"
>>> print(s3,type(s3))------------------------123$456_abc <class 'str'>
>>> s4="@#$%^&8912"
>>> print(s4,type(s4))-------------------------@#$%^&8912 <class 'str'>
>>> s1="Python Programming"
>>> print(s1,type(s1))-----------------------Python Programming <class 'str'>
------------------------------------------
>>> addr1="Guido Van Rossum
------------------- Syntax Error: unterminated string literal (detected at line 1)
>>> addr1='Guido Van Rossum
--------------------- Syntax Error: unterminated string literal (detected at line 1)
PYTHON TRAINING MANUAL, WEEK-3 4
---------------------------------------------------------------------
>>> addr1=" " "Guido Van Rossum
... FNO:3-4, Hill Side
... Python Software Foundation
... Nether Lands-56 " " "
>>> print(addr1,type(addr1))
Guido Van Rossum
FNO:3-4, Hill Side
Python Software Foundation
Nether Lands-56 <class 'str'>
-------------------------------------------------------------------------------------------
>>> addr2= ' ' ' Travis Oliphant
... HNO:12-34, Sea Side
... Numpy Organization
... Nether lands-58 ' ' '
>>> print(addr2,type(addr2))
Travis Oliphant
HNO:12-34, Sea Side
Numpy Organization
Nether lands-58 <class 'str'>
-----------------------------------------------------------------
>>> s1="""Python Programming"""
>>> print(s1,type(s1))------------Python Programming <class 'str'>
>>> s1='''Python Programming'''
>>> print(s1,type(s1))-------------------Python Programming <class 'str'>
>>> s2="""A"""
>>> print(s2,type(s2))------------------A <class 'str'>
>>> s2='''A'''
>>> print(s2,type(s2))---------------A <class 'str'>
=====================================================================

PYTHON TRAINING MANUAL, WEEK-3 5


Memory Management of String

Operations on str data


=============================================
 On str data, we can perform Two Types of Operations. They are

1. Indexing
2. Slicing
--------------------------------------------------------------------------------------------------------------------
1. Indexing
--------------------------------------------------------------------------------------------------------------------
 The Process of Obtaining Single Value from given str object by passing Valid Index is
called Indexing.
Syntax: strobj[ Index ]
 Here strobj is an object of <class, 'str'>
 Here Index Represents Either +ve Index or -Ve Index
 If we enter Valid Index, then we get Corresponding Value of that Index from str obj
 If we enter Invalid Index, then we get Index Error
----------------------------------------

PYTHON TRAINING MANUAL, WEEK-3 6


Examples
----------------------------------------
>>> s="PYTHON"
>>> print(s[0])----------------------------P
>>> print(s[1])----------------------------Y
>>> print(s[3])----------------------------H
>>> print(s[4])----------------------------O
>>> print(s[-1])---------------------------N
>>> print(s[-6])---------------------------P
>>> print(s[-2])---------------------------O
>>> print(s[-4])---------------------------T
>>> print(s[10])-------------------------Index Error: string index out of range
>>> print(s[-10])-----------------------Index Error: string index out of range
-----------------------------------------------------
>>> s="PYTHON"
>>> s[0]------------------------------'P'
>>> len(s)-1 ------------------------5
>>> s[len(s)-1]---------------------'N'
>>> s[-1]-----------------------------'N'
>>> s[-len(s)]-----------------------'P'
>>> s[len(s)]------------------------Index Error: string index out of range
--------------------------------------------------
>>> "HYDERABAB"[-1]---------------'B'
>>> "HYDERABAB"[0]----------------'H'
>>> "HYDERABAB"[len("HYDERABAD")-1]--------'B'
>>> "HYDERABAB"[-len("HYDERABAD")]--------'H'
>>> "HYDERABAB"[len("HYDERABAD")]---------Index Error: string index out of range
---------------------------------------------------------
>>> "JAVA PROG"[False]----------------'J'
>>> "JAVA PROG"[True]-----------------'A'
>>> "JAVA PROG"[0b100]-------------' '
PYTHON TRAINING MANUAL, WEEK-3 7
>>> "JAVA PROG"[0b100+1]----------'P'
>>> "JAVA PROG"[-1]--------------------'G'
>>> "JAVA PROG"[3]--------------------'A'
>>> "JAVA PROG"[0xF]----------------Index Error: string index out of range
--------------------------------------------------------------------------------------------------------------------
2. Slicing
-------------------------------------------------------------------------------------------------------------------
 The Process of Obtaining Range of Values OR Sub String either Forward Direction or in
Backward Direction from Main String is Called Slicing.
 To Perform Slicing Operations, we have 5 Syntaxes. They are
*****************************************************************************
Syntax-1: strobj[Begin:End]
***************************************************************************
 This Syntax generates range of Chars/Values from BEGIN to END-1 Index provided
BEGIN<END Otherwise we get No Result OR Space as a Result.
----------------------
Examples 1
----------------------
>>> s="PYTHON"
>>> print(s,type(s))--------------------PYTHON <class 'str'>
>>> s[0:4]--------------------------------'PYTH'
>>> print(s[0:4])------------------------PYTH
>>> s[4:0]--------------------------------' ' due to 4<0 False
>>> print(s[4:0])----------------------- NO Result due to 4<0 False
>>> s[0:6]-------------------------------'PYTHON'
>>> s[2:6]------------------------------'THON'
>>> s[2:4]------------------------------'TH'
>>> s[1:5]------------------------------'YTHO'
>>> s[3:6]------------------------------'HON'
>>> s[4:6]------------------------------'ON'
>>> s[3:5]------------------------------'HO'
PYTHON TRAINING MANUAL, WEEK-3 8
------------------------------
>>> s="PYTHON"
>>> print(s,type(s))--------------PYTHON <class 'str'>
>>> s[-5:-2]------------------------'YTH'
>>> s[-2:-5]------------------------' '
>>> print(s[-2:-5])---------------No Result
>>> s[-6:-1]-----------------------'PYTHO'
>>> s[-5:-1]------------------------'YTHO'
>>> s[-4:-1]------------------------'THO'
>>> s[-3:-1]------------------------'HO'
>>> s[-6:-3]------------------------'PYT'
>>> s[-6:-2]------------------------'PYTH'
>>> s[-4:-2]------------------------'TH'
>>> s[-5:-4]------------------------'Y'
*******************************************
Special SUB POINTS (Begin<End) Rule Become Silence
******************************************
>>> s="PYTHON"
>>> print(s,type(s))------------PYTHON <class 'str'>
>>> s[0:-4]-----------------------'PY'
>>> s[2:-1]-----------------------'THO'
>>> s[0:-1]-----------------------'PYTHO'
>>> s[4:-1]-----------------------'O'
>>> s[2:-1]------------------------'THO'
>>> s[-6:5]------------------------'PYTHO'
>>> s[-6:6]-------------------------'PYTHON'
>>> s[-5:5]-------------------------'YTHO'
>>> s[-4:5]-------------------------'THO'
>>> s[-6:3]-------------------------'PYT'
>>> s[-4:6]-------------------------'THON'
>>> s[-3:6]-------------------------'HON'
PYTHON TRAINING MANUAL, WEEK-3 9
*******************************************
MOST Special SUB POINTS (Begin<End) Rule Become Silence
******************************************
>>> s="PYTHON"
>>> print(s)---------------------PYTHON
>>> s[0:100]--------------------'PYTHON'
>>> s[2:99]----------------------'THON'
>>> s[3:10]----------------------'HON'
>>> s[2:45]----------------------'THON'
>>> s[45:2]----------------------''
>>> s[-10:-1]--------------------'PYTHO'
>>> s[-1000:-3]-----------------'PYT'
>>> s[-100:-1]-------------------'PYTHO'
>>> s[-10:-4]--------------------'PY'
>>> s[-10:-5]--------------------'P'
>>> s[-10:-6]--------------------''
------------------------------------
>>> s="PYTHON"
>>> print(s)----------------------PYTHON
>>> s[-1000:-1]----------------'PYTHO'
>>> s[-1000:0]----------------''
>>> s[-4:0]---------------------''
>>> s[-5:2]----------------------'Y'
>>> s[-5:1]----------------------''
>>> s[2:-7]---------------------''
>>> s[3:-5]---------------------''
>>> s[3:-2]---------------------'H'
>>> s[-100:100]---------------'PYTHON'
>>> s[0:-99]---------------------''
>>> s[-1:100]-------------------'N'
>>> s[-2:100]-------------------'ON'
PYTHON TRAINING MANUAL, WEEK-3 10
>>> s[1000:2000]--------------''
>>> s[-1000:-2000]-----------''
>>> s[-2000:-1000]----------''
*****************************************************************************
Syntax-2: strobj[Begin: ]
*****************************************************************************
*********************************************************************
 In This Syntax, we Specified BEGIN Index and we didn’t not specify END Index
 If we don't specify END Index, then PVM Takes The characters from BEGIN Index to
strobj[len(strobj)-1].
(OR)
 If we don't specify END Index, then PVM Takes The characters from BEGIN Index to
Last Character Index
---------------------------
Examples
---------------------------
>>> s="PYTHON"
>>> print(s)---------------PYTHON
>>> s[2:]-------------------'THON'
>>> s[3:]-------------------'HON'
>>> s[0:]------------------'PYTHON'
>>> s[4:]------------------'ON'
>>> s[3:]-------------------'HON'
-----------------
>>> s="PYTHON"
>>> print(s)------------------PYTHON
>>> s[-6:]---------------------'PYTHON'
>>> s[-5:]---------------------'YTHON'
>>> s[-3:]---------------------'HON'
>>> s[-4:]---------------------'THON'
>>> s[-2:]---------------------'ON'
PYTHON TRAINING MANUAL, WEEK-3 11
------------------------
>>> s="PYTHON"
>>> print(s)--------------------PYTHON
>>> s[-1000:]-----------------'PYTHON'
>>> s[-10:]--------------------'PYTHON'
>>> s[10:]---------------------''
>>> s[6:]----------------------''
>>> s[5:]---------------------'N'
>>> s[-1:]--------------------'N'
****************************************************************************
Syntax-3: strobj[ : End]
***************************************************************************
 In This Syntax, we Specified END Index, and we didn’t not specify BEGIN Index
 If we don't specify BEGIN Index, then PVM Takes BEGIN Index as 0 or -len(strobj) to
END-1 Index
(OR)
 If we don't specify BEGIN Index, then PVM Takes The characters from First Character to
END-1 Index
-----------------------------
Examples
-----------------------------
>>> s="PYTHON"
>>> s[:5]-------------------------'PYTHO'
>>> s="PYTHON"
>>> s[:4]------------------------'PYTH'
>>> s[:4]------------------------'PYTH'
>>> s[:5]-------------------------'PYTHO'
>>> s[:6]-------------------------'PYTHON'
>>> s[:600]---------------------'PYTHON'
>>> s[:-1]-----------------------'PYTHO'
>>> s[:-3]-----------------------'PYT'
PYTHON TRAINING MANUAL, WEEK-3 12
>>> s[:-5]-----------------------'P'
>>> s[:-2]-----------------------'PYTH'
>>> s[:700]---------------------'PYTHON'
>>> s[:-100]--------------------' '
>>> s[:-450]--------------------' '
*****************************************************************************
Syntax-4: strobj[ : ]
*****************************************************************************
 In this Syntax, we didn’t specify BEGIN and END Index.
 If we don't specify BEGIN and END Indices, then PVM Takes From First Character to
Last Character.
-----------------------
Examples
-----------------------
>>> s="PYTHON"
>>> print(s)----------------------PYTHON
>>> s[:]----------------------------'PYTHON'
>>> s="PYTHON PROGRAMMING"
>>> s[:]---------------------------'PYTHON PROGRAMMING'
>>> "JAVA"[:]-------------------'JAVA'
>>> "JAVA"[0:]------------------'JAVA'
>>> "JAVA"[:100]---------------'JAVA'
>>> "JAVA"[-100:100]---------'JAVA'
>>> "JAVA"[-100:]--------------'JAVA'
>>> "JAVA"[:]--------------------'JAVA'
=>NOTE: In Syntax-1,Syntax-2,Syntax-3 and Syntax-4, we are getting the Data from Str object
in FORWARD DIRECTION by maintaining DEFAULT STEP +1
*****************************************************************************

PYTHON TRAINING MANUAL, WEEK-3 13


Syntax-5: strobj[Begin:End:Step]
***************************************************************************
Rule-1: Here the Values of BEGIN, END and STEP can be either +VE or -VE

Rule-2: If the Value of STEP is +VE then PVM gets the Values / Chars from str obj from
BEGIN to END-1 Index in FORWARD --------- DIRECTION Provided BEGIN < END
otherwise we get Space OR No Result Output as result.

Rule-3: If the Value of STEP is -VE then PVM get the Values / Chars from str obj from BEGIN
to END+1 Index in BACKWARD -------------- DIRECTION provided BEGIN>END otherwise
we get Space OR No Result Output as result.

Rule-4: In FORWARD DIRECTION if we specify END Index as 0 then we never get any result
OR get Space as Result
Rule-5: In BACKWARD DIRECTION if we specify END Index as -1 then we never get any
result OR get Space as Result
--------------------------------------------------------------------------------------------------------------------
Examples--RULE-2
-----------------------------------
>>> s="PYTHON"
>>> s[:]------------------'PYTHON'
>>> s[::]------------------'PYTHON'
>>> s[::2]-----------------'PTO'
>>> s[::3]-----------------'PH'
>>> s[::4]----------------'PO'
>>> s[::5]----------------'PN'
>>> s[::6]-----------------'P'
>>> s[::600]--------------'P'
>>> #-------------------------------
>>> s="PYTHON"
>>> s[0:6:2]------------------'PTO'
PYTHON TRAINING MANUAL, WEEK-3 14
>>> s[2:6:2]------------------'TO'
>>> s[1:5:3]-----------------'YO'
>>> s[0::2]--------------------'PTO'
>>> s[:6:4]--------------------'PO'
>>> "HYDERABAD"[::2]------'HDRBD'
************************************************
Examples--RULE-3
----------------------------------------------------------
>>> s="PYTHON"
>>> print(s)------------------------PYTHON
>>> s[::-1]--------------------------'NOHTYP'
>>> "HYDERABAD"[::-1]----------'DABAREDYH'
>>> "LIRIL"[::-1]---------------------'LIRIL'
>>> "MALAYALAM"[::-1]------------'MALAYALAM'
>>> "MOM"[::-1]----------------------'MOM'
>>> "DAD"[::-1]-----------------------'DAD'
>>> "MADAM"[::-1]-------------------'MADAM'
>>> "RACECAR"[::-1]---------------'RACECAR'
>>> "8558"[::-1]----------------------'8558'
>>> #------------------------------------------------
>>> s="PYTHON"
>>> print(s)---------------------------PYTHON
>>> s[1:6:-1]-------------------------''
>>> s[5:1:-1]-------------------------'NOHT'
>>> s[4:0:-1]------------------------'OHTY'
>>> s[5:0:-2]------------------------'NHY'
>>> s[5:0:-3]-----------------------'NT'
>>> s[4::-2]-------------------------'OTP'
>>> #---------------------------------------------------
>>> s="PYTHON"
>>> print(s)--------------------------PYTHON
PYTHON TRAINING MANUAL, WEEK-3 15
>>> s[-6:-1:-1]-----------------------''
>>> s[-6:-1:]-------------------------'PYTHO'
>>> s[-1:-6:-1]-----------------------'NOHTY'
>>> s[-1:-7:-1]-----------------------'NOHTYP'
>>> s[-1:-7:-2]------------------------'NHY'
>>> s[-2:-6:-3]------------------------'OY'
>>> s[-2:-6:-4]--------------------------'O'
>>> s[-1:-5:-1]--------------------------'NOHT'
>>> #------------------------------------------------
>>> s="PYTHON"
>>> print(s)
PYTHON
>>> s[-1000:-2000:-1]
''
>>> s[2000:1000:-1]
''
>>> s[-1000::-1]
''
>>> s[:-1000:-1]
'NOHTYP'
>>> #-----------------------------------
>>> s="PYTHON"
>>> print(s)
PYTHON
>>> s[-5:5:-1]
''
>>> s[0:1000:-1]
''
>>> s[-500:-600:-1]
''
>>> s[-1:-600:-2]
PYTHON TRAINING MANUAL, WEEK-3 16
'NHY'
***********************************************
>>> s="PYTHON"
>>> s[-2::-2][::-1]
'PTO'
>>> s[::2][::-1]
'OTP'
>>> s[::-1][::-2]
'PTO'
>>> s[::-1]
'NOHTYP'
>>> 'NOHTYP'[::-2]
'PTO'
>>>
>>>
>>> "LIRIL"[0:3][::-1]
'RIL'
>>> "NISSON"[2:][::-1]
'NOSS'
*****************************************
>>> "JAVA"[::True][::-True]------------------'AVAJ'
>>> "JAVA"[::-True][::False]---------------ValueError: In slice Operation, step cannot be zero
>>> "PYTHON"[::0]---------------------------ValueError: slice step cannot be zero
***********************************************************
Examples for RULE-4
-------------------------------------------------------------------
>>> s="PYTHON"
>>> s[:0]
''
>>> s[:0:1]
''
PYTHON TRAINING MANUAL, WEEK-3 17
>>> s[:0:2]
''
>>> s[:0:3]
''
***********************************************************
Examples for RULE-5
-------------------------------------------------------------------
>>> s="PYTHON"
>>> s[:-1:-1]----------------------------''
>>> s[:-1:-2]---------------------------''
>>> s[:-1:-3]--------------------------''
>>> s[:-1:-4]-------------------------''
>>> "HYDERABAD"[:-1:-2]-----''
>>> "HYDERABAD"[:-1:-3]-----''
>>> "HYDERABAD"[:-1:-True]-----''

Type Casting Techniques in Python


============================================================
 The process of Converting One Type of Possible Value into another type of Possible
Value is Called Type Casting.
 Fundamentally, we have 5 Type of Type Casting Techniques in Python. They are

1. int()
2. float()
3. bool()
4. complex()
5. str()

PYTHON TRAINING MANUAL, WEEK-3 18


1. int()
============================================================
 int() is used for converting Any Possible Type of Value into int type value.
 Syntax: varname=int(float / bool / complex / str)
*****************************************************************************
Example1: float type into int type--POSSIBLE
*****************************************************************************
>>> a=12.34
>>> print(a,type(a))---------------------12.34 <class 'float'>
>>> b=int(a)
>>> print(b,type(b))--------------------12 <class 'int'>
>>> a=0.45
>>> print(a,type(a))--------------------0.45 <class 'float'>
>>> b=int(a)
>>> print(b,type(b))-------------------0 <class 'int'>
*****************************************************************************
Example2: bool type into int type--POSSIBLE
*****************************************************************************
>>> a=True
>>> print(a,type(a))----------------------True <class 'bool'>
>>> b=int(a)
>>> print(b,type(b))---------------------1 <class 'int'>
>>> a=False
>>> print(a,type(a))--------------------False <class 'bool'>
>>> b=int(a)
>>> print(b,type(b))-------------------0 <class 'int'>
*****************************************************************************
Example3: complex type into int type----NOT POSSIBLE
*****************************************************************************
>>> a=2+3.5j

PYTHON TRAINING MANUAL, WEEK-3 19


>>> print(a,type(a))-----------------(2+3.5j) <class 'complex'>
>>> b=int(a)-----------TypeError: int() argument must be a string not 'complex'
*****************************************************************************
Example: str type into int type
****************************************************************************
Case-1: str int into int----POSSIBLE
----------------------------------------------------------------------------------------
>>> a="123"
>>> print(a,type(a))--------------123 <class 'str'>
>>> b=int(a)
>>> print(b,type(b))--------------123 <class 'int'>
----------------------------------------------------------------------------------------
Case-2: str float into int---NOT POSSIBLE
----------------------------------------------------------------------------------------
>>> a="12.34"
>>> print(a,type(a))------------------12.34 <class 'str'>
>>> b=int(a)--------------------------ValueError: invalid literal for int() with base 10: '12.34'
----------------------------------------------------------------------------------------
Case-3: str bool into int-----NOT POSSIBLE
----------------------------------------------------------------------------------------
>>> a="True"
>>> print(a,type(a))-----------True <class 'str'>
>>> b=int(a)--------------------ValueError: invalid literal for int() with base 10: 'True'
----------------------------------------------------------------------------------------
Case-4: str complex into int--Not POSSIBLE
----------------------------------------------------------------------------------------
>>> a="2+3j"
>>> print(a,type(a))--------------2+3j <class 'str'>
>>> b=int(a)------------------------ValueError: invalid literal for int() with base 10: '2+3j'
----------------------------------------------------------------------------------------
Case-5: pure str into int--NOT POSSIBLE
PYTHON TRAINING MANUAL, WEEK-3 20
----------------------------------------------------------------------------------------
>>> a="PYTHON"
>>> print(a,type(a))--------------------PYTHON <class 'str'>
>>> b=int(a)-------------------------ValueError: invalid literal for int() with base 10: 'PYTHON'

2. float()
=============================================================
 float() is used for converting Any Possible Type of Value into float type value.
 Syntax: varname=float(int / bool / complex / str)
*****************************************************************************
Example1: int type into float type--POSSIBLE
*****************************************************************************
>>> a=123
>>> print(a,type(a))-----------------123 <class 'int'>
>>> b=float(a)
>>> print(b,type(b))----------------123.0 <class 'float'>
*****************************************************************************
Example2: bool type into float type--POSSIBLE
*****************************************************************************
>>> a=True
>>> print(a,type(a))---------------True <class 'bool'>
>>> b=float(a)
>>> print(b,type(b))---------------1.0 <class 'float'>
>>> a=False
>>> print(a,type(a))---------------False <class 'bool'>
>>> b=float(a)
>>> print(b,type(b))----------------0.0 <class 'float'>
*****************************************************************************
Example3: complex type into float type--NOT POSSIBLE
****************************************************************************

PYTHON TRAINING MANUAL, WEEK-3 21


>>> a=2+3j
>>> print(a,type(a))---------------(2+3j) <class 'complex'>
>>> b=float(a)-----------Type Error: float() argument must be a string or a real number, not
'complex'
*****************************************************************************
Example: str type into float type
*****************************************************************************
Case-1: str int into float--POSSIBLE
----------------------------------------------------------------------------------------
>>> a="1234"
>>> print(a,type(a))-------------------1234 <class 'str'>
>>> b=float(a)
>>> print(b,type(b))-------------------1234.0 <class 'float'>
----------------------------------------------------------------------------------------
Case-2: str float into float---POSSIBLE
----------------------------------------------------------------------------------------
>>> a="12.34"
>>> print(a,type(a))--------------------12.34 <class 'str'>
>>> b=float(a)
>>> print(b,type(b))-----------------12.34 <class 'float'>
---------------------------------
>>> a="198.0.0.1"
>>> print(a,type(a))-------------198.0.0.1 <class 'str'>
>>> b=float(a)------------Value Error: could not convert string to float: '198.0.0.1'
----------------------------------------------------------------------------------------
Case-3: str bool into float----NOT POSSIBLE
----------------------------------------------------------------------------------------
>>> a="True"
>>> print(a,type(a))
True <class 'str'>
>>> b=float(a)----------Value Error: could not convert string to float: 'True'
PYTHON TRAINING MANUAL, WEEK-3 22
----------------------------------------------------------------------------------------
Case-4: str complex into float--NOT POSSIBLE
----------------------------------------------------------------------------------------
>>> a="2+3.6j"
>>> print(a,type(a))------------------2+3.6j <class 'str'>
>>> b=float(a)-------------Value Error: could not convert string to float: '2+3.6j'
----------------------------------------------------------------------------------------
Case-5: pure str into float--NOT POSSIBLE
----------------------------------------------------------------------------------------
>>> a="PYTHON.Java"
>>> print(a,type(a))----------PYTHON.Java <class 'str'>
>>> b=float(a)------------Value Error: could not convert string to float: 'PYTHON.Java'

3. bool()
=============================================================
 bool is used for converting Any Possible Type of Value into bool type value.
 Syntax: varname=bool(int / float / complex / str)
 ALL NON-ZERO VALUES ARE CONSIDERED AS TRUE
 ALL ZERO VALUES ARE CONSIDERED AS FALSE
*****************************************************************************
Example1: int type into bool type---POSSIBLE
*****************************************************************************
>>> a=123
>>> print(a,type(a))----------123 <class 'int'>
>>> b=bool(a)
>>> print(b,type(b))------------True <class 'bool'>
>>> a=0
>>> print(a,type(a))------------0 <class 'int'>
>>> b=bool(a)
>>> print(b,type(b))----------- False <class 'bool'>

PYTHON TRAINING MANUAL, WEEK-3 23


>>> a=-124
>>> print(a,type(a))---------------- -124 <class 'int'>
>>> b=bool(a)
>>> print(b,type(b))------------------ True <class 'bool'>
*****************************************************************************
Example2:float type into bool type--POSSIBLE
*****************************************************************************
>>> a=23.45
>>> print(a,type(a))---------------- 23.45 <class 'float'>
>>> b=bool(a)
>>> print(b,type(b))-----------------True <class 'bool'>
>>> a=0.0
>>> print(a,type(a))--------------- 0.0 <class 'float'>
>>> b=bool(a)
>>> print(b,type(b))--------------- False <class 'bool'>
>>>
>>> a=0.00000000000000000000000000000000000000000000001
>>> print(a,type(a))---------- 1e-47 <class 'float'>
>>> b=bool(a)
>>> print(b,type(b))------------ True <class 'bool'>
*****************************************************************************
Example3: complex type into bool type--POSSIBLE
*****************************************************************************
>>> a=2+3j
>>> print(a,type(a))------------------- (2+3j) <class 'complex'>
>>> b=bool(a)
>>> print(b,type(b))-------------------------- True <class 'bool'>
>>> a=0+0j
>>> print(a,type(a))-------------------- 0j <class 'complex'>
>>> b=bool(a)
>>> print(b,type(b)) ---------------- False <class 'bool'>
PYTHON TRAINING MANUAL, WEEK-3 24
*****************************************************************************
Example: str type into float type
*****************************************************************************
Case-1: str int into bool--POSSIBLE
----------------------------------------------------------------------------------------
>>> a="123"
>>> print(a,type(a))------------------- 123 <class 'str'>
>>> b=bool(a)
>>> print(b,type(b))--------------------- True <class 'bool'>
>>> a="0"
>>> print(a,type(a))---------------------- 0 <class 'str'>
>>> b=bool(a)
>>> print(b,type(b))----------------- True <class 'bool'>
>>> a="10-10"
>>> print(a,type(a))-------------------- 10-10 <class 'str'>
>>> b=bool(a)
>>> print(b,type(b))-------------------- True <class 'bool'>
>>> len(a)-------------------------------- 5
----------------------------------------------------------------------------------------
Case-2: str float into bool--Possible
----------------------------------------------------------------------------------------
>>> a="12.34"
>>> print(a,type(a)) --------------------- 12.34 <class 'str'>
>>> b=bool(a)
>>> print(b,type(b)) --------------------- True <class 'bool'>
>>> a="0.0"
>>> print(a,type(a)) -------------------- 0.0 <class 'str'>
>>> b=bool(a)
>>> print(b,type(b)) -------------------- True <class 'bool'>
---------------------------------------------------------------------------------------
Case-3: str bool into bool--POSSIBLE
PYTHON TRAINING MANUAL, WEEK-3 25
----------------------------------------------------------------------------------------
>>> a="True"
>>> print(a,type(a)) --------------------- True <class 'str'>
>>> b=bool(a)
>>> print(b,type(b)) ------------------------ True <class 'bool'>
>>> a="False"
>>> print(a,type(a)) -------------------------- False <class 'str'>
>>> b=bool(a)
>>> print(b,type(b)) --------------- True <class 'bool'>
----------------------------------------------------------------------------------------
Case-4: str complex into bool--POSSIBLE
----------------------------------------------------------------------------------------
>>> a="2+3j"
>>> print(a,type(a)) -------------------- 2+3j <class 'str'>
>>> b=bool(a)
>>> print(b,type(b)) -------------------- True <class 'bool'>
>>> a="0+0j"
>>> print(a,type(a)) ------------- 0+0j <class 'str'>
>>> b=bool(a)
>>> print(b,type(b)) ------------- True <class 'bool'>
----------------------------------------------------------------------------------------
Case-5: pure str into bool--POSSIBLE
----------------------------------------------------------------------------------------
>>> a="PYTHON"
>>> print(a,type(a)) ------- PYTHON <class 'str'>
>>> b=bool(a)
>>> print(b,type(b)) --- True <class 'bool'>
>>> a="JAVA"
>>> print(a,type(a)) ---------------- JAVA <class 'str'>
>>> b=bool(a)
>>> print(b,type(b)) -------------- True <class 'bool'>
PYTHON TRAINING MANUAL, WEEK-3 26
>>> a=" "
>>> print(a,type(a)) ------------------ <class 'str'>
>>> b=bool(a)
>>> print(b,type(b)) ---------- True <class 'bool'>
>>> len(a) ------------------ 3
>>>
>>> a=""
>>> print(a,type(a)) ---------------- <class 'str'>
>>> b=bool(a)
>>> print(b,type(b)) ------------- False <class 'bool'>
>>> len(a) ---------------- 0

4. complex()
=============================================================
 complex() is used for converting Any Possible Type of Value into complex type value.
 Syntax: varname=complex(int / float / bool / str)
*****************************************************************************
Example1: int type into complex--POSSIBLE
*****************************************************************************
>>> a=10
>>> print(a,type(a))---------------- 10 <class 'int'>
>>> b=complex(a)
>>> print(b,type(b)) ---- (10+0j) <class 'complex'>
*****************************************************************************
Example2: float type into complex type--POSSIBLE
*****************************************************************************
>>> a=1.3
>>> print(a,type(a)) -------------------- 1.3 <class 'float'>
>>> b=complex(a)
>>> print(b,type(b)) ------------------- (1.3+0j) <class 'complex'>

PYTHON TRAINING MANUAL, WEEK-3 27


*****************************************************************************
Example3: bool type into complex type --POSSIBLE
*****************************************************************************
>>> a=True
>>> print(a,type(a)) ----------------- True <class 'bool'>
>>> b=complex(a)
>>> print(b,type(b)) ----------------- (1+0j) <class 'complex'>
*****************************************************************************
Example: str type into complex type
*****************************************************************************
Case-1: str int into complex--POSSIBLE
----------------------------------------------------------------------------------------
>>> a="12"
>>> print(a,type(a)) ------------------- 12 <class 'str'>
>>> b=complex(a)
>>> print(b,type(b))---------------------------- (12+0j) <class 'complex'>
----------------------------------------------------------------------------------------
Case-2: str float into complex---POSSIBLE
----------------------------------------------------------------------------------------
>>> a="4.5"
>>> print(a,type(a)) ----------------- 4.5 <class 'str'>
>>> b=complex(a)
>>> print(b,type(b)) ------------------- (4.5+0j) <class 'complex'>
----------------------------------------------------------------------------------------
Case-3: str bool into compelx--NOT POSSIBLE
----------------------------------------------------------------------------------------
>>> a="True"
>>> print(a,type(a)) -------- True <class 'str'>
>>> b=complex(a)----------ValueError: complex() arg is a malformed string
----------------------------------------------------------------------------------------

PYTHON TRAINING MANUAL, WEEK-3 28


Case-4: str complex into complex---POSSIBLE
----------------------------------------------------------------------------------------
>>> a="2+3j"
>>> print(a,type(a)) ------- 2+3j <class 'str'>
>>> b=complex(a)
>>> print(b,type(b)) -------------- (2+3j) <class 'complex'>
----------------------------------------------------------------------------------------
Case-5: pure str into complex---NOT POSSIBLE
----------------------------------------------------------------------------------------
>>> a="PYTHON"
>>> print(a,type(a)) --- PYTHON <class 'str'>
>>> b=complex(a)------------ValueError: complex() arg is a malformed string

5. str()
str() is used for converting All types of values into str type.
 Syntax: varname=str(int / float / bool / complex )
--------------------------------------------------------------------------------------------------------------------
Examples
--------------------------------------------------------------------------------------------------------------------
>>> a=123
>>> print(a,type(a)) ---------------- 123 <class 'int'>
>>> b=str(a)
>>> print(b,type(b)) -------------- 123 <class 'str'>
>>> b ------------------- '123'
---------------------------------
>>> a=12.34
>>> print(a,type(a)) ------------------- 12.34 <class 'float'>
>>> b=str(a)
>>> print(b,type(b)) ------------------- 12.34 <class 'str'>
>>> b --------------------- '12.34'
-----------------------------------

PYTHON TRAINING MANUAL, WEEK-3 29


>>> a=True
>>> print(a,type(a)) ---------------- True <class 'bool'>
>>> b=str(a)
>>> print(b,type(b)) -------------- True <class 'str'>
>>> b --------------- 'True'
-----------------------------------------
>>> a=2+3.5j
>>> print(a,type(a)) ------------------ (2+3.5j) <class 'complex'>
>>> b=str(a)
>>> print(b,type(b)) ---------------- (2+3.5j) <class 'str'>
>>> b ---- '(2+3.5j)'
================================================================

2. bytes
=========================================================
 'bytes' is one of the pre-defined class and treated as Sequence Data Type.
 The purpose of bytes data type is that "To Implement End-to-End Encryption---Security"
 At time of Implementing End-to-End Encryption by using bytes data type, bytes data type
stores the values in the range of (0,256). i.e bytes data type stores the data from 0 to 255
(256-1).
 In Python Programming, we don't have any symbolic notation for Representing bytes
data. But we can convert into bytes data type by using bytes().
 Syntax: varname=bytes(object)
 An object of bytes maintains Insertion Order (Insertion order is nothing but whatever the
order we organize the data in the same order we display the Data).
 On the Object of bytes, we can perform Both Indexing and Slicing Operations

 An object of bytes belongs to IMMUTABLE because 'bytes' object does not support item
assignment
--------------------------------------------------------------------------------------------------------------------
Examples

PYTHON TRAINING MANUAL, WEEK-3 30


--------------------------------------------------------------------------------------------------------------------
>>> lst=[100,23,256,7,0,12]
>>> print(lst,type(lst))------------------------[100, 23, 256, 7, 0, 12] <class 'list'>
>>> b=bytes(lst)-------------------------------ValueError: bytes must be in range(0, 256)
>>> lst=[100,-23,255,7,0,12]
>>> print(lst,type(lst))-----------------------[100, -23, 255, 7, 0, 12] <class 'list'>
>>> b=bytes(lst)-------------------------------ValueError: bytes must be in range(0, 256)
-------------------------------------------------------------------------------------
>>> lst=[100,23,255,7,0,12]
>>> print(lst,type(lst))-----------------[100, 23, 255, 7, 0, 12] <class 'list'>
>>> b=bytes(lst)
>>> print(b,type(b))-------------------b'd\x17\xff\x07\x00\x0c' <class 'bytes'>
>>> for val in b:
... print(val)
...
100
23
255
7
0
12
------------------------------------------
>>> b[0]------------------100
>>> b[-1]---------------- 12
>>> b[2]-----------------255
>>> b[2:5]--------------b'\xff\x07\x00'
>>> for val in b[2:5]:
... print(val)
...
255
7
PYTHON TRAINING MANUAL, WEEK-3 31
0
>>> for val in b[::-1]:
... print(val)
...
12
0
7
255
23
100
----------------------------------------------------------
>>> b[0]=125-----------Type Error: 'bytes' object does not support item assignment
>>> b[0:2]=[10,125]----Type Error: 'bytes' object does not support item assignment

3. bytearray
============================================================
 bytearray' is one of the pre-defined class and treated as Sequence Data Type.
 The purpose of bytearray data typs is that "To Implement End-to-End Encryption---
Security"
 At time of Implementing End-to-End Encryption by using bytearray data type, bytearray
data type stores the values in the range of (0,256). i.e bytearray data type stores the data
from 0 to 255 (256-1).
 In Python Programming, we don't have any symbolic notation for Representing bytearray
data. But we can convert into bytearray data type by using bytearray().
 Syntax: varname=bytearray(object)

 An object of bytearray maintains Insertion Order (Insertion order is nothing but whatever
the order we organize the data in the
 same order we display the Data).
 On the Object of bytearray , we can perform Both Indexing and Slicing Operations
 An object of bytearray belongs to MUTABLE bcoz 'bytearray' object support item

PYTHON TRAINING MANUAL, WEEK-3 32


assignment
--------------------------------------------------------------------------------------------------------------------
NOTE: The Functionality of bytearray is exactly similar to bytes but an object bytes
belongs to IMMUTABLE because bytes object does not support Item Assignment whereas
an object bytearray belongs to MUTABLE because bytearray object allows Item
Assignment OR Modifications.
------------------------------------------------------------------------------------------------------------------
Examples
----------------------------------------------------------------------------------------------------------------
>>> lst1=[100,35,67,256,67,11]
>>> print(lst1,type(lst1))---------------------[100, 35, 67, 256, 67, 11] <class 'list'>
>>> ba=bytearray(lst1)----------------------ValueError: byte must be in range(0, 256)
>>> lst1=[100,35,0,-67,255,67,11]
>>> print(lst1,type(lst1))----------------------[100, 35, 0, -67, 255, 67, 11] <class 'list'>
>>> ba=bytearray(lst1)------------------ValueError: byte must be in range(0, 256)
-------------------------------------------------------------------
>>> lst1=[100,35,0,67,255,67,11]
>>> print(lst1,type(lst1))-----------------[100, 35, 0, 67, 255, 67, 11] <class 'list'>
>>> ba=bytearray(lst1)
>>> print(ba,type(ba))-------------------bytearray(b'd#\x00C\xffC\x0b') <class 'bytearray'>
>>> for val in ba:
... print(val)
...
100
35
0
67
255
67
11
-------------------------------------------------
PYTHON TRAINING MANUAL, WEEK-3 33
>>> ba[0]----------------------100
>>> ba[-1]---------------------11
>>> ba[1]----------------------35
>>> ba[2:5]-------------------bytearray(b'\x00C\xff')
>>> for val in ba[2:5]:
... print(val)
...
0
67
255
-------------------------------------------------------
>>> lst1=[100,35,0,67,255,67,11]
>>> print(lst1,type(lst1))--------------[100, 35, 0, 67, 255, 67, 11] <class 'list'>
>>> ba=bytearray(lst1)
>>> print(ba,type(ba),id(ba))-------------bytearray(b'd#\x00C\xffC\x0b') <class 'bytearray'>
1568090935856
>>> for val in ba:
... print(val)
...
100
35
0
67
255
67
11
>>> ba[0]=125 # Item Assigment allowed
>>> print(ba,type(ba),id(ba))----------------bytearray(b'}#\x00C\xffC\x0b') <class 'bytearray'>
1568090935856
>>> for val in ba:
... print(val)
PYTHON TRAINING MANUAL, WEEK-3 34
...
125
35
0
67
255
67
11
>>> ba[1]=256--------------------ValueError: byte must be in range(0, 256)
============================================x=================

4. range
===============================================================
 'range' is one of the pre-defined class and treated sequence data type.
 The purpose of range data type is that "To generate sequence of Numerical Integer Values
by maintaining equal Interval of Step either in Forward Direction or Backward
Direction."
 On the object of range data type, we can perform Indexing and Slicing Operations.
 An object of range of range data belongs to Immutable because range object does not
support item assignment
 An object of range maintains Insertion Order.
 To generate range of values, we have 3 types range() and we can use them 3 ways. They
are
range(value)
range (Start,Stop)
range (Start,Stop,Step)
---------------------------------------------------------
Syntax1: varname=range(value)
---------------------------------------------------------
 This Syntax generates range of Values from 0 to value-1 by maintaining default step as
+1

PYTHON TRAINING MANUAL, WEEK-3 35


---------------------------------------------------------
Syntax-2: varname=range(Start,Stop)
---------------------------------------------------------
 This Syntax generates range of values from start to stop-1 by maintaining default step as
+1
-----------------------------------------------------------------------
Syntax-3: varname=range(Start,Stop,Step)
-----------------------------------------------------------------------
 This Syntax generates range of values from start to stop-1 by maintaining Programmer-
Defined Interval in terms of step
--------------------------------------------------------------------------------------------------------------------
Examples
--------------------------------------------------------------------------------------------------------------------
>>> r=range(6)
>>> print(r,type(r))
range(0, 6) <class 'range'>
>>> for v in r:
... print(v)
...
0
1
2
3
4
5
>>> for val in range(10):
... print(val)
...
0
1
2
PYTHON TRAINING MANUAL, WEEK-3 36
3
4
5
6
7
8
9
-------------------------------------------------
>>> r=range(10,16)
>>> print(r,type(r))
range(10, 16) <class 'range'>
>>> for val in r:
... print(val)
...
10
11
12
13
14
15
>>> r[0]
10
>>> r[-1]
15
>>> r[1]
11
>>> for val in range(100,106):
... print(val)
...
100
101
PYTHON TRAINING MANUAL, WEEK-3 37
102
103
104
105
>>> for val in range(100,106)[0:3]:
... print(val)
...
100
101
102
>>>
>>>
>>> r=range(1000,1006)
>>> print(r)
range(1000, 1006)
>>> r[2:5]
range(1002, 1005)
>>> for val in r[2:5]:
... print(val)
...
1002
1003
1004
------------------------------------------------
>>> r=range(10,17,2)
>>> for val in r:
... print(val)
...
10
12
14
PYTHON TRAINING MANUAL, WEEK-3 38
16
-------------------------------------------------
>>> for val in range(90,102,3):
... print(val)
...
90
93
96
99
----------------------------------------------
>>> r=range(1000,1006)
>>> for val in r:
... print(val)
...
1000
1001
1002
1003
1004
1005
>>> r[0]----------------------1000
>>> r[0]=123-----------TypeError: 'range' object does not support item assignment
--------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------
Example sheet
--------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------
Q1) 0 1 2 3 4 5 6 7 8 9 -------range(10) OR range(0,10) OR range(0,10,1)

>>> for val in range(10):


... print(val)
PYTHON TRAINING MANUAL, WEEK-3 39
...
0
1
2
3
4
5
6
7
8
9
>>> for val in range(0,10):
... print(val)
...
0
1
2
3
4
5
6
7
8
9
>>> for val in range(0,10,1):
... print(val)
...
0
1
2
3
PYTHON TRAINING MANUAL, WEEK-3 40
4
5
6
7
8
9
--------------------------------------------------------------------------------------------------------------------

Q2) 10 11 12 13 14 15 16 17 18 19 20-----range(10,21) OR range(10,21,1)


>>> for v in range(10,21):
... print(v)
...
10
11
12
13
14
15
16
17
18
19
20
>>> for v in range(10,21,1):
... print(v)
...
10
11
12
13
14
PYTHON TRAINING MANUAL, WEEK-3 41
15
16
17
18
19
20
--------------------------------------------------------------------------------------------------------------------

Q3) 1000 1001 1002 1003 1004 1005--------range(1000,1006) OR range(1000,1006,1)


>>> for val in range(1000,1006):
... print(val)
...
1000
1001
1002
1003
1004
1005
>>> for val in range(1000,1006,1):
... print(val)
...
1000
1001
1002
1003
1004
1005
--------------------------------------------------------------------------------------------------------------------
Q4) 10 12 14 16 18 20---------------range(10,21,2)
>>> for val in range(10,21,2):
... print(val)
PYTHON TRAINING MANUAL, WEEK-3 42
...
10
12
14
16
18
20
--------------------------------------------------------------------------------------------------------------------
Q5) 1000 1010 1020 1030 1040 1050 ---------range(1000,1051,10)
>>> for val in range(1000,1051,10):
... print(val)
...
1000
1010
1020
1030
1040
1050
--------------------------------------------------------------------------------------------------------------------
Q6) -10 -11 -12 -13 -14 -15------------range(-10,-16,-1)
>>> for val in range(-10,-16,-1):
... print(val)
...
-10
-11
-12
-13
-14
-15
NOTE: >>> for val in range(-10,-16,1):
print(val) # No Output
PYTHON TRAINING MANUAL, WEEK-3 43
--------------------------------------------------------------------------------------------------------------------
----------------------------
Q7) -100 -99 -98 -97 -96 -95------------------------range(-100,-94,1) OR range(-100,-94)
>>> for val in range(-100,-94):
... print(val)
...
-100
-99
-98
-97
-96
-95
>>> for val in range(-100,-94,1):
... print(val)
...
-100
-99
-98
-97
-96
-95
---------------------------------------------------------------------------------------------------------
Q8) -100 -105 -110 -115 -120------------------range(-100,-121,-5)
>>> for val in range(-100,-121,-5):
... print(val)
...
-100
-105
-110
-115
-120
PYTHON TRAINING MANUAL, WEEK-3 44
>>>

---------------------------------------------------------------------------------------------------------
Q9) 1000 900 800 700 600 500-------------------range(1000,499,-100)
>>> for val in range(1000,499,-100):
... print(val)
...
1000
900
800
700
600
500
------------------------------------------------------------------------------------
Q10) -5 -4 -3 -2 -1 0 1 2 3 4 5-------------range(-5,6,1) OR range(-5,6)
------------------------------------------------------------------------------------
>>> for val in range(-5,6):
... print(val)
...
-5
-4
-3
-2
-1
0
1
2
3
4
5
>>> for val in range(-5,6,1):
PYTHON TRAINING MANUAL, WEEK-3 45
... print(val)
...
-5
-4
-3
-2
-1
0
1
2
3
4
5
=====================================================================
==
>>> n=9
>>> for i in range(1,11):
... print(n,"x",i,"=",n*i)
...
9x1=9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90
=====================================================================

PYTHON TRAINING MANUAL, WEEK-3 46


Task:
Practice all the example programs mentioned under each topic

PYTHON TRAINING MANUAL, WEEK-3 47

You might also like