0% found this document useful (0 votes)
9 views64 pages

Python Notes

Uploaded by

Dhanunjay Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views64 pages

Python Notes

Uploaded by

Dhanunjay Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 64

Python

Softwares require to learn Full Stack Python Development


• Python v3.7.0
• IDET - Visual Studio Code

How to save a Python file:


We can save a Python file with a .py extension with any name

• Valid file names:


ihub1.py
test1.py
info1.py
demo1.py

• Invalid file names:


123456.py
ihub 1.py

print() function:
It is a predefine function in Python. The main objective of print() function
is to display the
content on the console or application console with ' ' or " " or ''' ''' or """
""" or a variable (an identifier) name.

Ex1:
print(‘Core Python’)
print(“Core Python”)
print(‘‘‘Core Python’’’)
print(“““Core Python”””)

Output:
Core Python
Core Python
Core Python
Core Python

Ex2:
print(‘Python Full Stack Development’)
print()
print(‘‘‘Python Full Stack Development’’’)
print()
print(“Python Full Stack Development”)
print()
print(“““Python Full Stack Development”””)
print()
print(“End of an application”)

Output:
Python Full Stack Development

Python Full Stack Development

Python Full Stack Development

Python Full Stack Development

Python is a:
Programming Lanuage
Scripting Language
OOPL

Interview_Question:
What is difference between a Program & a Script?

Python as a Program:
If the developers or programmer or software engineers write the business
logic without
using any predefine module then it is said to be a program.

Ex1:
print('Core Python session is going on at IHUB')
print()
print("Core Python session is going on at IHUB")
print()
print('''Core Python session is going on at IHUB''')
print()
print("""Core Python session is going on at IHUB""")
print()
print("End of an application")

Python as a Script:
If the developers or programmers or software engineers write the
business logic with
one or more than one module then it is said to be a script.

Ex1:
import time
time.sleep(3)
print("Good morning dear students ....")

id() function:
It is a predefine function in Python. The main objective of id() function is
to know the address of a variable(an identifier) at memory allocation
where our data or information stored at memory allocation.

Ex1:
import time
Eid=1001
print(Eid)
print()
print(id(Eid))
print()
time.sleep(2)
print("End of an application")

Ex2:
import time
Eid=1001
print("Eid is:",Eid)
print()
print("Address of Eid at memory allocation is:",id(Eid))
print()
time.sleep(2)
print('End of an application')

Interview_Question:
What is the difference between Java & Python?

type() function:
It is a predefine function in Python. The main objective of type() function is
to know the type of data type we are using while writing the business
logic for our applicaiton.

Ex1:
import time
Pid=5001
print("Pid is:",Pid)
print()
print("Address is:",id(Pid))
print()
print("Data type is:",type(Pid)) #<class 'int'>
print()
time.sleep(2)
print('End of an application')

Interview_Question
What is <class 'int'> In python?

Variable (Identifier)
What is the difference between an identifier & a variable?
1+2=3
2+1=3
It is a name inside the Python program or Python module or Python Script.

Rule_1:
Only allowed characters are A-Z, a-z, number or decimal number from 0-
9 and,
only special character(_).
Specail characters in Python:_,space,$,#,@
From above specail characters only _ is allowed.

Ex1:
import time
ABC=150
print("The value is:",ABC)
print()
print("Address is:",id(ABC))
print()
print("Data type is:",type(ABC))
print()
time.sleep(3)
print('End of an application')

Ex2:
import time
abc=150
print("The value is:",abc)
print()
print("Address is:",id(abc))
print()
print("Data type is:",type(abc))
print()
time.sleep(3)
print('End of an application')

Ex3:
import time
ABCabc=150
print("The value is:",ABCabc)
print()
print("Address is:",id(ABCabc))
print()
print("Data type is:",type(ABCabc))
print()
time.sleep(3)
print('End of an application')

