Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 60

1. Explain strings and operations on string type.

(slicing, indexing, stride,


etc..)
2)What is known as Mutability? Which are mutable types?
An object whose internal state (data) can be changed is called a Mutable
object. Similarly, an object whose internal state (data) can not be changed is
called an Immutable object.
Mutable
 Lists
 Sets
 Dictionaries
Since lists, sets & dictionaries are mutable, we can add, delete & modify its
contents. In all the three examples below, the memory location did not get
changed as expected during the operation. This confirms that lists, sets &
dictionaries are mutable objects.
For eg:1
my_list = [1,2,3]
print(my_list)
print(id(my_list))
>>> [1,2,3]
>>> 1863010210504
For eg:2
my_set = {1,2,3}
print(my_set)
print(id(my_set))
>>> {1, 2, 3}
>>> 2688892018248
For eg:3
my_dict = {‘key1’: 10, ‘key2’: 20, ‘key3’:30}
print(my_dict)
print(id(my_dict))
>>> {'key1': 10, 'key2': 20, 'key3': 30}
>>> 2688890813480
3)Differentiate Strings, Tuples, Lists and Dictionaries.
Q4 List out operations on Strings, Tuples, Lists and Dictionaries.
Q-5 Explain Membership Operators with examples.

Q-6 Explain Identity Operators with examples.

Q-7 Data types of Python


Q-8 Explain branching in the context of python. (if, else, else..if,break)
UNIT-2
Q-10 - Discuss control structure in context of python. (for, while).
1. Q-12 Explain Argument passing methods in function. (Positional
Arguments/Keyword Arguments/Default Arguments/Variable length
arguments).
Positional Arguments
Output
Hello Jack
What is going on?
Hello Jill
Howdy?
Python Default Arguments

Function arguments can have default values in Python. The default


arguments are parameters in a function definition that have a
predefined value assigned to them.
These default values are used when an argument for that parameter is
not provided during the function call.
Output:1100

Variable Length Argument in Python


Variable-length arguments refer to a feature that allows a function to accept a
variable number of arguments in Python. It is also known as the argument that
can also accept an unlimited amount of data as input inside the function.
Eg:
def my_min(*args):
result = args[0]
for num in args:
if num < result:
result = num
return result
my_min(4, 5, 6, 7, 2)
output: 2
Q-13 Discuss scoping in terms of functions with sample code. (local/Global)
Python Local Variables
When we declare variables inside a function, these variables will have a local
scope (within the function). We cannot access them outside the function.
These types of variables are called local variables. For example,
def greet():
# local variable
message = 'Hello'
print('Local', message)
greet()
print(message)
Output:-
Local Hello
NameError: name 'message' is not defined

Python Global Variables


In Python, a variable declared outside of the function or in global scope is known
as a global variable. This means that a global variable can be accessed inside or
outside of the function.
Let's see an example of how a global variable is created in Python.
# declare global variable
message = 'Hello'
def greet():
# declare local variable
print('Local', message)
greet()
print('Global', message)
Output
Local Hello
Global Hello

Q-14 Discuss recursion with an example.


ANS - Recursion is a method of solving problems where you solve smaller portions
of the problem until you solve the original, larger problem. A method or function
is recursive if it can call itself.

Q15 What is Module? Give a detailed description of it with an example.


A module is a file with the extension .py and contains executable Python or C
code. A module contains several Python statements and expressions. We can use
pre-defined variables, functions, and classes with the help of modules. This saves
a lot of developing time and provides reusability of code. Most modules are
designed to be concise, unambiguous, and are meant to solve specific problems of
developers.
Types of Modules in Python:
1. Built-in Modules in Python
2. User-defined Modules in Python
Importing Modules in Python:
1. Using Keyword: import
Eg:
import hello
2.Importing Multiple objects in Python
Eg:
import math
print(math.log2(2))
print(math.sqrt(16))
print(math.pow(2,4))
Output
1.0
4.0
16.0
3.Importing Multiple Modules in Python
Eg:
import hello, math, random
hello.greet()
print(math.sqrt(4))
print(random.randint(1, 2)) #generates a random number
Output
Hello! Nice to meet you
2.0
2
Q16: Differentiate following functions. (1) lambda (2) map (3) filter.
Q17Explain higher order functions supported by python.
Higher Order Function
if it contains other functions as a parameter or returns a function as an output i.e,
the functions that operate with another function are known as Higher order
Functions. It is worth knowing that this higher order function is applicable for
functions and methods as well that takes functions as a parameter or returns a
function as a result. Python too supports the concepts of higher order functions.
1)Functions as objects
In Python, a function can be assigned to a variable. This assignment does not call
the function, instead a reference to that function is created. Consider the below
example,
# Python program to illustrate functions
# can be treated as objects
def shout(text):
return text.upper()

