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

Python Question Bank

Uploaded by

payeja1730
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)
23 views

Python Question Bank

Uploaded by

payeja1730
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/ 8

PYTHON QUESTION BANK

1. What is dictionary, properties of dictionary and declaration of


dictionary.
• Pyhton provides another datatype called dictionary. It is used to store the data in a key
value pair format
➢ Properties of dictionary:
To represent a group of key values we will use dictionary data type
We will use a duplicate value but we cant use duplicate keys
Slicing concept are not allowed
For both the keys and values heterogenous objects are allowed
Dictionary is a mutable datatype
Dictionary is dynamic in nature
If we try to use duplicate keys in the dictionary than the old values will be replaced with
the new values
➢ Declaration of dictionary:
We can declare in 2 ways:
• First method:Declaration using curly brackets, in this we can create a dictionary by
placing a sequence of elements with in curly braces separated by comma and each key
is separated its values by the colon.
Syntax-variable name={<key>:<value>,<key>:<value>…}
Eg- a={1:’computer’, 2:’science’, 3:’engineering’}
Where ‘a’ is a dictionary is variable 1,2,3 is a keys, computer, science,
engineering is the value
• Second method : Declaration usuing dict() function, you can construct a dictionary
using dict() function, the argument to dict() function should be sequence of key value
pairs
Syntax- variable name= dict([(<key>, <value>),(<key>,<value>)…])
Eg- a= dict([(1,’computer’),(2, science),(3, engineering)])
2. Explain basic operations of dictionary.
a. Accessing elements from a dictionary
• A value is retrived from a dictionary by specifying its corresponding key in square
bracket.
• There is also a method called get() method that will help in accessing the elements
from a dictionary.
• Eg: 1. a={1:’computer’,2:’science’,3:’engineering’}
Print(a[‘2’])
2. a=[1:’computer’,’name’:’for’,3:’science’}
Print(a.get(3))
//by using get() method
b. Membership operator in dictionary
• Membership operator checks for the only key and returns true or false depending
on the condition
• Eg: a=[1:’computer’,2:’science’,3:’engineering’]
Print(2 in a)
Print(4 in a)
Cuncatination and repeation operators:
Dictionary datatype doesnot support cuncatination and repeatation operators.
c. Equality operators
The dictionary will return true only when the content of the two dictionaries are
same.
Eg:
dict1={a:123,b:456}
dict2={c:458,d:789}
dict3={a:123,b:456}
x=dict1==dict2
y=dict==dict3
print(x)
print(y)
3. List and explain the type of indexing in dictionary
➢ Indexing
Python dictionary object provides key value indexing facility. They are:
➢ Python dictionary index:
Dictionary element provide key value indexing where the order is preserved use the
indexing syntax list [index] to return the key and use the item() function to return a
collection of all dictionary key value pairs.
Eg:
my_dict={}
my_dict[‘l’]=4
my_dict[‘m’]=3
my_dict[‘n’]=2

new_value=list(my_dict.items())

print(“dictionary content at index 2”,new_value[2])


➢ python dictionary index of key:
by using list (args) with a dictionary it will return a collection od keys we can use the index
to access the required key the method and convert into a list.
Eg: my_dict={‘m’:1,’l’:2}
New_dict=list(my_dict)
Index_key=new_key[0]
Print(index_key)
➢ python dictionary index value:
python dictionary will return the collection of all the values by using values function ().
We can use the indexing syntax list [index] to return the key element and values function
used to return values of a particular key element.
Eg: my_dict={‘m’:1,’l’:2}
new_dict=list(my_dict values())
print(“indexing number of specific values”,new_list[1])
4. Explain how to add elements to a dictionary
➢ Adding elements in a dictionary
the dictionary is a mutable datatype and its values can be applied by using the specific key.
The addition of elements can be done in multiple ways. One value at a time ca be added to
a dictionary by defining values along with the key.
Eg:
#adding dictionary elements
dict{}
print(“employee dictionary”)
print(dict)
dict[0]=’peter’
dict[2]=’joseph’
dict[‘name’]=’rocky’
print(“after adding 3 element”)
print(dict)
element in a dictionary are removed by using del keyword using a del keyword specific
values from a dictionary as well as the whole dictionary can be deleted
eg: employee={“name”:”jhone”,”age”:20,”salary”:25000,”company”:”wipro”}
print(employee)
del employee[‘name’]
del employee[‘salary’]
print(employee)

5. What is an array, Features of an array, declaration of an array?

• Array is defined as a collection of items that are stored at continuous memory locations
and these items should be same datatype
• The terms used in an array:
• Element: each element stored in an array is called an element
• Index: the location of an element in an array as a numerical index which is used to identify
the position of an element
➢ Features of an array

• An array is a derived data type which is defined using basic datatypes like int, char, float
and even structures
• Array elements are stored in continuous memory blocks in primary memory
• Array name represent its base address(i.e. the address of the starting element)
• An array can be declared in various ways in different languages and its index starts with
zero, if the length of the array defines the capacity to store the elements

➢ Declaration of an array

• Array in python are created by importing array module


• Array(datatype, value-list) is used to create an array with the datatype and value-list
specified in its arguments.
Syntax:
from array import*
array_name=array(type code,[initializers]
eg:
import array as arr
a=arr.array(‘I’[1,2,3,4])
#type code
Long int-> i
Short int->I
character->c
double->d
float->f

6. Illustrate array manipulation using a program


➢ import array as ar
num=ar.array('i',[1,2,3])
length=len(num)
print(length)
num.append(4)
print(num)
num.extend([5,6,7])
print(num)
num.insert(1,9)
for x in num:
print(x)
num.remove(9)
for x in num:
print(x)

7. Illustrate iteration on dictionary with a program


my_index={}
my_index['A']=3
my_index['V']=7
my_index['M']=6
new_values=list(my_index.values())
print("index number is",new_values[1])
employee={"name":"john","age":20,"salary":25000,"company":"wipro"}
for x in employee:
print(x)
print(employee[x])

8. Build a python program for deleting an element using del() and pop()
method in dictionary.
➢ del()
employee={"name":"john","age":20,"salary":25000,"company":"wipro"}
del employee['name']
del employee['salary']
print(employee)
➢ pop()
employee={"name":"john","age":20,"salary":25000,"company":"wipro"}
for x in employee:
print(x)
print(employee[x])

9. List and explain string operators

Operators Description

+ It is also known as concatenation operator used to join the strings.

* It is known as repetition operator.

[] Slice operator : It is used to access substring.

[:] It is known as range slice operator used to access the characters


from a specified range.

IN It is known as a membership operator, it return true if a particular


sub-string is present.

% It is used to perform string formatting

10.Illustrate anonymous function with a program


def cube(y):
return y*y*y
lambda_cube=lambda y:y*y*y
print("using def function", cube(5))
print("using lambda function",lambda_cube(5))

11.Illustrate string operation with a program.

message="Hello World!"

message1="hello"+""+"world!"

print(message1)
message2="hello"*3

print(message2)

message3="hello"+""+"world!"

print(len(message3))

message4="Hello world!"

message4a=message4.find("world")

print(message4a)

message5="HELLO"

message5a=message5.lower()

print(message5a)

message6="HELLO"

message6a=message6.replace("L","hi")

print(message6a)

message7="hello world"

message7a=message[1:8]

print(message7a)

print("hello\t hello\t hello \n world")message="Hello World!"

You might also like