0% found this document useful (0 votes)
48 views7 pages

Python Basics: Data Types & Operations

out based phython for class 12
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)
48 views7 pages

Python Basics: Data Types & Operations

out based phython for class 12
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

Chapter 1.

Python Revision Tour-I (2 Marks Questions)

(1) Distinguish between Dictionary and List

Dictionary List

[Link] collection of elements 1. Ordered collection of elements

2. Group of objects are not in any particular order 2. Elements are in particular order

(2) Distinguish between Keyword and Identifier

Keyword Identifier
1. Special word that has special [Link] defined name given to a part
meaning & is predefined of program
[Link] are reserved words [Link] are not reserved words

(3) Distinguish between String and List

Sting List
[Link] sequence & stores [Link] sequence & stores multiple
single type of elements type of elements
[Link] consecutive locations it starts [Link] references of its elements
individual characters

(4) Distinguish between Mutable Objects and Immutable Objects


Mutable Objects Immutable Objects

[Link] be changed after creation 1. cannot be changed after creation

[Link] you alter an object its id will be same 2. when you alter an object its id will change
OUTPUT FINDING QUESTIONS
Q1. Find output generated by the following code:
p=10 q=20
p*=q//3
q+=p
q=q**2
print(p, q)
Ans: 60 6400

Q2. Find output generated by the following code:


str="Computer"
print(str[-4:])
print(str*2)
Ans: uter
‘ComputerComputer’

Q3. Find out the output of the Following –


x=20
x=x+5
x=x-10
print (x)
x, y=x-1,50
print (x, y)
Ans: 15
14,50

Q4. Find output generated by the following code:


p=5/2
q=p*4
r=p+q
p+=p+q+r
q-=p+q*r
print(p,q,r)
Ans: (27.5 - 142.5 12.5)

Q5. Find output generated by the following code:


a=(2 + 3) ** 3 – 6 / 2
b=(2 + 3) * 5// 4 + (4 + 6) / 2
c=12 + ( 3 * 4 – 6 ) / 3
d=12 % 5 * 3 + (2 * 6) // 4
print(a, b, c, d)

Ans: (122.0 11.0 14.0 9)

QUESTIONS BASED ON TUPLE


Q1: Find the output of following codes
1. t1=("sun","mon","tue","wed")
a. print(t1[-1])
Ans: wed
Q 2. t6=('a','b')
t7=('p','q')
t8=t6+t7
print(t8*2)
Ans: ('a', 'b', 'p', 'q', 'a', 'b', 'p', 'q')

Q 3. t9=('a','b')
t10=('p','q')
t11=t9+t10
print(len(t11*2))
Ans: 8

Q 4. t12=('a','e','i','o','u')
p, q, r, s, t=t12
print("p= ",p)
print("s= ",s)
print("s + p", s + p)
Ans: p= a
s= o
s + p oa

Q5. t13=(10,20,30,40,50,60,70,80)
t14=(90,100,110,120)
t15=t13+t14
print(t15[0:12:3])
Ans: 10, 40, 70, 100

Q2. Find the errors


1. t1=(10,20,30,40,50,60,70,80)
t2=(90,100,110,120)
t3=t1*t2
Print(t5[0:12:3])
Ans: a. t1*t2 can not multiply
b. P is in uppercase in print command
c. t5 is not defined
2. t1=(10,20,30,40,50,60,70,80)
i=[Link]()
Print(T1,i)
Ans: a. len() is used wrongly
b. P is in uppercase in print command
c. T1 is not defined

3. t1=(10,20,30,40,50,60,70,80)
t1[5]=55
[Link](90) print(t1,i)
Ans: a. 'tuple' object does not support item assignment in line 2
b. Append() Method is not with tuple

4. t1=(10,20,30,40,50,60,70,80)
t2=t1*2 t3=t2+4
print t2,t3
Ans: a) line 3 cannot concatenate with int
b) Parenthesis is missing in line 4

5. t1=(10,20,30,40,50,60,70,80)
str=”” str=index(t1(40))
print(“index of tuple is ”, str)
str=[Link]()
print(“max item is “, str)

Ans: a. Syntax error in index function


b. Syntax error in max function

LIST BASED QUESTION