Ex4:
import time
ABC_abc=150
print("The value is:",ABC_abc)
print()
print("Address is:",id(ABC_abc))
print()
print("Data type is:",type(ABC_abc))
print()
time.sleep(3)
print('End of an application')

Ex5:
import time
ABC_abc_12345=150
print("The value is:",ABC_abc_12345)
print()
print("Address is:",id(ABC_abc_12345))
print()
print("Data type is:",type(ABC_abc_12345))
print()
time.sleep(3)
print('End of an application')

Ex6:
import time
_=150
print("The value is:",_)
print()
print("Address is:",id(_))
print()
print("Data type is:",type(_))
print()
time.sleep(3)
print('End of an application')

Ex7:
import time
_________________________________=150
print("The value is:",_________________________________)
print()
print("Address is:",id(_________________________________))
print()
print("Data type is:",type(_________________________________))
print()
time.sleep(3)
print('End of an application')

Ex8:
import time
_ _=150
print("The value is:",_ _)
print()
print("Address is:",id(_ _))
print()
print("Data type is:",type(_ _))
print()
time.sleep(3)
print('End of an application')

Ex9:
import time
$=150
print("The value is:",$)
print()
print("Address is:",id($))
print()
print("Data type is:",type($))
print()
time.sleep(3)
print('End of an application')

Ex10:
import time
ABC_abc_$=150
print("The value is:",ABC_abc_$)
print()
print("Address is:",id(ABC_abc_$))
print()
print("Data type is:",type(ABC_abc_$))
print()
time.sleep(3)
print('End of an application')

Ex11:
import time
ABC_abc=125
print("The value is:" ABC_abc)
print()
print("Address is:",id( ABC_abc))
print()
print("Data type is:",type( ABC_abc))
print()
time.sleep(2)
print('End of an application')

Rule_2:
An identifier or variable should not start with a number or decimal number
from 0-9.

Ex1:
import time
12345_ABC_abc=10000
print("The value is:",12345_ABC_abc)
print()
print("Address is:",id(12345_ABC_abc))
print()
print("Data type is:",type(12345_ABC_abc))
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
A12345_ABC_abc=10000
print("The value is:",A12345_ABC_abc)
print()
print("Address is:",id(A12345_ABC_abc))
print()
print("Data type is:",type(A12345_ABC_abc))
print()
time.sleep(2)
print('End of an application')

Ex3:
import time
a12345_ABC_abc=10000
print("The value is:",a12345_ABC_abc)
print()
print("Address is:",id(a12345_ABC_abc))
print()
print("Data type is:",type(a12345_ABC_abc))
print()
time.sleep(2)
print('End of an application')

Ex4:
import time
_12345_ABC_abc=10000
print("The value is:",_12345_ABC_abc)
print()
print("Address is:",id(_12345_ABC_abc))
print()
print("Data type is:",type(_12345_ABC_abc))
print()
time.sleep(2)
print('End of an application')

Rule_3:
There is no length limit for an identifier (variable).

