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

CLASS XI COMP SCI PYTHON REVISION TOUR

The document provides a comprehensive overview of Python programming, covering fundamental concepts such as data types, operators, keywords, and rules for naming identifiers. It includes short answer questions and exercises to reinforce understanding of Python syntax and functionality. Additionally, it addresses common programming tasks and concepts such as iteration, scope of variables, and error handling.

Uploaded by

parasharshagun4
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)
60 views

CLASS XI COMP SCI PYTHON REVISION TOUR

The document provides a comprehensive overview of Python programming, covering fundamental concepts such as data types, operators, keywords, and rules for naming identifiers. It includes short answer questions and exercises to reinforce understanding of Python syntax and functionality. Additionally, it addresses common programming tasks and concepts such as iteration, scope of variables, and error handling.

Uploaded by

parasharshagun4
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/ 17

PYTHON REVISION TOUR

IMPORTANT POINTS:
Introduction to Python
• Python is an open source, object oriented HLL developed by Guido van Rossum in 1991
• Tokens - smallest individual unit of a python program.
• Keyword - Reserved word that can’t be used as an identifier
• Identifiers - Names given to any variable, constant, function or module etc.
• Literals - A fixed numeric or non-numeric value.
• Variable - A variable is like a container that stores values to be used in program.
• String - The text enclosed in quotes.
• Comment - Comments are non-executable statement begin with # sign.
• Docstring - Comments enclosed in triple quotes (single or double).
• Operator – performs some action on data
o Arithmetic :- (+,-,*,/,%,**,//)
o Relational/comparison :- (<,>, <=,>=, = =, !=).
o Assignment :- =
o Augmented Assignment :- (/=,+=,-=,*=,%=,**=,//=)
o Logical :- and, or, not
o Membership – in, not in
o Identity :– is, is not
o Bitwise :- (&, |, ~, ^, >>, <<)

• RULES FOR NAMING AN IDENTIFIER:-


i) Python is a case-sensitive programming language so Uppercase Letters and Lower
Case letters are different
ii) The name of identifier cannot begin with a digit. However, Underscore and alphabet
can be used as first character while declaring the identifier.
iii) Only alphabetic characters, digits and underscore (_) are permitted in Python
Programming language for declaring identifier.
iv) Other special characters are not allowed for naming a variable / identifier
v) No blank space is used in the name of identifier
vi) Keywords cannot be used as Identifier.

• KEYWORDS:-

• PRECEDENCE OF OPERATORS:-

Operator Description
** Exponentiation (raise to the power)

~+- Complement, unary plus and minus (method names for the last two are +@ and -@)

* / % // Multiply, divide, modulo and floor division

+- Addition and subtraction


• Data Types:-

• SHORT ANSWER QUESTIONS (EACH QUESTION CARRIES 1 MARK)

1 Find the invalid identifier(s) from the following


a) MyName b) True c) 2ndName d) My_Name
ANS b) True c) 2ndName
2 Given the lists L=[1,3,6,82,5,7,11,92] , write the output of print(L[2:5])
ANS [6,82,5]
3 Identify the valid arithmetic operator in Python from the following.
a) ? b) < c) ** d) and
ANS c) **
4 Suppose a tuple T is declared as T = (10, 12, 43, 39), which of the following is
incorrect?
a) print(T[1])
b) T[2] = -29
c) print(max(T))
d) print(len(T))
ANS b) T[2]= -29 (as tuple is immutable)
5 Write a statement in Python to declare a dictionary whose keys are 1, 2, 3 and
values are Monday, Tuesday and Wednesday respectively.
ANS Day={1:'monday',2:'tuesday',3:'wednesday'}
6 A tuple is declared as
T = (2,5,6,9,8)
What will be the value of sum(T)?
ANS 30
7 Identify the valid declaration of L:
L = [‘Mon’, ‘23’, ‘hello’, ’60.5’]
a. dictionary b. string c.tuple d. list
ANS d. List
8 If the following code is executed, what will be the output of the following code?
name="ComputerSciencewithPython"
print(name[3:10])
ANS puterSc
9 Which of the following is valid arithmetic operator in Python:
(i) // (ii) ? (iii) < (iv) and
ANS (i) //
10 Write the type of tokens from the following:
(i) if (ii) roll_no
ANS (i) Key word (ii) Identifier
11 What do you understand by the term Iteration?
ANS Repetition of statement/s finite number of times is known as Iteration.
12 Which is the correct form of declaration of dictionary?
(i) Day={1:’monday’,2:’tuesday’,3:’wednesday’}
(ii) Day=(1;’monday’,2;’tuesday’,3;’wednesday’)
(iii) Day=[1:’monday’,2:’tuesday’,3:’wednesday’]
(iv) Day={1’monday’,2’tuesday’,3’wednesday’]
ANS (i) Day={1:’monday’,2:’tuesday’,3:’wednesday’}
13 Find and write the output of the following python code:
x = "abcdef"
i = "a"
while i in x:
print(i, end = " ")
ANS aaaaaa----- OR infinite loop
14 Find and write the output of the following python code:
a=10
def call():
global a
a=15
b=20
print(a)
call()
ANS 15
15 Identify the Invalid relational operator in Python from the following.
a) <= b) > c) == d) <>
ANS d) <>
16 Assume that a List L is declared as L = [5,10, 15, 20, 25], which of the following
will produce output 25?
a) print(L[1])
b) print(L[-1])
c) print(L[2])
d) print(L[-2])
ANS b) print(L[-1])
17 Write a statement in Python to declare a dictionary of 3 items, whose keys are
“Country name” and respective values are “their Capital”.
ANS country={"india":"New Delhi","Sri Lanka":"Colombo","China":"Beijing"}
or any other correct answer.
18 A tuple is declared as
Name = ('Sumit', 'Anil', 'Rahul')
What will be the value of min(Name)?
ANS Anil
19 Identify the valid declaration of T:
T = “[‘Computer’, ‘23000’, ‘Mobile’, ’15000’,’Printer’,’10000’]“