print(shout('Hello'))
# Assigning function to a variable
yell = shout
print(yell('Hello'))
Output:

HELLO
HELLO

2) Passing Function as an argument to other function


Functions are like objects in Python, therefore, they can be passed as argument to other
functions. Consider the below example, where we have created a function greet which takes a
function as an argument.
# Python program to illustrate functions
# can be passed as arguments to other functions
def shout(text):
return text.upper()

def whisper(text):
return text.lower()

def greet(func):

# storing the function in a variable


greeting = func("Hi, I am created by a function \
passed as an argument.")
print(greeting)
greet(shout)
greet(whisper)
Output:
HI, I AM CREATED BY A FUNCTION PASSED AS AN ARGUMENT.
hi, i am created by a function passed as an argument.
3) Returning function

As functions are objects, we can also return a function from another function. In the below
example, the create_adder function returns adder function.
Example:
# Python program to illustrate functions
# Functions can return another function

def create_adder(x):
def adder(y):
return x + y

return adder

add_15 = create_adder(15)

print(add_15(10))
Output:

25

Q18 Sorting method


Q19 Searching method
Unit 3
Q20 Describe methods to create an array using numpy.
NumPy
NumPy (Numeric Python) is a Python library to manipulate arrays. Almost all the
libraries in python rely on NumPy as one of their main building block. NumPy
provides functions for domains like Algebra, Fourier transform etc.. NumPy is
incredibly fast as it has bindings to C libraries. Install :
conda install numpy
pip install numpy
for Ubuntu
sudo apt install python3-numpy
Q21How to use the reshape method in numpy? Explain with examples.
Q22 Apply arithmetic operators, relational operators and logical operators on
vectors using numpy.

Logical Operators in NumPy


‘or’ operator with NumPy Arrays

Relational Operators:
Q23Briefly explain the matrix module.
A matrix is a collection of numbers arranged in a rectangular array in
rows and columns. In the fields of engineering, physics, statistics, and
graphics, matrices are widely used to express picture rotations and
other types of transformations.
The matrix is referred to as an m by n matrix, denoted by the
symbol “m x n” if there are m rows and n columns.
Accessing Value in a matrix
Method 1: Accessing Matrix values
Eg:
print("Matrix at 1 row and 3 column=", X[0][2])
print("Matrix at 3 row and 3 column=", X[2][2])
Output:
Matrix at 1 row and 3 column= 3
Matrix at 3 row and 3 column= 9

Method 2: Accessing Matrix values using negative indexing


Eg:
import numpy as np
X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(X[-1][-2])
Output:
8
Output:
[10, 10, 10]
[10, 10, 10]
[10, 10, 10]
Output:
Matrix Addition
[10, 10, 10]
[10, 10, 10]
[10, 10, 10]

Matrix Subtraction
[-8, -6, -4]
[-2, 0, 2]
[4, 6, 8]

Q24: Explain generation of random numbers, all zero valued matrices,


all ones valued matrices, identity matrix.
Q25:Draw plot for given data using Pylab/ matplotlib and numpy.
Q26:List and explain methods supported by tkinter to place/arrange
various widgets in the window.
Q27:Explain steps to create widgets. Write Python program to display
a label on clicking a button using tkinter.(any widgets)
Same as above ans 26
Q28:Write a code to draw a rectangle with border color of blue and
inner filling of color yellow using turtle.
Q29:List and Explain Methods supported by turtle module.
Q30:Write a code to draw a circle, star, square, triangle using turtle
etc.
Rectangle:
# draw Rectangle in Python Turtle
import turtle
t = turtle.Turtle()
l = int(input("Enter the length of the Rectangle: "))
w = int(input("Enter the width of the Rectangle: "))
# drawing first side
t.forward(l) # Forward turtle by l units
t.left(90) # Turn turtle by 90 degree
# drawing second side
t.forward(w) # Forward turtle by w units
t.left(90) # Turn turtle by 90 degree
# drawing third side
t.forward(l) # Forward turtle by l units
t.left(90) # Turn turtle by 90 degree
# drawing fourth side
t.forward(w) # Forward turtle by w units
t.left(90) # Turn turtle by 90 degree

Triangle:
Q32 Implement following inheritance:
(1)Single (2) Multiple (3) Multilevel (4) Hybrid
Q34: Demonstrate Overriding and methods to overcome.
Method overriding is an ability of any object-oriented programming language that
allows a subclass or child class to provide a specific implementation of a method
that is already provided by one of its super-classes or parent classes. When a
method in a subclass has the same name, the same parameters or signature, and
same return type(or sub-type) as a method in its super-class, then the method in
the subclass is said to override the method in the super-class.
Q35: Explain Abstract method
Q36: Explain Encapsulation - Public, Private , Protected mode.

You might also like