Ex1:
import time
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAA=1000
print("The value
is:",AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAA)
print()
print('Address
is:',id(AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAA))
print()
print("Data type
is:",type(AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAA))
print()
time.sleep(3)
print('End of an application')

Ex2:
import time
A=1000
print("The value is:",A)
print()
print("Address is:",id(A))
print()
print("Data type is:",type(A))
print()
time.sleep(2)
print('End of an application')

Ex3:
import time
Emp_age=21
print("Age is:",Emp_age)
print()
print("Address is:",id(Emp_age))
print()
print("Data type is:",type(Emp_age))
print()
time.sleep(2)
print("End of an application")

Rule_4:
An identifier or a variable is case sensitive & case in-sensitive as well.

case sensitive:
If the addresses of variables are different then it is said to be case
sensitive.
A=120 ---->id(A) ----->12345
a=100 ----->id(a) ------>54321

Ex1:
import time
A=120
a=100
print("====Python_Objects_Value====")
print(A)
print()
print(a)
print()
print("====Addresses====")
print(id(A))
print()
print(id(a))
print()
time.sleep(2)
print('End of an application')

case in-sensitive:
If the addresses of variables are same then it is said to be case in-
sensitive.
A=100 ----->id(A) ---->22331
a=100 ----->id(a) ------>22331

Ex1:
import time
A=100
a=100
print("====Python_Objects_Value====")
print(A)
print()
print(a)
print()
print("====Addresses====")
print(id(A))
print()
print(id(a))
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
A=100
A=200
A=300
A=400
A=500
print("====Python_Objects_Value====")
print(A)
print()
print(A)
print("====Addresses====")
print(id(A))
print()
print(id(A))
time.sleep(2)
print('End of an application')

Output:
====Python_Objects_Value====
500
500
====Addresses====
21617904

21617904
End of an application

Rule_5:
An identifier or variable should not start with a reserved keyword. As we
know that in Python there are 35 reserved keywords. In that first three are
in title case (True, False, None) and remaining all are in lowercase.
To display 35 reserved keyword:
import time
import keyword as IHUB
print('----------35 Reserved Keywords----------')
print(IHUB.kwlist)
print('------------------------------------------------')
time.sleep(2)
print('End of an application')

Output:
----------35 Reserved Keywords----------
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if',
'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try',
'while', 'with', 'yield']
-----------------------------------------------
End of an application

Ex1:
import time
if=1000
print("The value is:",if)
print()
print('The address is:',id(if))
print()
print("The data type is:",type(if))
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
If=1000
print("The value is:",If)
print()
print('The address is:',id(If))
print()
print("The data type is:",type(If))
print()
time.sleep(2)
print('End of an application')

Ex3:
import time
True=1000
print("The value is:",True)
print()
print('The address is:',id(True))
print()
print("The data type is:",type(True))
print()
time.sleep(2)
print('End of an application')
Ex4:
import time
TruE=1000
print("The value is:",TruE)
print()
print('The address is:',id(TruE))
print()
print("The data type is:",type(TruE))
print()
time.sleep(2)
print('End of an application')

Task_1:
->Develop 25 valid_cases(Python script) with five rules of an identifier?
Task_2:
->Develop 25 In_valid_cases(Python script) with five rules of an identifier?

Data types in Python


Why do we use data types?
Data types are used to store different values at memory allocation via
variables.
In python we have the following types of data types:
1. Integer data type
2. Floating data type
3. Boolean data type
4. String data type
5. Complex data type
6. List data type
7. Tuple data type
8. Set data type
9. Dict data type
10. Bytes data type
11. Bytearray data type
12. Frozenset data type
13. Range data type
14. None data type

Integer data type:


Python supports Interger data type. It can be represented as a number or
decimal number from 0-9. It may be positive number or negative number
with any length.

Ex1:
import time
X1=123
print("The value is:",X1)
print()
print("The data type is:",type(X1))
print()
X2=-156
print("The value is:",X2)
print()
print("The data type is:",type(X2))
print()
time.sleep(2)
print('End of an application')

Floating data type:


Python supports Floating data type. It can be represented as a decimal
point or floating point number. It may be either positive floating number
or negative floating number. In python 1.3e is also consider as floating
data type
e (exponent) = 102 (10*10)

Ex1:
import time
Y1=156.67
print("The value is:",Y1)
print()
print("The data type is:",type(Y1))
print()
Y2=-112.3456
print("The value is:",Y2)
print()
print("The data type is:",type(Y2))
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
X1=1.3e
print("The value is:",X1)
print()
print("The data type is:",type(X1))
print()
time.sleep(2)
print('End of an application')

Ex3:
import time
X1=1.3*10*10
print("The value is:",X1)
print()
print("The data type is:",type(X1))
print()
time.sleep(2)
print('End of an application')
Ex4:
import time
X1=1.3e1
print("The value is:",X1)
print()
print("The data type is:",type(X1))
print()
time.sleep(2)
print('End of an application')

Ex5:
import time
X1=1.3e2
print("The value is:",X1)
print()
print("The data type is:",type(X1))
print()
time.sleep(2)
print('End of an application')

Ex6:
import time
X1=1.3e0
print("The value is:",X1)
print()
print("The data type is:",type(X1))
print()
time.sleep(2)
print('End of an application')

Boolean data type:


Python supports boolean data type. Boolean data types return True or
False value after checking the condition (==, !=).

Ex1:
import time
X1=1200
X2=1400
print('=====Using Booelean data type======')
print(X1)
print()
print(X2)
print()
res1=X1==X2
res2=X1!=X2
print("The result_set is:",res1)
print()
print("The data type is:",type(res1))
print()
print("The result_set is:",res2)
print()
print("The data type is:",type(res2))
print()
time.sleep(2)
print('End of an application')

String data type:


Python supports String data type.

What is String?
String can be represent as set of characters or sequence of characters
enclosed with ‘ ’ or “ ” or ‘‘‘ ’’’ or “““ ”””
While working with string data type space is also consider a character.
Python supports positive direction which can be represent as 0 to end-1. It
is also known
as forward direction.
Python also supports negative direction which can be represent as -1 to
end+1. It is also known as backward direction.
‘‘‘ ’’’ or “““ ””” is used to display multi line content.

Ex1:
import time
str1='Core Python'
str2="Advance Python"
str3='''FrontEnd+Django'''
str4="""Angular/React + Flask +FastAPI"""
str5='10'
str6="True"
str7='145.67'
print()
print("====Python_Objects=====")
print(str1)
print(str2)
print(str3)
print(str4)
print(str5)
print(str6)
print(str7)
print()
print("=======Data types========")
print(type(str1))
print(type(str2))
print(type(str3))
print(type(str4))
print(type(str5))
print(type(str6))
print(type(str7))
print()
time.sleep(2)
print('End of an application')

I will be a 'Software Engineer' after end of Full Stack Python development


Course.
Form_1:
import time
str1="I will be a 'Software Engineer' after end of Full Stack Python
development Course"
print()
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print('End of an application')

Form_2:
import time
str1='''I will be a 'Software Engineer' after end of Full Stack Python
development Course'''
print()
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print('End of an application')

Form_3:
import time
str1="""I will be a 'Software Engineer' after end of Full Stack Python
development Course"""
print()
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print('End of an application')

Invalid_Case:
import time
str1='I will be a 'Software Engineer' after end of Full Stack Python
development Course'
print()
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print('End of an application')

I will be a 'Software Engineer' after end of "Full Stack Python


development" Course.

Form_1:
import time
str1="""I will be a 'Software Engineer' after end of "Full Stack Python
development" Course"""
print()
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print('End of an application')

Form_2:
import time
str1='''I will be a 'Software Engineer' after end of "Full Stack Python
development" Course'''
print()
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print('End of an application')

Invalid_Case:
Ex1:
import time
str1='I will be a 'Software Engineer' after end of "Full Stack Python
development" Course'
print()
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
str1="I will be a 'Software Engineer' after end of "Full Stack Python
development" Course"
print()
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print('End of an application')

Ex3:
import time
str1="I will be a 'Software Engineer' after end of 'Full Stack Python
development' Course"
print()
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print('End of an application')

Ex4:
import time
str1='I will be a "Software Engineer"" after end of "Full Stack Python
development" Course'
print()
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print('End of an application')

Good morning Dear Students


Hope you are getting the Python Skills
Thank you
IHUB TALENT

Ex1:
import time
str1='''Good morning Dear Students
Hope you are getting the Python Skills
Thank you
IHUB TALENT'''
print()
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
str1="""Good morning Dear Students
Hope you are getting the Python Skills
Thank you
IHUB TALENT"""
print()
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print('End of an application')

Ex3:
import time
str1="Good morning Dear Students
Hope you are getting the Python Skills
Thank you
IHUB TALENT"
print()
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print('End of an application')

Ex4:
import time
str1='Good morning Dear Students
Hope you are getting the Python Skills
Thank you
IHUB TALENT'
print()
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print('End of an application')

Ex5:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print("=====Using + direction=====")
print(str1[0])
print(str1[1])
print(str1[2])
print(str1[3])
print()
print('=====Using - direction=====')
print(str1[-11])
print(str1[-10])
print(str1[-9])
print(str1[-8])
print()
time.sleep(2)
print('End of an application')

Slice operator:
Python supports slice operator. The main objective of slice operator is to
make the pieces of the string as per the application requirement. Slice
operator is applicable for positive direction and negative direction as well.
The syntax for slice operator as follows:
str1[begin:end(end-1):step]

Slice operator with positive direction(Forward direction):


Form_1:
str1[begin:]
Here end(end-1) and step value is optional. The output of the string will be
from given position to end of the string.

str1[3:]
Ex1:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_1====')
print(str1[3:])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_1====')
print(str1[7:])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex3:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_1====')
print(str1[0:])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex4:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_1====')
print(str1[12:])
print('=================')
print()
time.sleep(2)
print('End of an application')

Form_2:
str1[:end(end-1)]
Here begin and step value is optional. The output of string will be from
indexing position (0) to end-1 position.
str1[:7] ----->7-1=6
Ex1:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_2====')
print(str1[:7])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_2====')
print(str1[:1])
print('=================')
print()
time.sleep(2)
print('End of an application')
Ex3:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_2====')
print(str1[:11])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex4:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_2====')
print(str1[:100])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex5:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_2====')
print(str1[:4])
print('=================')
print()
time.sleep(2)
print('End of an application')

Form_3:
str1[begin:end(end-1)]
Here step is optional. The output of the string will be from given position
to end-1 position.
str1[1:9] ----9-1=8

Ex1:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_3====')
print(str1[1:9])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_3====')
print(str1[9:11])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex3:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_3====')
print(str1[0:11])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex4:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_3====')
print(str1[5:7])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex5:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_3====')
print(str1[100:5])
print('=================')
print()
time.sleep(2)
print('End of an application')

Form_4:
str1[begin:end(end-1):step]
Here output will be generated based on step value.
str1[0:11:1]

Ex1:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_4====')
print(str1[0:11:1])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_4====')
print(str1[0:11:2])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex3:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_4====')
print(str1[0:11:3])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex4:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_4====')
print(str1[2:9:4])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex5:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_4====')
print(str1[0:3:2])
print('=================')
print()
time.sleep(2)
print('End of an application')

Form_5:
str1[0:]
or
str1[:]
or
str1[::]

Ex1:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form_5====')
print(str1[0:])
print()
print(str1[:])
print()
print(str1[::])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with + direction=====')
print()
print('===Form====')
print(str1[1:5:0])
print('=================')
print()
time.sleep(2)
print('End of an application')

Ex3:
import time
str1="Core Python"
print("Reverse of string is:",str1[::-1])
print()
time.sleep(2)
print('End of an application')

Ex4:
import time
str1="Core Python"
print("My string_object is in ascending_order is:",str1[0:])
print()
print("My string_object is in decending_order is:",str1[::-1])
print()
time.sleep(2)
print('End of an application')

Slice operator with negative direction:


Python supports slice operator with negative direction which is from -1 to
end+1. It is also known as backward direction.While working with
negative direction our step value must be negative number otherwise
most of the time output will be empty.
str1[-1:-7:-1] ---->-7+1=-6

Ex1:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[-1:-7:-1])
print('======================')
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[-1:-7:-2])
print('======================')
print()
time.sleep(2)
print('End of an application')