a. dictionary b. string c.tuple d. list


ANS b. string
20 Find output of the following code?
progL="Python Java Visual Basic"
print(progL[-5:])
ANS Basic
21 Rearrange the following operators in Highest to Lowest Priority.
%, or, ==, not, =
ANS %, ==, not, or, =
22 Find the invalid identifier from the followings:
a) File_name, b) sl1, c) False, d) num34
ANS c) False
23 How would you write xy-4x9 in python
ANS math.pow(x,y) – 4 * math.pow(x,9)
24 What is the output of the following code:
int(“5” + “7”)
ANS 57
25 Given s1= “My Vidyalaya” . Write the output of print(s1[1:5])
ANS y Vi
26 Whether the following statement in connection with dictionary is True or False.
The Keys of a dictionary must be of immutable types.
ANS True
27 What will be the output of the following expression:
print(24//6%3, 24//4//2, 20%3%2)
ANS (1,3,0)
28 Evaluate 16%15//16
ANS 0
29 What do you mean by Type error?
ANS Occur when there is attempt to call a function or use an operator on something
of the incorrect type.
30 Which one of the following is the default extension of a Python file?
a. .exe b. .p++ c. .py d. .p
ANS c. .py
31 Given a Tuple tup1= (10, 20, 30, 40, 50, 60, 70, 80, 90).
What will be the output of print (tup1 [3:7:2])?
ANS (40,60)
32 What is the return type of the input() function?
ANS String
33 Which of the following operator cannot be used with string data type?
a. + b. in c. * d. /
ANS d. /
34 Which of the following symbol is used in Python for single line comment?
a. / b. /* c. // d. #
ANS d. #
35 Which of these about a dictionary is false?
a) The values of a dictionary can be accessed using keys
b) The keys of a dictionary can be accessed using values
c) Dictionaries aren’t ordered
d) Dictionaries are mutable
ANS b) The keys of a dictionary can be accessed using values
36 What is the output of following code:
T=(100)
print(T*2)
ANS 200
37 What is the output of following code:
T=(100,)
print(T*2)
ANS (100,100)

38 Write the output of the following Python statements.


x = [[10.0, 11.0, 12.0],[13.0, 14.0, 15.0]]
y = x[1][2]
print(y)
ANS 15.0
39 Write the output of the following Python statements.
x=2
while x < 9:
print(x, end='')
x=x+1
ANS 2345678

• SHORT ANSWER QUESTIONS (EACH QUESTION CARRIES 2 MARKS)


1 Evaluate the following expressions:
a) 6 * 3 + 4**2 // 5 – 8
b) 10 > 5 and 7 > 12 or not 18 > 3
ANS a) 6 * 3 + 4**2 // 5 – 8
= 6 * 3 + 16 // 5 - 8
= 18 + 3 – 8
=13

b) 10 > 5 and 7 > 12 or not 18 > 3


= True and False or False
= False or False
= False
2 Rewrite the following code in Python after removing all syntax error(s). Underline
each correction done in the code.

ANS

3 What possible outputs(s) are expected to be displayed on screen at the time of


execution of the program from the following code? Also specify the maximum
values that can be assigned to each of the variables Lower and Upper.

ANS OUTPUT: (ii) 30#40#50#


Maximum value of Lower: 3
Maximum value of Upper: 4
4

ANS OUTPUT : fUN#pYTHONn#.

5 Rewrite the following code in python after removing all syntax error(s).
Underline each correction done in the code.
30=To
for K in range(0,To)
IF k%4==0:
print (K*4)
Else:
print (K+3)
ANS

6 Find and write the output of the following python code:


def fun(s):
k=len(s)
m=" "
for i in range(0,k):
if(s[i].isupper()):
m=m+s[i].lower()
elif s[i].isalpha():
m=m+s[i].upper()
else:
m=m+'bb'
print(m)
fun('school2@com')
ANS SCHOOLbbbbCOM
(2 marks for correct output)
Note: Partial marking can also be given
7 What do you understand by local and global scope of variables? How can you access
a global variable inside the function, if function has a variable with same name.
ANS A global variable is a variable that is accessible globally. A local variable is one that
is only accessible to the current scope, such as temporary variables used in a single
function definition.
A variable declared outside of the function or in global scope is known as global
variable. This means, global variable can be accessed inside or outside of the
function where as local variable can be used only inside of the function. We can
access by declaring variable as global A.
8 Identify the Local and Global variable in the following code.
x=100
def myfunc(a):
k=a
print(k,a)
p=(0,1,2,3,4)
myfunc(p)
print(x)
ANS x= 100 # Global var x
def myfunc(a): # Local var a
k=a # Local var k
print(k,a)
p=(0,1,2,3,4) # Global var p
myfunc(p)
print(x)
9 Rewrite the following code in Python after removing all syntax error(s).
Underline each correction done in the code.
x==310
for z in Range(0,x)
if z//5==0:
print (z**5)
else if z%5==0:
print (z+300 )
ANS x=310
for z in range(0,x):
if z//5==0:
print(z**5)
elif z%5==0:
print(z+300)
10 What possible outputs(s) are expected to be displayed on screen at the time of
execution of the program from the following code? Also specify the maximum and
minimum value that can be assigned to pos variable.
import random
languages=[‘Python’, ‘Java’, ‘PHP’, ‘HTML’, ‘C++’]
pos =random.randint(1,4)
for c in range(0, pos+1):
print (languages[c],end=”&”)

(i) Python&Java&PHP&HTML&C++&
(ii) Java&PHP&HTML&C++&
(iii) Python&Java&
(iv) Python&Java&PHP&
ANS Option (i),(iii) &(iv) (1½ mark for correct answer)(No partial marking)
Maximum = 4 Minimum=1 ( ½ mark for correct answer)
11 Find and write the output of the following Python code:
def Display(str):
m=""
for i in range(0,len(str)):
if(str[i].isupper()):
m=m+str[i]+'*'
elif str[i].islower():
m=m+'@'
elif str[i]==' ' :
m=m+'#'
print(m)
Display('CracK it')
ANS C*@@@K*#@@
12

(3 MARKS)
ANS 250 # 150
250 # 100
130 # 100
(1 mark each for correct line)
13 Find the output of the following program;
def increment(n):
n.append([4])
return n
l=[1,2,3]
m=increment(l)
print(l,m)
ANS [1, 2, 3, [4]] [1, 2, 3, [4]]
14 Find the error in the given code and correct the code. Underline each correction.
if n==0
print(“Zero”)
elif : n==1
Print(“One”)
elif
n==2:
print(“two”)
else n==3:
print(“Three”)
ANS

15 In the following code, which variables are in same scope and why?
def func1();
A=1
B=2
def func2():
C=3
D=4
E=5
ANS 1. A,B are in same scope as they belong to func1()
2. C,D are in same scope as they belongs to func2()
16 Predict the output of the following code snippets?
arr=[1,2,3,4,5,6]
for i in range(1,6):
arr[i-1]=arr[i]
for i in range(2,6):
print(arr[i], end=" ")
ANS 4566
17 Write the output of the following Python statements.
b=1
for a in range(1, 10, 2):
b += a + 2
print(b)
ANS 36
18 Write the output of the following Python statements.
lst1 = [10, 15, 20, 25, 30]
lst1.insert( 3, 4)
lst1.insert( 2, 3)
print (lst1[-5])
ANS 3
19 What will be the output of the following Python code?
def add (num1, num2):
sum = num1 + num2
sum = add(20,30)
print(sum)
ANS None
20 Evaluate the following expression
16-(4+2)*5+2**3*4
ANS 18
21 What will be the output of the following Python code?
def my_func(var1=100, var2=200):
var1+=10
var2 = var2 - 10
return var1+var2
print(my_func(50),my_func())
ANS 250 300

22 What will be the output of the following Python code?


value = 50
def display(N):
global value
value = 25
if N%7==0:
value = value + N
else:
value = value - N
print(value, end="#")
display(20)
print(value)
ANS 50#5
23 What will be the output of the following Python code?
import random
List=["Delhi","Mumbai","Chennai","Kolkata"]
for y in range(4):
x = random.randint(1,3)
print(List[x],end="#")
a. Delhi#Mumbai#Chennai#Kolkata#
b. Mumbai#Chennai#Kolkata#Mumbai#
c. Mumbai# Mumbai #Mumbai # Delhi#
d. Mumbai# Mumbai #Chennai # Mumbai
ANS b. Mumbai#Chennai#Kolkata#Mumbai#
24 What is the output of the following code snippet?
def ChangeVal(M,N):
for i in range(N):
if M[i]%5 == 0:
M[i]//=5
if M[i]%3 == 0:
M[i]//=3
L = [25,8,75,12]
ChangeVal(L,4)
for i in L:
print(i,end="#")
ANS 5#8#5#4#
25 Find the output of the following code:
Name="PythoN3.1"
R=""
for x in range(len(Name)):
if Name[x].isupper():
R=R+Name[x].lower()
elif Name[x].islower():
R=R+Name[x].upper()
elif Name[x].isdigit():
R=R+Name[x-1]
else:
R=R+"#"
print(R)
ANS pYTHOnN#@
26 What will be the output of the following code?
x=3
def myfunc():
global x
x+=2
print(x, end=' ')
print(x, end=' ')
myfunc()
print(x, end=' ')
ANS 355
27 What will be the output of the following code?
tup1 = (1,2,[1,2],3)
tup1[2][1]=3.14
print(tup1)
ANS (1,2,[1,3.14],3)
ANS def ODDSum(NUMBERS):
s=0
for i in NUMBERS:
if i%2 !=0:
s=s+i
print(s)
4 Write a function in REP which accepts a list of integers and its size as
arguments and replaces elements having even values with its half and elements
having odd values with twice its value .
eg: if the list contains
3, 4, 5, 16, 9
then the function should rearranged list as
6, 2,10,8, 18
ANS def REP (L, n):
for i in range(n):
if L[i] % 2 == 0:
L[i] /= 2
else:
L[i] *= 2
print (L)
USING PYTHON LIBRARIES
Functions using libraries:-
Built-in Functions

String Methods and Built-in functions


Function Description
len() Returns the length of the string.
capitalize() Converts the first letter of the string in uppercase
split() Breaks up a string at the specified separator and returns a list of
substrings.
replace() It replaces all the occurrences of the old string with the new string.
find() It is used to search the first occurrence of the substring in the given
string.
It also searches the first occurrence and returns the lowest index of the
index()
substring.
It checks for alphabets in an inputted string and returns True in string
isalpha()
contains only letters.
isalnum() It returns True if all the characters are alphanumeric.
isdigit() It returns True if the string contains only digits.
It returns the string with first letter of every word in the string in
title()
uppercaseand rest in lowercase.
count() It returns number of times substring str occurs in the given string.
lower() It converts the string into lowercase
islower() It returns True if all the letters in the string are in lowercase.
upper() It converts the string into uppercase
isupper() It returns True if all the letters in the string are in uppercase.
lstrip() It returns the string after removing the space from the left of the string
rstrip() It returns the string after removing the space from the right of the string
It returns the string after removing the space from the both side
strip()
of thestring
It returns True if the string contains only whitespace characters,
isspace()
otherwisereturns False.
istitle() It returns True if the string is properly title-cased.
swapcase() It converts uppercase letter to lowercase and vice versa of the given
string.
ord() It returns the ASCII/Unicode of the character.
It returns the character represented by the imputed Unicode
chr()
/ASCIInumber

Built-in Function (Manipulating Lists)


Function Description
append() It adds a single item to the end of the list.
extend() It adds one list at the end of another list
insert() It adds an element at a specified index.
reverse() It reverses the order of the elements in a list.
index() It returns the index of first matched item from the list.
len() Returns the length of the list i.e. number of elements in a list
sort() This function sorts the items of the list.
clear() It removes all the elements from the list.
count() It counts how many times an element has occurred in a list and returns
it.
It removes the element from the end of the list or from the specified
pop()
indexand also returns it.
Tuple Functions

Dictionary Functions
Some more functions of random module

1) random() -> it returns a random floating point number N in the range [0.0, 1.0]
print(random.random())

2) randint(a,b) -> it returns a random integer in the range (a,b) -> a>=N<=b
print(random.randint(3,5))

3) random.uniform(a,b) -> it returns a random floating point number in the range (a,b) -
> a>=N<=b

4) random.randrange(start,stop[,step]) -> it returns a randomly selected element from


range (start, stop, step)

You might also like