Python Programming Basics Explained
Python Programming Basics Explained
[Link] is the fundamental diffrence between Interactive mode and batcj mode and
explain with steps?
Ans:-
Interactive mode
=================
->Execute the python program line by line.
->This mode is genrally use for testing purpose
steps
-----
->go to start menu select python3.7(64-bit)
->here type the lines
>>> a=100
>>> b=200
>>> a+b
300
>>> a-b
-100
>>> a*b
20000
Batch mode
===========
Execute the entire python program file is called Batch mode.
[Link] is the diffrence between local varibale and global variable and explain with
examples?
Ans:-
Local Variable
-----------------
->A varibale which is declared within the function which known as local variable
->Local variable only accessed within the function.
Ex
===
def function1():
a=100 ---->Local variable
b=200 ---->Global variable
print("Addition=",a+b)
In this example a,b are Local variable these are only accessed eithin the
function.
def addition():
a=100
b=200
print("Addition of two number",a+b)
def substraction():
aprint("Substraction",a-b)
a,b are local variable of addition function so a,b are not accessed within
substraction
Global variable
===============
"global variable" variable declared in outside of the function
It can be accessed throughout the module.
Ex
====
Global_variable=1000
def function1():
print("function1(),global variable=",Global_variable)
def function2():
print("function2(),global variable=",Global_variable)
output
========
function1()
function1(),Global variable=1000
function2()
function,Global variable=1000
[Link] is a class?
Ans:-class is a collection of variable and methods.
[Link] is an Object?
Ans:-Object is a physical existance of a clas or a instance of a class.
[Link] is an Instance variable?
Ans:-if data is changing from one object to another object then should use instance
variable.
[Link] a program or write a program for showing the diffrence between,with out
typecasting and with type casting?
Ans
====
without typecasting
=====================
>without typecasting function we can only perform string operation.
>We can not perform arithmetic operation without using type casting
Ex
===
a=input('Enter the value of a')
b=input('Enter the value of b')
print(a+b)
output
======
Enter the value of a10
Enter the value of b20
1020
>here is we are going to add two it will concatinate the value.
Ex
========
int_value=int(input('Enter integer value'))
float_value=float(input('Enter float value'))
string_value=input('Enter string value')
bool_value=bool(input('Enter boolean value'))
complex_value=complex(input('Enter complex value'))
output
=======
Enter integer value10
Enter float value10.2
Enter string valuearjit
Enter boolean valueTrue
Enter complex value1+2j
3)In memory instance variable create seprate copy,i.e 3)In memory static
variable create single copy for all the object.
number of variable equal to number of number of copies.
E1 Ex
---- ===
Emp_no='E001' Company_name='TCS'
Emp_Name='Raju' Company_ID='TCS001'
[Link] is the diffrence between bitwise operator and bitwise assignment operator?
i)BitWise operator perform operation bits the following are the bitwise operator.
Ex
---
i)Bitwise and operator(&)
ii)Bitwise or operator(|)
iii)Bitwise XOR operator(^)
iv)Right shift operator(<<)
v)Left shift operator(>>)
vi)One's Compliment Bitwise Operator(~)
ii)
>Bitwise-Assignment operator perform two operation one bitwise operation and second
is assignment operation.
>It is also called as short hand assignment operator.
Ex
---
&=
|=
^=
<<=
>>=
[Link] Method overloading Method operation [Link] Method overriding child class
change the method logic of parent class.
and method logic is same
[Link] is the diffrence between paking and un-paking of tuple show practically.
Ans:
Packing
-----------
Packing diffrent object into a tuple is called packing
Ex
----
>>> a=100
>>> b=200
>>> c=300
>>> d=400
>>> t=(a,b,c,d)
>>> t
(100, 200, 300, 400)
Un-packing
-------------------
Dividing the from the elementss from the tuple
Ex
---
>>> t=(10,20,30,40)
>>> a,b,c,d=t
>>> a
10
>>> b
20
>>> c
30
>>> d
40
[Link] to call either parent class method or parent class constructor in child
class.
Ans:By using super Method we call parent class method into child class.
(OR)
By using super() Method we can also call parent class constructor into child class
Ex:Let us call parent class Method into child class Method by using super() method.
---
class Parent:
def AO(self):
self.a=int(input('Enter first integer'))
self.b=int(input('Enter seconnd integer'))
print('Addition',self.a+self.b)
print('Substraction',self.a-self.b)
class Child(Parent):
def AO(self):
super().AO()
print('Multiplication:',self.a*self.b)
print('Division',self.a/self.b)
c1=Child()
[Link]()
Let us call parent class Constructor into child class constructor by using super()
method.
Ans:
class Parent:
def __init__(self):
self.a=int(input('Enter first inetger'))
self.b=int(input('Enter second integer'))
print('Addition',self.a+self.b)
print('Substraction',self.a-self.b)
class Child(Parent):
def __init__(self):
super().__init__()
print('Multiplication',self.a*self.b)
print('Division',self.a/self.b)
c=Child()
[Link] many types of error are existed in python and prove with diffrent examples?
Ans:Two type of Error occur in python
[Link] Error
[Link]-time Error
Syntax Error
----------------------
if Programmer write wrong syntax then Syntax Error occur.
Run-time Error
-----------------------
During the period of Execution if any Error occur is call Run-time Error
it may be
i)wrong input
ii)specified file may not be existed
iii)Database connection failure
iv)Network Error
v)Memory is not sufficient
Ex
---
print('Division',10/0)
output
----------
ZeroDivisionError
>>> print 'arjit'
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('arjit')?
[Link] the operation of tuple by using tuple method?
Ans:
except block
----------------
except block is also call 'except' clause
except block contain the Exception handling code(i.e solution to the raised
program)
except ValueError:
except typeError:
Syntax Syntax
------------- -------------------------
set={element-1,element-2,.................,elment-n} f=({element-1,element-
2,........................,element-3})
33)what is the speciality of finally block explain with example?
Ans:The speciality of finally block is finally block excute always it means the
exception is raised or not raised (OR)The exception is handled or not handled
Ex
-----
Enter first integer:10
Enter second integer:0
Enter valid input
finally block is executed
PS D:\python\Coding> & C:/Python37/[Link] d:/python/Coding/[Link]
Enter first integer:10
Enter second integer:2
Multiplication: 5.0
finally block is executed
PS D:\python\Coding> & C:/Python37/[Link] d:/python/Coding/[Link]
Enter first integer:apple
Enter valid input
finally block is executed
PS D:\python\Coding>
ii)Duplicate keys are not allowed but duplicate values are allowed.
>>> Mobile_Details={1122:'Samsung S7',2233:'Lenovo A6',1122:'Sony A6'}
>>> Mobile_Details
{1122: 'Sony A6', 2233: 'Lenovo A6'}
So,python hash() function produce 'Hash code (OR) unique id number' to an mutable
object
Ex
-----
>>> frozenset=frozenset({10,20,30,40})
>>> hash(frozenset)
-8101661640447970608
36)give 4 Real-time example of dictioonary?
iii)Syntax iii)Syntax
---------------- except:
except ZerodivisionError: print('Please enter valid
input')
print(''Divisible by zero is not possible)
except block.
39)How to handle multiple Exception?
To handle multiple exception we have 2 choice.
i)'try' and 'Named except block'
ii)'try' and 'Default except block'
Ex
----
here individual message given for diffrent Exception
try:
i=int(input('Enter first integer:'))
j=int(input('Enter second integer:'))
print('Division:',i/j)
print('********************************')
a=float(input('Enter first float:'))
b=float(input('Enter second float:'))
print('Multiplication:',a*b)
except ZeroDivisionError:
print('A Number is not divisible by zero')
except ValueError:
print('Please Enter valid input')
output:
----------
Enter first integer:10
Enter second integer:0
A Number is not divisible by zero
PS D:\python\Coding> & C:/Python37/[Link] d:/python/Coding/[Link]
Enter first integer:10
Enter second integer:2
Division: 5.0
********************************
Enter first float:apple
Please Enter valid input
PS D:\python\Coding>
output
-----------
Enter first integerapple
Please Enter valid input
PS D:\python\Coding>
output
----------
Outer try block is executed
Enter Mobile number:112233
Inner try block is executed:
Enter Mobile Price:53000.00
Inner finally block is executed
Outer finally block is executed
PS D:\python\Coding> & C:/Python37/[Link]
d:/python/Coding/Nester_try_except_finally.py
Outer try block is executed
Enter Mobile number:Arjit
Outer Except block is executed
Please Enter valid input
Outer finally block is executed
Traceback (most recent call last):
File "d:/python/Coding/Nester_try_except_finally.py", line 18, in <module>
del Mobile_number
NameError: name 'Mobile_number' is not defined
PS D:\python\Coding> & C:/Python37/[Link]
d:/python/Coding/Nester_try_except_finally.py
Outer try block is executed
Enter Mobile number:9337984334
Inner try block is executed:
Enter Mobile Price:apple
Inner except block is executed
Please Enter valid input
Inner finally block is executed
Outer Except block is executed
Please Enter valid input
Outer finally block is executed
by using del
------------------
>>> Fruit_Dictionary={1122:'Apple',2233:'Banana',3344:'pinaple'}
>>> del Fruit_Dictionary[1122]
>>> Fruit_Dictionary
{2233: 'Banana', 3344: 'pinaple'}
>>>
Employee_Details={'Employee_ID':1122,'Employee_Naame':'Arjit','Employee_Dept':'IT'}
>>> Employee_Details.items()
dict_items([('Employee_ID', 1122), ('Employee_Naame', 'Arjit'), ('Employee_Dept',
'IT')])
44)what is a Function ?
Ans:function is a set of instruction to perform some operation.
System-Defined function:
------------------------------------
Python provide some ready-made function to perform operation,these ready-made
function are called 'System-defined function'
Example of "system-defined-function:print(),type(),id
User-defined-function:
--------------------------------
Ans:
Based on the business requirment,programmer give new function to perform
operation,these new function are called "User-defined-function".
In Banking application:Deposit(),Transfer(),cheque()
System-Defined Exception
=======================
>python provide some built-in Exception class,This Exception is also called as pre-
defined Excption.
These pre-defined Exception are called System-Defined Exception.
Ex:ValueError,ZeroDivisionError,NameError
Ex
---
try:
i=int(input('Enter the first integer'))
j=int(input('Enter Seccond Integer'))
print('Division:',i/j)
print('****************************')
a=float(input('Enter first float value'))
b=float(input('Enter second float value'))
print('Multiplication',a*b)
except ZeroDivisionError:
print('Division by zero is not possible')
except ValueError:
print('Please Enter valid input')
output
---------
Enter the first integer10
Enter Seccond Integer0
Division by zero is not possible
>>>
=============== RESTART: D:\python\Coding\[Link] ===============
Enter the first integer10
Enter Seccond Integer2
Division: 5.0
****************************
Enter first float valueString
Please Enter valid input
User-Defined-Exception
=======================
Based on the application requirment,programmer give new Exception
this type of Exception are called User-Defined EXception
Example of user-defined
Exception:ValueNotFoundException,TooldException,ToyoungException
Ex
----
Enter your age18
you will get match detail by your email
>>>
===================== RESTART: D:\python\Coding\[Link] =====================
Enter your age19
you will get match detail by your email
>>>
===================== RESTART: D:\python\Coding\[Link] =====================
Enter your age65
Traceback (most recent call last):
File "D:\python\Coding\[Link]", line 11, in <module>
raise ToOldException('You to old to getting marriage')
ToOldException: You to old to getting marriage
50)in how many ways a function can be returned?
Ans
output
---------
>>> marks=[91,92,89,76,56,67]
>>> avg(marks)
78.5
Ex
---
def deposit(accoount_number,account_type,Deposit_amount):
if accoount_number==11223344 and account_type=='Saving':
account_balance=500000
account_balance=account_balance+Deposit_amount
return account_balance
else:
return ('Please Enter valid account Details')
output
--------------
>>> deposit(11223344,'Saving',12000)
512000
Ex
-----
def deposit(accoount_number,account_type,Deposit_amount):
if accoount_number==11223344 and account_type=='Saving':
account_balance=500000
account_balance=account_balance+Deposit_amount
print('Account Balance',account_balance)
else:
return ('Please Enter valid account Details')
output
-------------
>>> deposit(11223344,'current',1200)
'Please Enter valid account Details'
>>> deposit(11223344,'Saving',2000)
Account Balance 502000
Ex
----
def deposit():
account_number=int(input('Enter account Number:'))
account_type=input('Enter account type:')
Withdraw_amount=float(input('Enter Withdraw amount'))
if account_number==1122334455 and account_type=='Saving':
account_balance=500000
account_balance=account_balance-Withdraw_amount
return account_balance
else:
return ('Please enter valid account details')
output
---------
>>> deposit()
Enter account Number:1122334455
Enter account type:Saving
Enter Withdraw amount12000
488000.0
Text file
------------
Text store the
1)Alphabet(a-z)
2)Number(0-9)
3)symbols(@#$)
->Text file is human understandable file
Binary file
-----------------
Audio,video,images and .Exe files are Binary file
61)what is a directory?
Ans:
directory contains the file.
output
----------
>>> marks=[91,92,93,94,56]
>>> Collection(marks)
426
Ex-2
----------
def Collection(tuple):
sum=0
for i in tuple:
sum=sum+i
print(sum)
output
-----------
>>> mmarks=(90,78,87,76,59)
>>> Collection(mmarks)
390
65)What are the diffrent type of parameter what are the function parametrs?
The following are the diffrent type of parameter
1)Default parameter
-----------------------------
When user does not pass any value then default value will be used.
Ex
----
def Arithmetic(a=10,b=20,c=30,d=40):
print('Addition',a+b+c+d)
output
----------
>>> Arithmetic()
Addition 100
>>> Arithmetic(100,200,300,400)
Addition 1000
Non-Default parameter:
----------------------------------
def Arithmetic(a,b=20,c=30,d=40):
print('Addition',a+b+c+d)
Ex
----
>>> Arithmetic(100)
Addition 190
Keyword Parameter
---------------------------------
Programmer can use parameter name as keyword at the time of function calling.
Ex
----
output
---------
def PersonDetails(Employee_ID,Employee_name,Employee_email,Employee_salary):
print('Dear Employee your ID:',Employee_ID,'your Name:',Employee_name,'your
Email:',Employee_email,'Employee_salary:',Employee_salary)
Arbitary parameter:
----------------------------
Arbitary paraameter is also called as variable length parameter
when user pass n number value to the variable then,we prefer aribitaary parameter.
Ex
---
def Addition(*n):
sum=0
for i in n:
sum=sum+i
print(sum)
output
---------
>>> Addition(1,2,3,4,5,6,7,8,9)
45
66)How to return the multiple values from a function?
Ans:
A function can return the values of another function by Handling function
A function can return the multiple values as possible
EX
-----
def AO(a,b):
return a+b,a-b,a*b,a/b
def display():
print(AO(20,2))
output
---------
>>> display()
(22, 18, 40, 10.0)
67)Diffrence between global variable and local variable?
Ans:
Global variable
-------------------
>Gobal variable accesed throughout the function
>Global variable Decalred Outside of the function
Global_Variable=1000
def function1():
print("In function()1,Global variable=",Global_Variable)
def function2():
print("In function()2,Global variable=",Global_Variable)
output
-------------
>>> function1
<function function1 at 0x0000014FAF82C8B8>
>>> function1()
In function()1,Global variable= 1000
>>> function2()
In function()2,Global variable= 1000
Local Variable
--------------------
Local variable declared within the function.
Local variable accesed within the function.
Ex
----
def Addition():
a=10
b=20
print('Addition:',a+b)
def Substraction():
print('Substraction:',a-b)
output
---------
>>> Addition()
Addition: 30
>>> Substraction()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
Substraction()
File "D:\python\Coding\[Link]", line 7, in Substraction
print('Substraction:',a-b)
NameError: name 'a' is not defined
output
------------
>>> marks={'Arjit':98,'Biswajit':67,'Ram':43,'Syam':89}
>>> Collection(marks)
Arjit
Biswajit
Ram
Syam
even bigger function can written in single statement by using lambda function
Ex
----
>>> sum=lambda a,b:a+b
>>> sum(100,200)
300
filter()
--------------
filter() is used to filter the required data froma group a of element.
Ex
---
>>> list1=[0,1,2,3,4,5,6,7,8]
>>> even=list(filter(lambda x:x%2==0,list1))
>>> even
[0, 2, 4, 6, 8]
map()
----------
mapping the same functionality to all element of the list
>>> list1=[100,200,300,400]
>>> list2=list(map(lambda x:x*x,list1))
>>> list2
[10000, 40000, 90000, 160000]
reduce()
------------
reduce multiple element into single element in a list
Ex
-----