Ex3:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[-1:-8:-3])
print('======================')
print()
time.sleep(2)
print('End of an application')

Ex4:
import time
str1="Core Python"
print(str1)
print(type(str1))
print('====Slice operator with - direction=====')
print(str1[-1:-7:0])
print('======================')
time.sleep(2)
print('End of an application')

Output:
Core Python
<class 'str'>
====Slice operator with - direction=====
Traceback (most recent call last):
File "test1.py", line 8, in <module>
print(str1[-1:-7:0])
ValueError: slice step cannot be zero

Ex5:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[-1:-7:1])
print('======================')
print()
time.sleep(2)
print('End of an application')
Ex6:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[-1:-7])
print('======================')
print()
time.sleep(2)
print('End of an application')

Ex7:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[-1:])
print('======================')
print()
time.sleep(2)
print('End of an application')

Ex8:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[-2:])
print('======================')
print()
time.sleep(2)
print('End of an application')

Ex9:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[-6:])
print('======================')
print()
time.sleep(2)
print('End of an application')

Ex10:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[-9:])
print('======================')
print()
time.sleep(2)
print('End of an application')

Ex11:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[:-3])
print('======================')
print()
time.sleep(2)
print('End of an application')

Ex12:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[:-7])
print('======================')
print()
time.sleep(2)
print('End of an application')

