Python Notes
Python Notes
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 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?
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')
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')
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')
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')
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')
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')
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]
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')
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')
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'>
<class 'str'>
====After typecasting======
Enter the value of X1:100
Enter the value of X2:200
100
<class 'int'>
200
<class 'int'>
<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')
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'>
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')
Output:
Enter the List objects: [1001, 1002, 1003, 1004]
[1001, 1002, 1003, 1004]
<class 'list'>
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
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
Output:
====Before Mutable operations====
['A', 'B', 'C', 'D', 'E']
<class 'list'>
<class 'list'>
End of an applicaiton
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:
[]
End of an application
Interview_Question:
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')