Python Week3 Notes
Python Week3 Notes
and Management
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)
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'>
=====================================================================
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
----------------------------------------
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]-----''
1. int()
2. float()
3. bool()
4. complex()
5. str()
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
****************************************************************************
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'>
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'>
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'
-----------------------------------
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
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
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
---------------------------------------------------------------------------------------------------------
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
=====================================================================