Ex13:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[:-5])
print('======================')
print()
time.sleep(2)
print('End of an application')

Ex14:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[::-1])
print('======================')
print()
time.sleep(2)
print('End of an application')

Ex15:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[::-2])
print('======================')
print()
time.sleep(2)
print('End of an application')

Ex16:
import time
str1="Core Python"
print(str1)
print()
print(type(str1))
print()
print('====Slice operator with - direction=====')
print(str1[::-5])
print('======================')
print()
time.sleep(2)
print('End of an application')

input() function
It is a predefine function in python. The main objective of input() function
is to dynamically read the value from the keyboard. While working with
input() function in python
if we read any data or information from the keyboard, PVM (Python Virtual
Machine) will by default consider it as a string data type.

Ex1:
import time
X1=input("Enter the value of X1:")
print(X1)
print()
print(type(X1))
print()
time.sleep(2)
print('End of an application')

Output:
Enter the value of X1:123.45
123.45

<class 'str'>

End of an application

Ex2:
import time
X1=input("Enter the value of X1:")
X2=input("Enter the value of X2:")
res1=X1+X2
print('The result_set is:',res1)
print()
time.sleep(2)
print('End of an application')

Why typecasting is required in python?

Ex1:
import time
print("====Before Typecasting=====")
X1=input("Enter the value of X1:")
X2=input("Enter the value of X2:")
res1=X1+X2
print(X1)
print()
print(type(X1))
print()
print(X2)
print()
print(type(X2))
print()
print('The result_set is:',res1)
print()
print(type(res1))
print()
print("====After typecasting======")
X1=int(input("Enter the value of X1:"))
X2=int(input("Enter the value of X2:"))
res1=X1+X2
print(X1)
print()
print(type(X1))
print()
print(X2)
print()
print(type(X2))
print()
print('The result_set is:',res1)
print()
print(type(res1))
print()
time.sleep(2)
print('End of an application')

