Chapter 1 Revision Tour
Chapter 1 Revision Tour
KEYWORDS
Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name or any other
identifier. They are used to define the syntax and structure of the Python language. In Python, keywords are case sensitive.
False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not
IDENTIFIER
An identifier is a name given to entities like class,functions, variables etc.
Rules for writing identifiers
• The name of the variable must always start with either a letter or an underscore (_). For example: _str, str, num,
_num are all valid name for the variables.
• The name of the variable cannot start with a number. For example: 9num is not a valid variable name.
• The name of the variable cannot have special characters such as %, $, #.- etc, they can only have alphanumeric
characters and underscore (A to Z, a to z, 0-9 or _ ).Variable name cannot contain space
• Variable name is case sensitive in Python which means num and NUM are two different variables in python.
• Keywords cannot be used as identifiers
1. Out of the following, find those identifiers, which can not be used for naming Variable or Functions in a Python
program: [2] [OD 16]
Total*Tax, While, class, switch, 3rdRow, finally, Column31, _Total
Ans. Total*Tax, class, 3rdRow, finally
2. Identify invalid variable names out of the following. State reason if invalid. [OD 16]
(i) for (ii) –salary (iii) salary12 (iv) product
Ans. Invalid variable names: (i) for and (ii) –salary Reason : i) ‘for’ : is a keyword ii) ‘–salary’:
variable name cannot start with special character
3. Identify invalid variable name(s) out of the following. State reason if invalid. [comptt 16]
(i) While (ii) 123salary (iii) Big (iv) Product
Ans. 123salary
4. Help Manish in identifying the incorrect variable name with justification from the following: [SP 18]
(i) unit@price (ii) fee (iii) userid (iv) avg marks
Ans. (i) unit@price; // Special symbols like „@‟ is not allowed in variable name
(iv) avg marks;// Spaces are not allowed in variable name
1
5. Identify the invalid variable names. State the reason if invalid. [1] [2018]
(i) Marks Unit (ii) Product_1 (iii) Sales123 (iv) 2Marks
Ans. Invalid variable names are :
(i) Marks Unit
Reason: Variable Name should not contain space
(iv) 2Marks Reason : Variable Name should not start with digit
6. Which of the following can be used as valid variable identifier(s) in Python [2] [d 17]
(i) total (ii) 7Salute (iii) Que$tion (iv) global
Ans. (i) total
7. Which of the following can be used as valid variable identifier(s) in Python? [2] [compt 17]
(i) elif (ii) BREAK (iii) in (iv) _Total
Ans. BREAK
_Total
8. Which of the following can be used as valid variable identifier(s) in Python? [OD 17]
(i) 4thSum (ii) Total (iii) Number# (iv) _Data
Ans. (ii) Total iv) _Data
9. Out of the following, find those identifiers, which can not be used for naming Variable or Functions in a Python
program: [D 16]
_Cost, Price*Qty, float, Switch, Address One,
Delete, Number12, do
Ans. Price*Qty, float, Address One
10. Find the correct identifiers out of the following, which can be used for naming variable, constants or functions
in a python program:
While, for, Float, new, 2ndName, A%B, Amount2, _Counter
Ans. While, Float, Amount2, _Counter
11. Find the correct identifiers out of the following, which can be used for naming Variable, Constants or Functions
in python program: [OD 2015]
For, while, INT, NeW, delete, 1stName, Add+Subtract, name1
Ans. For, INT, NeW, name1
12. Out of the following, find those identifiers, which cannot be used for naming Variable, Constants or Functions in
a python program:
_Cost, Price*Qty, float, switch, Address One, Delete, Number12, do
Ans. Price*Qty
float
Address One
13. Out of the following, find those identifiers, which can not be used for naming Variable, Constants or Functions
in a python program:
Total*Tax, double, Case, My Name, NeW, switch, Column31, _Amount
Ans. Total*Tax
My Name
14. Write the type of python keywords and user defined identifiers from the following:
(i) For (ii) Delete (iii) else (iv) Value
Ans. (i) For - user defined identifier (ii) Delete - user defined identifier
(iii) else - keyword (iv) Value - user defined identifier
2
15. Write the type of python keywords and user defined identifiers from the following :
(i) case (ii) _delete (iii) while (iv) 21stName
Ans. (i) case-user defined identifier (ii) delete-user dofined identifier
(iii) while - keyword (iv) 21st Name - neither
16. Write the type of python keywords and user defined identifiers from the following:
(i) in (ii) While (iii) and (iv) Num_2
Ans. (i) in - Keyword (ii) While - User defined Identifier
(iii) and - Keyword (iv) Num_2 - User defined Identifier
17. Write the type of python keywords and user defined identifiers from the following:
(i) else (ii) Long (iii) break (iv) _count 2
Ans. (i) keyword (ii) Identifier (iii) keyword (iv) Identifier
OPERATORS
Arithmetic operators Relational Operators
Addition(+) Subtraction(-) Multiplication(*) Less than(<) Greater than(>) Less than or
equal to(<=)
Division(/) Exponentiation(**) Floor Division(//) Greater than or equal to(>=) Equal to(= =) Not equal to(!=)
Modulus(%)
Assignment operators
Assign(=) Add and Assign(+=) Subtract and Assign(-=) Divide and Assign(/=)
Multiply and Modulus and Assign(%=) Exponent and Assign(**=) Floor-Divide and Assign(//=)
Assign(*=)
Logical Operator Membership Python Operator Python Identity Operator
and or not in not in is is not
Bitwise Operator
& | ^
Binary AND Binary OR Binary XOR
~ Binary Ones Complement << Binary Left Shift >> Binary Right Shift
18. Which of the following are valid operators in Python 2? [comptt 19]
(i) += (ii) ^ (iii) in (iv) &&
(v) between (vi) */ (vii) is (viii) like
Ans. (i) += (ii) ^ (iii) in (vii) is
19. Which of the following are valid operators in Python 2: [2019]
(i) ** (ii) */ (iii) like (iv) ||
(v) is (vi) ^ (vii) between (viii) in
Ans. (i) ** (v) is (vi) ^ (viii) in
Data Types
Text Type: string
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
3
Operator Precedence
The precedence of the operators is important to know which operator should be evaluated first The precedence table
of the operators in python is given below:
Operator Description
** The exponent operator is given priority over all the others used in the expression.
~+- The negation, unary plus and minus.
* / % // The multiplication, divide, modulus, remainder, and floor division.
+- Binary plus and minus
>> << Left shift and right shift
& Binary and.
^| Binary xor and or
<= < > >= Comparison operators (less than, less than equal to, greater than, greater than equal to).
<> == != Equality operators.
= %= /= //= -= += Assign, modulus and assign, divide and assign, floor divide and assign, subtract and assign, add and assign.
*= **= Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators
20. Write the names of any four data types available in Python 2. [2019]
Ans. Numbers Integer Boolean Floating Point
Complex None Sequences Strings
Tuple List Sets Mappings
Dictionary
21. Consider the statement:
first_name = “Ayana”;
(i) What is the datatype of first_name ?
(ii) Is 325 the same as “321” ? Give reason.
Ans. (i) String data type (ii) No, 325 is a Number/Integer while “321” is a String.
22. Consider the statement : [1]
fname = “Rishabh”;
(i) What is the datatype of fname ?
(ii) Is 456 the same as “456” ? Give reason
Ans. (i) String data type (ii) No, 456 is a Number/Integer while “456” is a String.
23. (i) Is 123 the same as 123.0 ? Give reason
(ii) Is “234” the same as “234.0” ? Give reason
Ans. (i) No, 123 is of integer type and 123.0 is of float type (ii) These are not numbers. Both are strings and
different
24. Write the data type of variables that should be used to store: (i) Marks of students (ii) Grades of students(Grade
can be ‘A’ or ‘B’ or ‘C’) [1] [2018]
Ans. (i) float/int (ii) string
MISCELLANEOUS
Print()
The print function in Python is used to display the output of variables: string, lists, tuples, range etc.
Following is the syntax of using the print function:
print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
4
Where:
• The object can be strings, lists, tuple etc.
• The sep = ‘’ parameter specifies a space be tween multiple objects. You can use other than space by using this parameter.
• The end= ‘\n’ means in each call, the print function will end at a new line, you can change that.
• The file=sys.stdout specifies where the print function should send the output. The default value in sys.stdont.
• The flush keyword argument specifies whether to flush or not the output stream. The default value is false.
INPUT()
The input() method reads a line from input(usually user), converts into a string and returns it. The syntax of input()
method is: input([prompt])
The input() method takes a single optional argument: prompt (Optional) - prompt is the string we wish to display on
the screen. It is optional
25. (i) What is difference between mutable and immutable in Python?
(ii) Distinguish between ‘/’ and ‘%’ operators.
Ans. (i) mutable object can be changed after it is created, and an immutable object can’t. Objects of built-in types like
(int, float, bool, string, tuple, unicode) are immutable. Objects of built-in types like (list, set, dict) are mutable.
(ii) ‘/’ divides first number with second number and returns the quotient.
‘%’ divides first number with second number and returns the remainder.
26. Write the value that will be assigned to variable x after executing the following statement : [1]
x = 3 + 36/12 + 2*5
Ans. 16.0
27. Write the value that will be assigned to variable C after executing the following statement:
C = 25-5*4/2-10+4
Ans. 9.0
28. Write the value that will be assigned to variable x after executing the following statement:
x = 20 -5 + 3 * 20/5
Ans. 27.0
29. Write the output of the following Python code: [2] [SP 18]
i=5
j=7
x=0
i=i+(j-1)
x=j+1
print(x,”:”,i)
j=j**2 x=j+i
i=i+1
print (i,”:”,j)
Ans. 8 : 11
12 : 49
30. Give the output if a=10 and b=20
a=input(“enter no”)
b=input(“enter 2nd no”)
print(a+b)
Ans. 1020
31. Write a program to take two nos and swap two nos
Ans. a=int(input(“enter a”))
5
b=int(input(“enter b”))
a,b=b,a
print(a,b)
32. Write a program to take two numbers and show 2nd number is power of 1st . For example if 1st number is 5
and 2nd number is 3 then result is 53 = 125
Ans. a=int(input(“enter 1st no “))
b=int(input(“enter power”))
print(a**b)
33. write a program to take two nos and show the sum of these two nos
Ans. a=int(input(“enter a”))
b=int(input(“enter b”))
print(a+b)
34. write a program to take two float numbers and show the sum of these two.
Ans. a=float(input(“enter a”))
b=float(input(“enter b”))
print(a+b)
35. Write a program to take first name and last name from user and show full name. For example if first name is
“Aman” and last name is “Sharma” then it will display “AmanSharma”(without space).
Ans. first=input(“enter 1st name”)
last=input(“enter last name”)
print(first+last)
36. Give the output
(i) print(20,sep=”-“,end=”$”) (ii) print(40,50,60,sep=”*“,end=”!”)
(iii) print(“hello,hi,bye”,sep=”*“,end=”!”) (iv) print(“hello”,”hi”,”bye”,sep=”*”,end=”!”)
(v) print(2>5,7<10,end=”@”) (vi) print(“2*3,6*5”,sep=”%”)
(vii) print(2*3,6+5,sep=”+”)
Ans. (i) 20$ (ii) 40*50*60!
(iii) hello,hi,bye! (iv) hello*hi*bye!
(v) False True@ (vi) 2*3,6*5
(vii) 6+11
37. Give the output
(i) a=20 (ii) a=20 (iii) a=10
b=30 b=30 b=a*20
a,b=b+20,a+30 a,c=b*10,a/10 b,c=b*10,a%10
print(a,b) print(a,b,c) print(a,b,c)
Ans. (i) 50 50 (ii) 300 30 2.0 (iii) 10 2000 0
MCQ
1. Which of the following can be used as valid variable identifier(s) in Python
(i) total (ii) 2switch (iii) Que$tion (iv) global
2. Identify invalid variable name out of the following.
(i) While (ii) 123salary (iii) Big (iv) Float
3. Which of the following is invalid operator in Python
(i) += (ii) ^ (iii) in (iv) &&
4. Identify invalid operator out of the following.
(i) between (ii) */ (iii) is (iv) //
5. What is the datatype of “321”
(i) None (ii) Integer (iii) String (iv) List
6
6. Output of the following code is
print(40,50,60,sep=”!“,end=”,”)
(i) 40,50,60! (ii) 40!50!60! (iii) 40!50!60, (iv) 40,50,60
7. print(2*5,6+5,sep=”+”)
output of above code is
(i) 10,11 (ii) 10+11 (iii) 21 (iv) ”10”+”11”
8. Write the value that will be assigned to variable
x x = 6 + 36/6 + 2*5
(i) 45.0 (ii) 17.0 (iii) 26.25 (iv) 22.0
9. Write the value that will be assigned to variable
c c = 25-5*4/2-10+5
(i) 10.0 (ii) 35.0 (iii) -2.5 (iv) -12.5
10. Write the value that will be assigned to variable a
a=20*5+3**2%2//5
(i) 101 (ii) 100 (iii) 1.9 (iv) None of these
7
15. a=16
b=3
c=a/2**b
print(c)
(i) 512.0 (ii) 4.0 (iii) 2.0 (iv) 24.0
Ans. 1. (i) 2. (ii) 3. (iv) 4. (i) 5. (iii) 6. (iii) 7. (ii) 8. (iv) 9. i) 10. (ii)
11. (iv) 12. (iii) 13. (iv) 14. (iii) 15. (iii)