Python Fundamentals C.W Notes
Python Fundamentals C.W Notes
Question 1
What are tokens in Python ? How many types of tokens are allowed in Python ? Examplify
your answer.
Answer
The smallest individual unit in a program is known as a Token. Python has following tokens:
Question 2
Answer
Keywords are reserved words carrying special meaning and purpose to the language
compiler/interpreter. For example, if, elif, etc. are keywords. Identifiers are user defined
names for different parts of the program like variables, objects, classes, functions, etc.
Identifiers are not reserved. They can have letters, digits and underscore. They must begin
with either a letter or underscore. For example, _chk, chess, trail, etc.
Question 3
What are literals in Python ? How many types of literals are allowed in Python ?
Answer
Literals are data items that have a fixed value. The different types of literals allowed in
Python are:
1. String literals
2. Numeric literals
3. Boolean literals
4. Special literal None
5. Literal collections
Question 4
Can nongraphic characters be used in Python ? How ? Give examples to support your answer.
Answer
Yes, nongraphic characters can be used in Python with the help of escape sequences. For
example, backspace is represented as \b, tab is represented as \t, carriage return is represented
as \r.
Question 5
How are floating constants represented in Python ? Give examples to support your answer.
Answer
Floating constants are represented in Python in two forms — Fractional Form and Exponent
form. Examples:
Question 6
Answer
Question 7
Which of these is not a legal numeric type in Python ? (a) int (b) float (c) decimal.
Answer
Question 8
Answer
(i) sep
(ii) end
Question 9
What are operators ? What is their function ? Give examples of some unary and binary
operators.
Answer
Operators are tokens that trigger some computation/action when applied to variables and
other objects in an expression. Unary plus (+), Unary minus (-), Bitwise complement (~),
Logical negation (not) are a few examples of unary operators. Examples of binary operators
are Addition (+), Subtraction (-), Multiplication (*), Division (/).
Question 10
Answer
An expression is any legal combination of symbols that represents a value. For example, 2.9,
a + 5, (3 + 5) / 4.
A statement is a programming instruction that does something i.e. some action takes place.
For example:
print("Hello")
a = 15
b = a - 10
Question 11
Answer
A Python program can contain various components like expressions, statements, comments,
functions, blocks and indentation.
Question 12
Answer
A block/code block/suite is a group of statements that are part of another statement. For
example:
if b > 5:
print("Value of 'b' is less than 5.")
print("Thank you.")
Question 13
Answer
Python uses indentation to create blocks of code. Statements at same indentation level are
part of same block/suite.
Question 14
Answer
Variables are named labels whose values can be used and processed during program run.
Variables are important for a program because they enable a program to process different sets
of data.
Question 15
Answer
In Python, a variable is not created until some value is assigned to it. A variable is created
when a value is assigned to it for the first time. If we try to use a variable before assigning a
value to it then it will result in an undefined variable. For example:
Question 16
Answer
x = 10
print(x)
x = "Hello World"
print(x)
Question 17
Answer
It will assign a value of 7 to the variables X and Y.
Question 18
Answer
The error in the above code is that we have mentioned two variables X, Y as Lvalues but only
give a single numeric literal 7 as the Rvalue. We need to specify one more value like this to
correct the error:
X, Y = 7, 8
Question 19
Answer
Python doesn't allow decimal numbers to have leading zeros. That is the reason why this line
is creating problem.
Question 20
"Comments are useful and easy way to enhance readability and understandability of a
program." Elaborate with examples.
Answer
Comments can be used to explain the purpose of the program, document the logic of a piece
of code, describe the behaviour of a program, etc. This enhances the readability and
understandability of a program. For example:
Question 1
From the following, find out which assignment statement will produce an error. State
reason(s) too.
(a) x = 55
(b) y = 037
(c) z = 0o98
(d) 56thnumber = 3300
(e) length = 450.17
(f) !Taylor = 'Instant'
(g) this variable = 87.E02
(h) float = .17E - 03
(i) FLOAT = 0.17E - 03
Answer
1. y = 037 (option b) will give an error as decimal integer literal cannot start with a 0.
2. z = 0o98 (option c) will give an error as 0o98 is an octal integer literal due to the 0o
prefix and 8 & 9 are invalid digits in an octal number.
3. 56thnumber = 3300 (option d) will give an error as 56thnumber is an invalid
identifier because it starts with a digit.
4. !Taylor = 'Instant' (option f) will give an error as !Taylor is an invalid identifier
because it contains the special character !.
5. this variable = 87.E02 (option g) will give an error due to the space present between
this and variable. Identifiers cannot contain any space.
6. float = .17E - 03 (option h) will give an error due to the spaces present in exponent
part (E - 03). A very important point to note here is that float is NOT a
KEYWORD in Python. The statement float = .17E-03 will execute successfully
without any errors in Python.
7. FLOAT = 0.17E - 03 (option i) will give an error due to the spaces present in
exponent part (E - 03).
Question 2
(i) 20 + 30 * 40
Answer
20 + 30 * 40
⇒ 20 + 1200
⇒ 1220
(ii) 20 - 30 + 40
Answer
20 - 30 + 40
⇒ -10 + 40
⇒ 30
Answer
(20 + 30) * 40
⇒ 50 * 40
⇒ 2000
Answer
15.0 / 4 + (8 + 3.0)
⇒ 15.0 / 4 + 11.0
⇒ 3.75 + 11.0
⇒ 14.75
Question 3
(i)
temperature = 90
print temperature
Answer
The call to print function is missing parenthesis. The correct way to call print function is this:
print(temperature)
(ii)
a = 30
b=a+b
print (a And b)
Answer
(iii)
a, b, c = 2, 8, 9
print (a, b, c)
c, b, a = a, b, c
print (a ; b ; c)
Answer
In the statement print (a ; b ; c) use of semicolon will give error. In place of semicolon,
we must use comma like this print (a, b, c)
(iv)
X = 24
4 = X
Answer
print("X ="X)
Answer
1. Variable X is undefined
2. "X =" and X should be separated by a comma like this print("X =", X)
(vi)
else = 21 - 5
Answer
Question 4
(i)
X = 10
X = X + 10
X = X - 5
print (X)
X, Y = X - 2, 22
print (X, Y)
Output
15
13 22
Explanation
(ii)
first = 2
second = 3
third = first * second
print (first, second, third)
first = first + second + third
third = second * first
print (first, second, third)
Output
2 3 6
11 3 33
Explanation
(iii)
Output
side7
7 49
Explanation
1. side = int(input('side') ) ⇒ This statements asks the user to enter the side. We
enter 7 as the value of side.
2. area = side * side ⇒ area = 7 * 7 = 49.
3. print (side, area) ⇒ prints the value of side and area as 7 and 49 respectively.
Question 5
(i)
a = 3
print(a)
b = 4
print(b)
s = a + b
print(s)
Answer
name = "Prejith"
age = 26
print ("Your name & age are ", name + age)
Answer
In the print statement we are trying to add name which is a string to age which is an integer.
This is an invalid operation in Python.
(iii)
a = 3
s = a + 10
a = "New"
q = a / 10
Answer
The statement a = "New" converts a to string type from numeric type due to dynamic typing.
The statement q = a / 10 is trying to divide a string with a number which is an invalid
operation in Python.
Question 6
(a)
x = 40
y = x + 1
x = 20, y + x
print (x, y)
Output
(20, 81) 41
Explanation
(b)
x, y = 20, 60
y, x, y = x, y - 10, x + 10
print (x, y)
Output
50 30
Explanation
(c)
a, b = 12, 13
c, b = a*2, a/2
print (a, b, c)
Output
12 6.0 24
Explanation
(d)
a, b = 12, 13
print (print(a + b))
Output
25
None
Explanation
Question 7
a, b, c = 10, 20, 30
p, q, r = c - 5, a + 3, b - 4
print ('a, b, c :', a, b, c, end = '')
print ('p, q, r :', p, q, r)
Output
a, b, c : 10 20 30p, q, r : 25 13 16
Explanation
Question 8
(a)
y = x + 5
print (x, Y)
Answer
(b)
print (x = y = 5)
Answer
Python doesn't allow assignment of variables while they are getting printed.
(c)
a = input("value")
b = a/2
print (a, b)
Answer
The input( ) function always returns a value of String type so variable a is a string. This
statement b = a/2 is trying to divide a string with an integer which is invalid operation in
Python.
Question 9
Find the errors in following code fragment : (The input entered is XI)
Answer
Question 10
Answer
The print() function appends a newline character at the end of the line unless we give our
own end argument. Due to this behaviour of print() function, the statement print ('Hi',
name, ',1) is printing a newline at the end. Hence "How are you doing?" is getting printed
on the next line.
To fix this we can add the end argument to the first print() function like this:
print ('Hi', name, ',1, end = '')
Question 11
Answer
Question 12
Answer
<class 'int'>
Answer
<class 'int'>
(c) >>>.type(int('0')
Answer
Answer
<class 'str'>
Answer
<class 'float'>
Answer
<class 'int'>
(g) >>>type(float(0))
Answer
<class 'float'>
Answer
<class 'float'>
Answer
<class 'float'>
Question 13
Output
NoneOne
Explanation
print() function doesn't return any value so its return value is None.
Hence, str(print()) becomes str(None). str(None) converts None into string 'None' and
addition operator joins 'None' and 'One' to give the final output as 'NoneOne'.
(b) >>> str(print("hello"))+"One"
Output
hello
NoneOne
Explanation
First, print("hello") function is executed which prints the first line of the output as hello. The
return value of print() function is None i.e. nothing. str() function converts it into string and
addition operator joins 'None' and 'One' to give the second line of the output as 'NoneOne'.
Output
Hola
None
Explanation
First, print("Hola") function is executed which prints the first line of the output as Hola. The
return value of print() function is None i.e. nothing. This is passed as argument to the outer
print function which converts it into string and prints the second line of output as None.
Output
Hola None
Explanation
First, print ("Hola", end = " ") function is executed which prints Hola. As end argument is
specified as " " so newline is not printed after Hola. The next output starts from the same line.
The return value of print() function is None i.e. nothing. This is passed as argument to the
outer print function which converts it into string and prints None in the same line after Hola.
Question 14
Carefully look at the following code and its execution on Python shell. Why is the last
assignment giving error ?
>>> a = 0o12
>>> print(a)
10
>>> b = 0o13
>>> c = 0o78
File "<python-input-41-27fbe2fd265f>", line 1
c = 0o78
^
SyntaxError : invalid syntax
Answer
Due to the prefix 0o, the number is treated as an octal number by Python but digit 8 is invalid
in Octal number system hence we are getting this error.
Question 15
a, b, c = 2, 3, 4
a, b, c = a*a, a*b, a*c
print(a, b, c)
Output
4 6 8
Explanation
Question 16
The id( ) can be used to get the memory address of a variable. Consider the adjacent code and
tell if the id( ) functions will return the same value or not(as the value to be printed via print()
) ? Why ?
[There are four print() function statements that are printing id of variable num in the code
shown on the right.
num = 13
print( id(num) )
num = num + 3
print( id(num) )
num = num - 3
print( id(num) )
num = "Hello"
print( id(num) )
Answer
num = 13
print( id(num) ) # print 1
num = num + 3
print( id(num) ) # print 2
num = num - 3
print( id(num) ) # print 3
num = "Hello"
print( id(num) ) # print 4
For the print statements commented as print 1 and print 3 above, the id() function will return
the same value. For print 2 and print 4, the value returned by id() function will be different.
The reason is that for both print 1 and print 3 statements the value of num is the same which
is 13. So id(num) gives the address of the memory location which contains 13 in the front-
loaded dataspace.
Question 17
Consider below given two sets of codes, which are nearly identical, along with their
execution in Python shell. Notice that first code-fragment after taking input gives error, while
second code-fragment does not produce error. Can you tell why ?
(a)
>>> print(float(input("valuel:")) )
value1:67
67.0
Answer
In part a, the value entered by the user is converted to a float type and passed to the print
function by assigning it to a variable named num. It means that we are passing an argument
named num to the print function. But print function doesn't accept any argument named num.
Hence, we get this error telling us that num is an invalid argument for print function.
In part b, we are converting the value entered by the user to a float type and directly passing it
to the print function. Hence, it works correctly and the value gets printed.
Question 18
Output
Input days : 1
Input hours: 2
Input minutes: 3
Input seconds: 4
Total number of seconds 93784
Question 19
n1, n2 = 5, 7
n3 = n1 + n2
n4 = n4 + 2
print(n1, n2, n3, n4)
Answer
The code will result into an error as in the statement n4 = n4 + 2, variable n4 is undefined.
Question 20
Answer
Question 1
Write a program that displays a joke. But display the punchline only when the user presses
enter key.
(Hint. You may use input( ))
Solution
Output
Why is 6 afraid of 7?
Press Enter
Because 7 8(ate) 9 :-)
Question 2
Write a program to read today's date (only del part) from user. Then display how many days
are left in the current month.
Solution
Output
Question 3
Solution
a = 5
print(a)
a = a * 2
print(a)
a = a - 1
print(a)
Output
5
10
9
Question 4
Solution
a = 5
print(a, end='@')
a = a * 2
print(a, end='@')
a = a - 1
print(a)
Output
5@10@9
Question 5
Write the program with maximum three lines of code and that assigns first 5 multiples of a
number to 5 variables and then print them.
Solution
Output
Enter a number: 2
2 4 6 8 10
Question 6
Write a Python program that accepts radius of a circle and prints its area.
Solution
Output
Enter radius of circle: 7.5
Area of circle = 176.7144375
Question 7
Write Python program that accepts marks in 5 subjects and outputs average marks.
Solution
Output
Question 8
Write a short program that asks for your height in centimetres and then converts your height
to feet and inches. (1 foot = 12 inches, 1 inch = 2.54 cm).
Solution
Output
Question 9
Solution
n = int(input("Enter n: "))
n2, n3, n4 = n ** 2, n ** 3, n ** 4
print("n =", n)
print("n^2 =", n2)
print("n^3 =", n3)
print("n^4 =", n4)
Output
Enter n: 2
n = 2
n^2 = 4
n^3 = 8
n^4 = 16
Question 10
Solution
Output
Question 11
Solution
Output
Question 12
Write a program to input a number and print its first five multiples.
Solution
Output
Enter number: 5
First five multiples of 5 are
5 10 15 20 25
Question 13
Write a program to read details like name, class, age of a student and then print the details
firstly in same line and then in separate lines. Make sure to have two blank lines in these two
different types of prints.
Solution
Output
Name: Kavya
Class: 11
Age: 17
Question 14
Write a program to input a single digit(n) and print a 3 digit number created as <n(n + 1)(n +
2)> e.g., if you input 7, then it should print 789. Assume that the input digit is in range 1-7.
Solution
Output
Question 15
Write a program to read three numbers in three variables and swap first two variables with
the sums of first and second, second and third numbers respectively.
Solution
Output