Output:
====Before Typecasting=====
Enter the value of X1:100
Enter the value of X2:200
100

<class 'str'>

200

<class 'str'>

The result_set is: 100200

<class 'str'>

====After typecasting======
Enter the value of X1:100
Enter the value of X2:200
100

<class 'int'>

200

<class 'int'>

The result_set is: 300

<class 'int'>

End of an application

Typecasting:
Python supports typecasting. It is a process or methodlogy to convert
variable from one data type to another data type.
To implement typecasting in python we have the following functions:
->int()
->float()
->complex()
->bool()
->str()

Ex1:
import time
X1=int(input("Enter the value of X1:"))
X2=int(input("Enter the value of X2:"))
res1=X1+X2
print('The result_set is:',res1)
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
X1=float(input("Enter the value of X1:"))
X2=float(input("Enter the value of X2:"))
res1=X1+X2
print('The result_set is:',res1)
print()
time.sleep(2)
print('End of an application')

Ex3:
import time
X1=complex(input("Enter the value of X1:"))
X2=complex(input("Enter the value of X2:"))
res1=X1+X2
print('The result_set is:',res1)
print()
time.sleep(2)
print('End of an application')

Ex4:
import time
X1=bool(input("Enter the value of X1:"))
X2=bool(input("Enter the value of X2:"))
res1=X1+X2
print('The result_set is:',res1)
print()
time.sleep(2)
print('End of an application')

