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

Python Material Chapter-1-2024

Uploaded by

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

Python Material Chapter-1-2024

Uploaded by

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

SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.

(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

B.C.A.&B.Sc.I.T.– SEMESTER - 5
SUB :- CS – 33 :- PROGRAMMING IN PYTHON

Shree H.N.Shukla College Shree H.N.Shukla College


Street No. 2, Vaishali Nagar, Street No. 3, Vaishali Nagar,
Nr. Amrapali Under Bridge, Nr. Amrapali Under Bridge,
Raiya Road, Rajkot. Raiya Road, Rajkot.
Ph. (0281)2440478 Ph. (0281)2471645

1
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

CS – 33 : Programming in Python –B.C.A. & B.Sc.I.T. –SEM -5

Sr.No. Topic Detail


1 Introduction to  Basic Element of Python
Python  Branching Programs
 String and Input
 Iteration
 Function and Scoping
 Specifications
 Recursion
 Global Variables
 Modules
 Files
 Tuples
 List & Mutability
 Functions as Object
 Strings
 Dictionaries

1
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

2 OOP Handling Exception


using Exception as Control Flow
Python Assertion
Abstract Data Type
Class
Inheritance
Encapsulation
Information hiding
Search Algorithm
Sorting Algorithm
Hash table

2
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

3 Plotting using PyLab  Plotting using PyLab


 Plotting Mortgages
 Extended Example
 Fibonacci Sequence Revisited
 Dynamic Programming
 0/1 Knapsack Algorithm
 Dynamic Programming with Divide and
Conquer
4 Network  Network Programming:
Programmin  Protocol, Sockets,
g and GUI  Knowing IP Address,
using Python  URL, Reading the Source Code of a
Web Page,
 Downloading a Web Page from Internet,
 Downloading an Image from Internet,
 A TCP/IP Server, A TCP/IP Client,
 A UDP Server, A UDP Client,
 File Server, File Client,
 Two-Way Communication between
Server and Client,
 Sending a Simple Mail.
 GUI Programming:
 Event-driven programming paradigm;
 creating simple GUI;
 buttons, labels, entry fields, dialogs;
 o widget attributes - sizes, fonts, colors
,layouts, nested frames

3
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

5 Connecting  Verifying the MySQL dB Interface


with Installation,
Database  Working with MySQL Database,
 Using MySQL from Python,
 Retrieving All Rows from a Table,
 Inserting Rows into a Table,
 Deleting Rows from a Table,
 Updating Rows in a Table,
 Creating Database Tables through
Python

4
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

CHAPTER-1
Introduction to Python
 Basic Element of Python
 Branching Programs
 String and Input
 Iteration
 Function
 Scoping
 Specifications
 Recursion
 Global Variables
 Modules
 Files
 Tuples
 List & Mutability
 Functions as Object
 Strings
 Dictionaries

5
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Q-1 What is Python ? Explain Basic Elements of Python :

Detail :-
 Python is popular programming language.
 It was created in 1991 by Guido Van Rossum.
 It is used for :
o Web Development (server side)
o Software Development
o System Scripting
o Mathematics
 Python support following elements to perform perfect programming:

1. Data Type :
 Python support integer and float data type to hold numbers.
 Python interpreter can produce the result of numeric values.

6
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

2. Variable :
 Unlike other programming language , python have no command
for declaring variable.
 You can create variable by assigning value directly to it.
o EX :- x = 50
Name = “hello”

3. Syntax :
 Python syntax can be executed by writing directly at the
command line like,
 >>> print (“hello”)

4. String :-
 String is a collection of different characters.
 You can write string in signle quotes(‘ ‘) as well as in
double quotes (“ “).
o EX :- a=
‘hello’ b=” “

5. Tuples :-
 In the case of tuples , it is collection of different elements and
values supported by python data types.
 Tuples are enclosed in round brackets ().
o EX :- a = (‘abc’ , ‘jkl’ , ‘xyz’,18)
o
6. List :-
 In the case of , It is collection of element or values supported by
python data types.
 List are enclosed in square brackets ().
o EX :- a = [1,2,3,4,5]

7. Dictionary :-

7
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

 A Dictionary is a collection which is unordered , changeable and


indexed.
 Dictionary can be enclosed in curly brackets { } with key &
value pair.
o EX :- dict = {name = “snehal” , surname = “pandya”}

8. Operators :-
 An operators are used to perform operations on variable or value.
 Python support following operators :
o Arithmatic
o Assignment
o Comparison
o Logical
o Membership
o Bitwise
o Identity

1 Word Question – Answer

O. QUESTION ANSWER

1 Python was developed by_____ Guido Van Rossum

Open source
2 Python is ___________ language.

8
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Q-2How to input and output value in the python:

Detail :-
 In python programming user can input the data as well as get the output as a
result of data.

How to input value :


 To input value python provide one of the built – in function named
input().
 You can ask user to input particular value from userside.
 You can call input () and wait for user to enter the data.
 In python2 you can use row_input() to get value from userside , while
in python3 you have input() to get value from user.
o EX :- num = input (“enter number”)
name = input(“enter name”)

How to ouput / print value :


 Python provide print() to get output to the file.

9
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

 Print() support message of the user as well as value of variable they


want to print.
o EX :- a=5
>>> print (a)
Output :- 5
o EX :- b=10
>>> print (“value of b is =” ,b)
Output :- value of b is = 10

1 Word Question – Answer

SR. QUESTION ANSWER


N O.
1 To input value in the python Input()
function can be used.
2 To print output on the screen _ Databse Access
function can be used.
3 Python prompt can be represented by >>>
_

Q-3Expalin Braching Statements in python:

10
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Detail :-
 If statement support generally the logical expression.
 In this statement first of all we have to give condition.
 If condition become true then statement following if will be execute
otherwise condition will be terminated.
 Syntax :-
If (<condition>):
<statement>

 Example :-
A=10
If(A>0):
Print(‘yes’)

11
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

If ….. Else :-
 In this statement first of all condition will be check.
 If condition become true then statement following if will be execute.
 But if condition become false then statement following else will be execute.

 Syntax :-
If (<condition>):
<statement>
Else:
<statement>

 Example :-
A=10
If(A>0):
Print(‘yes’)
Else:
Print(‘no’)

12
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Elif :-
 The elif statement is used to support multiple conditions at the same time.
 At a time only one condition will become true.
 In this case if no any condition become true then finally the statement
following else will be execute.

 Syntax :-
If (<condition>):
<statement>
Elif(<condition>):
<statement>
Elif(<condition>):
<statement>
Else:
<statement>

13
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

 Example :-
a=10,b=20,c=30
if(a>b and a>c):
print(‘a is max’)
elif(b>a and b>c):
print(‘b is max’)
else:
print(‘c is max’)

1 Word Question – Answer

SR.NO QUESTION ANSWER


.
1 Python provide one of the important If statement
branching statement that is .
2 In if statement the condition is followed by Colon(:)
.
3 If condition become false then statement Else :
followed by _ will be execute.
4 _ statement can be used to elif
support multiple conditions at the same time.

Q-4 Explain String in Python.

14
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Detail :-
 Pythondoesnotsupportcharactertype.
 String is mostpopular datatype in python.
 Stringis acollectionof differentcharacterandsymbols.
 We cancreate or declare astring by enclosing “ “(Double Quotes) aswellas ‘
‘Single Quotes
forthestatements.
 Let’ssee, how to declare astringvalue in python:
o Example:- var1 =“hello”

How to Access Characters from particular String :-


 We can accessdifferentcharactersfromgivenstring using indexing.
 Inpython stringindex willstart from 0(zero).
 We can notdelete or removethecharacters fromthestringbut deletingentire string is
possible using del keyword.
o Example:- var1 =“helloworld”
Var2 =“ pythonprogramming”
Print (“var1[0]:”, var1[0]) output:- h
Print(“var2[1:5]”,var2[1:5]) output:- ytho

15
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

How to Concatenate ( join) two strings :-

 To join or concatenatetwo strings in python , (+) plusoperatorcan be used.


o Example:- var1 =“hello ”
Var2 =“ pythonprogramming”
Print(var1 +var2) output:- hellopythonprogramming

How to update existing string :-


 You can "update" an existing string by (re)assigning a variable to another
string.
 The new value can be related to its previous value or to a completely
different string altogether.
o Example :- var1 = ‘hello world’
Print (‘updated string:-‘ , var1[:6] + ‘python’)
output : hello python

1 Word Question – Answer

SR.N QUESTION ANSWER


O.
1 In python ,String can be represented by Single quotes & double
quotes
2 To access substring ,we have to use : (Colon)
index number with _ Operator.

Q-5 Explain String Operators in Python.

16
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

17
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Assume string variable a holds 'Hello' and variable b holds 'Python', then −

Operato Descriptio Example


r n
+ Concatenation - Adds values on either side of a + b will
the operator give
HelloPython
* Repetition - Creates new strings, concatenating a*2 will give
multiple copies of the same string - HelloHello
[] Slice - Gives the character from the given index a[1] will give e
[:] Range Slice - Gives the characters from the a[1:4] will
given range give ell
in Membership - Returns true if a character exists H in a will give
in the given string 1
not in Membership - Returns true if a character does M not in a
not exist in the given string will give 1

Detail :-
 The string operators can be used to perform different types of operations on
the string.
 There are three types of string operators supported by python.
o Basic Operator
o Membership Operator

Basic Operator:-
 String operator support two types of basic operators :
(i) Concatenate Operator :-
o The concatenate operator can be used to combines two t wo or more
string Values.

18
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

o Plus(+) operator can be used as concatenate operator in python.


o Example :-
>>> “hello” + “hi”
Output :- Hellohi

(ii) Replication Operator :-


o The Replication operator (*) is used to repeat particular
string , character or symbol for given number of time .
o In this operator we have to give one integer paramter
and string value.
o Example :-
>>> 5 * “hi ”
Output :- hi hi hi hi hi
Membership Operator:-
 Membership operator is used to indicate possibilities of available members.
 There are two types of membership operators:
(i) in Operator :-

o The in operator returns true if particular character or


string available in the given string otherwise false.

(ii) Not in Operator :-


o This operator returns true if particular character or string does
not exist in given string otherwise returnfalse.

19
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
o Example :-
>>> str1 = “java programming”
>>> str2 = “HNS IT”
>>> str3 = “SEO Material”
>>> str4 = “java”
>>> str5 = “IT”
>>> str4 in str1 Output :- True
>>> str5 in str2 Output :- False
>>> str4 not in str1 Output :- False

1 Word Question – Answer


SR.NO QUESTION ANSWER
.
1 string operator can be used for +
concatenation of multiple strings or words
2 string operator can be used for *
repetition of multiple strings or characters
3 string operator can be used to return :
characters from given range.
4 & are known as membership In & not in
operators.

Q- 6 Write note on Iteration OR Looping Statements.

20
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Detail :-
 Generally the statements are executed sequentially.
 When user want to execute group of the statements at that time looping
statements are used.
 The main use of loop is to repeat the statements for number of times.
 Python programming support following types of looping statements:

o While Loop
o For Loop
Loop Type Description
while loop Repeats a statement or group of statements while a
given condition is TRUE. It tests the condition before
executing the loop body.
for loop Executes a sequence of statements multiple times
and abbreviates the code that manages the loop
variable.

While Loop :-
 While loop is known as entry – control loop.
 In this loop first of all condition will be checked and then after statement will
be execute.
 If condition become true then the statement following while will be
execute otherwise loop will be terminated.

o Syntax :-
<initialization>
While(<condition>):
<statement>
<increment / decrement>

21
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

o Example :-
i=1

while(i<=10):
Print(i)
i=i+1

 For Loop :-
 For loop is used to execute block of the code for given number of times.
 First of all condition will be check and then after statement will be execute.
 For loop will iterate for the particular collection or list items.

o Syntax :-
For <variable> in <sequence>:
<statement>

o Example
:-i=1
fruits = [‘apple’ , ‘banana’ , ‘mango’]

22
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

for i in fruits:
print(i)
 If a sequence contains an expression list, it is evaluated first.
 Then, the first item in the sequence is assigned to the iterating
variable iterating_var.
 Next, the statements block is executed.
 Each item in the list is assigned to iterating_var, and the statement(s)
block is executed until the entire sequence is exhausted.

23
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

 Nested Loop :-

24
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

 If you want to create one loop inside another loop then it is called
nested loop or nesting of loop.
 First of all outer loop will be execute first and then after inner loop
will be execute.
o Syntax :-
For <variable> in <sequence>:
<statements>
<statements>

o Example :-
i=1
for I in range(1,10):
for j in range(1,10):
print (j)
print()

1 Word Question – Answer


SR.NO QUESTION ANSWER
.
1 Python support & looping While & for
statements.
2 is loop inside body of another Nested loop
loop
3 In looping statement , the expression or : (Colon)
condition must be followed by .
4 Looping statements are also known as Iterative
statements
5 The loop inside another loop is called Inner loop
loop

25
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Q-7 What is Scoping ? Explain.

Detail :-

 Variable is acontainer thatuse to storedifferent values.


 Variable can onlyreachthearea in whichtheyaredefine , which is calledscope.
 Pythonsupporttwotypes of variable scopes:

o Localscope of variable
o Globalscope of variable

 Thescope of variable is used to decide placewhere you can access variable.


 If you define variable at the top level of your script or module then it is always global
variable.

 Local scope of variable :-

 If youdeclarevariable inside the functionthen it is called local scope.


 Local variable have limited scope , it can be accessed by only the function in
which it is declare.

o Example :-
def
my_function()
a=10
print(“a=” , a)
return
>>>my_func()
>>>print(a) # name error : name ‘a’ is not defined

 Global scope of variable :-

 If you declare variable at the top of your script or module then it is always global.

26
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

 Global variable can be easily access anywhere within your script.


o Example :-

My_var = 20
def my_function()
print(“a=” , a)
return
>>>print(a)
>>>my_function()

1 Word Question – Answer

SR.NO QUESTION ANSWER


.
1 In python , scope of variable can be _ Local & Global
& .
2 The variable that declare inside function block Local scope
and can be access within function is called
.
3 The variable that declare outside the function Global scope
and can be access anywhere in the script is
called

Q-7 What is Recursion ? Explain with example.

Detail :-
 Pythonprogramming supportrecursion as programmingconcept.
 Whenthefunctioncall itself againand again then it is calledrecursion.
 Recursionworklikealoop, you canconvert any loo to recursion.

27
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
 Recursive Function :-

 Recursive function is called byexternalcode.


 If the base condition is available then the program do something meaningful otherwise
exit.
 Functionhave to do somerequiredprocessingandthencall itself to continuerecursion.

o Example :-
#Factorial using
recursion
def fact(n):
if(n==0):
return 1
else:
return n*fact(n-1)
#calling function
Print(fact(0))
Print(fact(5))
 Nowtry to execute above function like afollowing:-
o Example:-
Print (fact(2000))

28
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

 Youwillgetfollowingerror at runtime:-
o Runtimeerror:- Maximumrecursion depthexceeded in comparison.

 The above error is available because python stop calling recursive function after 1000
calls by default.
 To change it you need to add following lines to starting of code.

Import Sys
Sys.setrecursionlimit(3000)
1 Word Question – Answer
SR.NO QUESTION ANSWER
1 When the function call itself recursion
again and againthen it is called .
2 function is called by external recursive

Q-8 What is Module ? Explain

29
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Detail :-
 Moduleallow you to logically organizeyourpythoncode.
 To grouprelatedcodeintothemodule makes thecodeeasier to understand and us d.
 Simplymodule is a file having thepythoncode.
 Modulecan define function, variable and class.
 Modulecanalso incuderunnable code.
o Example:-
[module – support.py]

Def print_func(x):
Print(“hello:”,x)

 Pythonprovidetwotypes of statements to load module:

30
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Import statement :-
 You can use any pyton file as a module by executing import statement in
other python file.
o Syntax :- import module 1 [,module 2 [,module N ] ]
 When interpreter find out import statement it import the module if module is
available.
o Example :- import module support
import support
Support.print_func(“zara”)
 Module is loaded only once , but number of times it will be imported.
 The module search the path which is stored in system module as Sys.path.

 From……import statement :-
 Python's from statement lets you import specific attributes from a module
into the current namespace.
 The from...import has the following syntax –

o Syntax :- from modname import name1[, name2[, ... nameN]]

 For example, to import the function fibonacci from the module fib, use the
following statement −
o Example :- from fib import Fibonacci

 This statement does not import the entire module fib into the current
namespace;
 it just introduces the item fibonacci from the module fib into the global
symbol table of the importing module.

 The from...import * Statement:-


 It is also possible to import all names from a module into the current
namespace by using the following import statement −

31
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

from modname import *


 This provides an easy way to import all the items from a module into the
current namespace.

Using the dir() Function


 There is a built-in function to list all the function names (or variable
names) in a module. The dir( ) function:
 module: import platform

o Example :-
x =
dir(platform)
print(x)
 Note: The dir() function can be used on all modules, also
the ones you create yourself.
1 Word Question – Answer
SR.NO QUESTION ANSWER
.
1 Module is a having python code. file
2 statement can be used to import import
code from the given module.
3 To import all the files from the particular Import *
module we can use

Q-9What is file? Explain How to open , read , write and close the file –[file
handling]

32
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Detail :-
 File is used to store related information permanently.
 When we want to read from or write to a file , we need to open it first.
 There are following file operations available:
o Open a file
o Read a file
o Write a file
o Close a file

Open a

33
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

 Python provide open() to open particular file.


 We have to provide file mode which can be read – ‘r’ , write – ‘r’ or append

– ‘a’.
 The default mode is reading mode.
 File Modes :-
(1) ‘R’ :- This file mode is used to open any file for reading purpose.
(2) ‘w :- This file mode is used to open any file for writing purpose.
(3) ‘a’ :- This file mode is used to open any file for appending data.

Example :- f = open (“text1.txt”)


f = open (“text2.txt”,”w”)

Write data to the file :-

 To write data from the file , we need to open it into write mode.
 To write data into file , write() can be used.

Example :- f = open (“text1.txt”, “w”)


f.write(“hello”)
print(“written successfully”)
f.close()

Read data from the file :-


 To read data from the file , we need too pen it into read mode.
 To read data from the file , read() is used.

Example :- f = open (“text1.txt”, “r”)


print (f.read())
f.close()

34
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

How to close file :-


 When we complete all the related operations the new need to close it
properly.
 To close any file , close() is used.
Example :-
f = open (“text1.txt”, “r”)
print (f.read())
f.close()

1 Word Question – Answer


QUESTION ANSWER
SR.NO

1 is used to store related imformations file


permanently.
2 can be used to open particular file Open()
3 can be used to read data from Read()
particular file.
4 can be used to write data to the Write()
particular file.
5 _ can be used to close particular Close()
file.

Q-10 Write note on tuple .

35
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

36
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Detail :-

 Tuple is a sequence of immutable python object.


 Tuple is very similar to the list but there may be following difference
between list and tuple.
o Tuple can not be change or update during execution.
o We have to use round brackets( ) to represent tuple.
o User can create empty tuple by representing empty brackets ( ).
o Creating tuple is as simple as representing different values
supported by comma ( , ).

 Example :-
T1=(‘a’,’b’,’c’,’d’,’e’)
T2=(1,2,3,4,5)
T3=(17,25,”hi”,”how”,4)

 How to access value from the tuple :-


 To access value from the tuple use square bracket [ ] with particular index
number or range.
 Example :-
T1=(‘a’,’b’,’c’,’d’,’e’)
Print (“t1[2]:” , t1[2])
o/p:- c

 How to update value in the tuple :-


 In tuple it is not possible to change the value during execution.
 But it is possible to merge more than one tuples at the same time.
 Example :-
T1=(‘a’,’b’,’c’,’d’,’
e’) T2=(1,2,3,4,5)
T3= T1 + T2
Print (T3) o/p :- a,b,c,d,e,1,2,3,4,5

37
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

 How to delete tuple :-


 To remove particular value of the tuple is not possible.
 So that we have to delete or remove entire tuple using del statement.
 Example :-
T1=(‘a’,’b’,’c’,’d’,’e’)
del (T1)
Print (T1)

1 Word Question – Answer


SR.NO QUESTION ANSWER
.
1 Tuple is object in python. immutable
2 Tuple can be represented by brackets. ( ) (round)
3 statement can be used to remove entire del
tuple.

Q-11 Write note on List.

38
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Detail:-
 Thelist is mostversatile datatype in python.
 List can be represented by squarebracket[] , separated by comma( , ).
 Inpython youcan createsimplelistlike following:

 Example :-
L1 = [“abc” , “xyz” , 2000 ,1999]
L2 = [1,2,3,4,5,6,7]

 How to access value from the List :-


 To access value from the List , use square bracket [ ] with particular
index number or range.

 Example :-
L1 = [“abc” , “xyz” , 2000 ,1999]
L2 = [1,2,3,4,5,6,7]
Print (“list1[0]:” , L1[0]) o/p :- abc
Print(“list2[1:5]:” , L2[1:5]) o/p :- 2,3,4,5

39
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

 How to update value in the List :-


 You can update single or multiple elements of list by assigning new
value from right to left.
Example :- L1 = [“abc” , “xyz” , 2000 ,1999]
Print (L1[2]) o/p :- 2000
L1[2] = 2005
Print (L1[2]) o/p :- 2005

 How to delete value in the List :-

 To remove list element , you can use del statement , if you know which
element you are going to delete.
 You can also use remove() , if you do not know which element you are
going to delete.

 Example :-
L1 = [“abc” , “xyz” , 2000 ,1999]
Print (L1[2])
del (L1[2])
Print (L1)
Del (L1)
Print(L1)
1 Word Question – Answer
SR.NO QUESTION ANSWER
.
1 List is object in python. mutable
2 List can be represented by [ ] (square )
brackets.
3 To remove particular element from the list del
statement can be used.

40
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Q-12 Write note on Dictionary.

Detail:-
 In dictionary we have to manageourdata by key and valuepair.
 In dictionaryeach key is separatedfromitsvalue usingcolon(: ).
 In dictionarythe key and value pairs are separated by comma(, ).
 Thekeys are always unique but valuescannot be.
 We can createourdictionaryusingcurlybrackets { }.

41
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
 Example :-
D1 = {‘name’ : ‘zara’ , ‘city’ : ‘rajkot’ , ‘age’ : 5)

 How to access value from dictionary :-


 To access value from the dictionary , we have to use square bracket with
particular key.

 Example :-
D1 = {‘name’ : ‘zara’ , ‘city’ : ‘rajkot’ , ‘age’ : 5)
Print (“D1[‘name’]:” , D1[‘name’]) o/p :- zara

 How to update value in dictionary :-


 You can update dictionary by adding new entry or by modifying existing
entry.

 Example :-
D1 = {‘name’ : ‘zara’ , ‘city’ : ‘rajkot’ , ‘age’ : 5)

D1[‘school’] = ‘G.T. Girls’ # adding new entry


Print (D1)
D1[‘age’] = 1 #modifying existingentry
Print(D1)

 How to delete value in the dictionary :-


 You can remove particular element from dictionary as well as you can
clear entire dictionary.
 To remove all the entries from dictionary , clear() is used.

 Example :-
 D1 = {‘name’ : ‘zara’ , ‘city’ : ‘rajkot’ , ‘age’ : 5)
del (D1 [‘name’]) # removing single entry
print(D1)
del (D1) # deleting entire dictionary
print(D1)
D1.clear() # remove all entries in dictionary Print(D1)

42
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

1 Word Question – Answer


SR.NO QUESTION ANSWER
.
1 Dictionay can be represented by _ { } (curly)
brackets.
2 In dictionary ,each key is separated from its : (colon)
values by .
3 Keys are always within dictionary. Unique
4 To remove an entire dictionary del
statement can be used.

Q-13 How to define function ? Explain.

43
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Detail:-

44
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

 Defining a Function :-
 Function blocks begin with the keyword def followed by the function name
and parentheses ( ( ) ).
 You can place any number of arguments inside the brackets().
 The code block of every function must start with colon( : ).
 The statement of the block must be exit with ‘return’ keyword.

o Syntax :-
def <function name> (parameters):
<block of code>
Return
Example :-
def sp():
Print(“hello sp”)
return
 Calling a Function :-
oIf you create your own function , then you can execute it by calling the
function with its name.
oYou have to take care about name of the function and argument of the function.
o Syntax :-
<function name> (parameters):
o Example :-
Sp()

45
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645
Passing parameters to function :-

oYou can pass multiple arguments by separating it with comma (,) .


owhile passing multiple arguments always take care about number of
arguments and types of arguments.

o Syntax :-
def <function name> (p1,p2,p3,…..,pn):
<block of code>
Print Return
Example :-
def sp(str1 ,str2):
(“hello sp”)
Print(str1)
Print(str2)
Return

46
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

1 Word Question – Answer

def
keyword can be used to define
function.
The code block within every function starts Function name &
with & followed by . colon(:)
Function can be called by just providing Function name
.
User can pass some values with function Parameters
which known as _
To return some values by function _ return
statement can be used.

Q-14 Explain Mutability in brief.

47
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

Detail :-
 Everything in python is an object.
 Python represent all it’s data as object.
 The mutability of object decided by its type.
 Some of the object like list and dictionary are mutable.
 Mutable means you can change the content without changing their
identity.
 Some other objects like tuple and string are immutable means that can
not be change.
 Variable in a python also support mutability , means if you call same
method with same variable can be muted anytime by other method.
 List object support mutability like following :

Example :-
My_list= [10,20,30]
Print (my_list)
My_list[0]=40
Print(my_list)

48
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)

2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Ph.No–(0281)2440478,2472590 Ph.No–(0281)2471645

 Unlike tuple , the list is mutable it means we can change the value by
assigning new value directly.

1 Word Question – Answer

SR.NO QUESTION ANSWER


.
1 is mutable object in python. List
2 is immutable object in python Tuple
3 means you can change the content Mutability
without changing their identity.

49

You might also like