Q1. Give the output of the following code:
list=['p','r','o','b','l','e','m']
print(list[1:3])
print(list[2:5])
Ans: ['r', 'o']
['o', 'b', 'l']

[Link] the output of the following code:-


l1=[13,18,11,16,13,18,13]
print([Link](18))
print([Link](18))
[Link]([Link](13))
print(l1)
Ans: 1
2
[13,18,11,16,13,18,13,3]

Q3. What is the output of the following:


list1 = [1, 2, 3, 4, 5]
list2 =list1
list2[0] =0;
print("list1= : ", list1)
Ans: [0,2,3,4,5]

Q4. What Will Be The Output Of The Following Code Snippet?


a =[1,2,3,4,5]
print(a[3:0:-1])
A. Syntax error B. [4, 3, 2]
C. [4, 3] D. [4, 3, 2, 1]
Ans: B

Q5. What Will Be The Output Of The Following Code Snippet?


a = {(1,2):1,(2,3):2}
print(a[1,2])
Ans: 1
Short Answer Questions
1. What is the difference between interactive mode and script mode in Python?
Ans. In interactive mode, instructions are given in front of Python prompt (>>>) in
Python Shell. Python carries out the given instructions and shows the result there
itself.
In script mode, Python instructions are stored in a file, generally with .py extension,
and executed together in one go as a unit. The saved instructions are known as
Python script or Python program.
2. Differentiate between mutable and immutable objects in Python language with
example.
Ans. Every variable in Python holds an instance of an object. There are two types of
objects in Python, i.e., Mutable and Immutable objects. Whenever an object is
instantiated, it is assigned a unique object id. The type of the object is defined at
the runtime and it can’t be changed afterwards.
However, its state can be changed if it is a mutable object.
For example, int, float, bool, string, unicode, tuple are immutable objects in Python. In
simple words, an immutable object can’t be changed after it is created. Lists,
dictionaries and sets are mutable types.
3. How many types of strings are supported in Python?
Ans. Python allows two string types:
1. Single line Strings—Strings that are terminated in a single line.
2. Multi-line Strings—Strings storing multiple lines of text.
4. Find the output of the following:
word = 'green vegetables' print([Link]('g', 2))
print([Link]('veg', 2))
print([Link]('tab', 4, 15))

Ans. Output:
8
6
10
5. Suppose L=["abc",[6,7,8], 3,"mouse"]
Consider the above list and answer the following:
(a) L[3:] (b) L[::2]
(c) L[1:2] (d) L[1][1]
Ans. (a) ['mouse'] (b) ['abc', 3]
(c) [[6, 7, 8]] (d) 7

6. What do you mean by scope of variables ?


Ans: Scope of variables refers to the part of the program where it is visible, i.e, the area where
you can use it

7. Which of the following is valid arithmetic operator in Python:


(i)// (ii)? (iii)< (iv)and
Ans: (i)

8. Write the type of tokens from the following:


(i) if (ii) roll_no
Ans: i) Keyword
ii)identifier
9. Which of the following are valid operators in Python:
(i) ** (ii) */ (iii) like (iv) ||
(v) is (vi) ^ (vii) between (viii) in
Ans: i) iv) vi) viii)

10. Which of the following can be used as valid variable identifier(s) in Python?
(i) 4thSum (ii) Total (iii) Number# (iv) _Data
Ans: ii) and iv)

11. What is a tuple?


Ans. A tuple is an immutable sequence of values which can be of any type and are indexed by
an integer.
12. Differentiate between lists and tuples.
Ans. Tuples cannot be changed unlike lists; tuples use parentheses, whereas lists use square
brackets.
13. What is a dictionary?
Ans. A Python dictionary is a mapping of unique keys to values. It is a collection of key-
value pairs. Dictionariesare mutable which means that they can be changed.

14. Write a Python program to replace the last element in a list with
another [Link] data : [1, 3, 5, 7, 9, 10], [2, 4, 6, 8]
Expected Output: [1, 3, 5, 7, 9, 2, 4, 6, 8]
Ans. num1 = [1, 3, 5, 7, 9, 10]
num2 = [2, 4, 6, 8]
num1[-1:] = num2
print(num1)

15. Write the type of tokens from the following:


a. if
b. roll_no
Ans. (a) Key word (b) Identifier

You might also like