eval() function:
It is a predefine function in python. The main objective of eval() function is
to read any type of data or information from the keyboard. While reading
the string data type we must use ‘ ’ or “ ”
eval() function is also used to perform following operations:
->String Concatenation
->String Reversal
->Arithmetic Operations

Ex1:
import time
X1=eval(input('Enter any type of as you wish:'))
print(X1)
print()
print(type(X1))
print()
time.sleep(2)
print('End of an application')
Ex2:
import time
X1=eval(input('Enter any type of data as you wish:'))
X2=eval(input('Enter the value of X2:'))
res1=X1+X2
print("The result is:",res1)
print()
time.sleep(2)
print('End of an application')

Ex3:
import time
X1=eval(input('Enter any type of data as you wish:'))
X2=eval(input('Enter the value of X2:'))
res1=X1*X2
print("The result is:",res1)
print()
time.sleep(2)
print('End of an application')

Immutable object (State-less object) & Mutable object (State-full


object)
Immutable object (State-less object):
Once we create an object, we cannot make the changes as per the
application requirement then it is said to be Immutable object (State-less
object).

Ex:
Int, Float, Str, Complex, Bool, Tuple, Frozenset, Bytes

Ex1:
import time
str1="core Python"
print('====Before Immutable operations======')
print(str1)
print()
print(type(str1))
print()
print('====After Immutable operations=====')
str1[0]="C"
print(str1)
print()
print(type(str1))
print()
time.sleep(2)
print("End of an application")

Output:
====Before Immutable operations======
core Python

<class 'str'>

====After Immutable operations=====


Traceback (most recent call last):
File "test1.py", line 9, in <module>
str1[0]="C"
TypeError: 'str' object does not support item assignment

Mutable object(State-full object):


Once we create an object, we can make changes as per the application
requirement then it
said to be Mutable object (State-full object).

Ex:
List, Set, Dict, Bytearray
List data type
Python supports List data type. If we want to represent one or more than
one object or group of objects as a single entity then we can use List data
type. In python List data type can be represented as [] and list().

Ex1:
import time
L1=[]
print(L1)
print()
print(type(L1))
print()
L2=list()
print(L2)
print()
print(type(L2))
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
L1=[1001]
print(L1)
print()
print(type(L1))
print()
time.sleep(2)
print('End of an application')

Ex3:
import time
L1=[1001,1002]
print(L1)
print()
print(type(L1))
print()
time.sleep(2)
print('End of an application')

Properties of List data type:


->Inseration is preserved.
->Duplicate objects are allowed.
->Heterogenous objects are allowed.
->List is mutable (State-full) object.
->List is dynamic or growable.
->Indexing and slice operation is applicable both (+,-) direction.

Inseration is preserved (i.e., Input==Output):


Ex1:
import time
L1=eval(input("Enter the List objects: "))
print(L1)
print()
print(type(L1))
print()
print("====Fetcing each object from the list====")
for x1 in L1:
print(x1)
print()
time.sleep(2)
print('End of an application')

Output:
Enter the List objects: [1001, 1002, 1003, 1004]
[1001, 1002, 1003, 1004]

<class 'list'>

====Fetcing each object from the list====


1001
1002
1003
1004

End of an application

Duplicate objects are allowed:


Ex1:
import time
L1=eval(input("Enter the List objects: "))
print(L1)
print()
print(type(L1))
print()
print("====Fetcing each object from the list====")
for x1 in L1:
print(x1)
print()
time.sleep(2)
print('End of an application')

Output:
Enter the List objects: [1001,1002,1003,1004,1001,1002,1003]
[1001, 1002, 1003, 1004, 1001, 1002, 1003]

<class 'list'>
====Fetcing each object from the list====
1001
1002
1003
1004
1001
1002
1003

End of an application

Hetrogenious objects are allowed:


Ex1:
import time
L1=eval(input("Enter the List objects: "))
print(L1)
print()
print(type(L1))
print()
print("====Fetcing each object from the list====")
for x1 in L1:
print(x1)
print()
time.sleep(2)
print('End of an application')

Output:
Enter the List_data type:[1001,"Mobile_1",27000.0,"Samsung"]
[1001, 'Mobile_1', 27000.0, 'Samsung']

<class 'list'>
====Fetcing the each object from list====
1001
Mobile_1
27000.0
Samsung

End of an application

List is mutable (State-full) object:


Ex1:
import time
L1=['A','B','C','D','E']
print("====Before Mutable operations====")
print(L1)
print()
print(type(L1))
print()
print("=====After mutable operations=====")
L1[0]="A for apple"
L1[1]="B for banana"
L1[2]="C for carrot"
L1[3]="D for dragon-fruit"
L1[4]="E for egg"
print(L1)
print()
print(type(L1))
print()
time.sleep(2)
print('End of an applicaiton')

Output:
====Before Mutable operations====
['A', 'B', 'C', 'D', 'E']
<class 'list'>

=====After mutable operations=====


['A for apple', 'B for banana', 'C for carrot', 'D for dragon-fruit', 'E for egg']

<class 'list'>

End of an applicaiton

List is dynamic or growable:


List data type is dynamic or growable which means increasing or
decreasing the size of the
list by using the following List functions:
->append(): It is used to add only one object at a time
->remove(): It is used to remove only one object at a time

Ex1:
import time
L1=[]
print(L1)
print()
print("===Adding the Product_Information====")
L1.append(1001,"Mobile_1")
print(L1)

Ex2:
import time
L1=[]
print(L1)
print()
print("===Adding the Product_Information====")
L1.append(1001)
L1.append("Mobile_1")
L1.append(29000)
L1.append("Samsung")
print(L1)
print()
print('===Removing the Product_Information====')
L1.remove(1001)
L1.remove("Mobile_1")
L1.remove(29000)
L1.remove("Samsung")
print(L1)
print()
time.sleep(2)
print("End of an application")

Output:
[]

===Adding the Product_Information====


[1001, 'Mobile_1', 29000, 'Samsung']

===Removing the Product_Information====


[]

End of an application

Indexing and slice operator is applicable:


Ex1:
import time
L1=[1001,"Mobile_1",31000,"Apple"]
print(L1)
print()
print("====Product_Information====")
print("Pid is:",L1[0])
print("Pname is:",L1[1])
print("Price is:",L1[2])
print("Company is:",L1[3])
print()
print("====Using slice operator====")
print(L1[0:])
print()
print(L1[::-1])
print()
print(L1[1:4])
print()
print(L1[-1:-4:-1])
print()
time.sleep(2)
print('End of an application')

Interview_Question:

Where can we use List data type in real time application?


We can use List data type in real time for dynamic operations such as
inserting, deleting, updating the application data or information as per the
applicaiton requirement.

Predefine functions for List data type:


->append()
->remove()
->clear()
->copy()
->extend()
->insert()
->pop()
->sort()
->sorted()

clear():
It is used to clear the complete list objects all at once.

Ex1:
import time
L1=[1001,1002,1003,1004,1005]
print("Before Operations:",L1)
print()
L2=L1.clear()
print("After operations:",L2)
print()
time.sleep(2)
print('End of an application')

Ex2:
import time
L1=[1001,1002,1003,1004,1005]
print("Before Operations:",L1)
print()
L1.clear()
print("After operations:",L1)
print()
time.sleep(2)
print('End of an application')

You might also like