CHAPTER 7 Python Fundamentals-1
CHAPTER 7 Python Fundamentals-1
PYTHON FUNDAMENTALS
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
(i) sep
(ii) end
if b > 5:
print("Value of 'b' is less than 5.")
print("Thank you.")
13. What is the role of indentation in Python ?
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.
x = 10
print(x)
x = "Hello World"
print(x)
17. What would the following code do : X = Y = 7 ?
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
19. Following variable definition is creating problem X = 0281, find
reasons.
20. "Comments are useful and easy way to enhance readability and
understandability of a program." Elaborate with examples.
(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
(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
(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
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
(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
236
11 3 33
Explanation
(iii)
side7
7 49
Explanation
(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.
(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
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
(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
9. Find the errors in following code fragment : (The input entered is XI)
Question 10
Hi <name>,
How are you doing?
What could be the problem ? Can you suggest the solution for the same ?
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
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
HolaNone
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
468
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
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
Write a program that generates the following output :
5
10
9
Assign value 5 to a variable using assignment operator (=) Multiply it with 2
to generate 10 and subtract 1 to generate 9.
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
Question 7
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
Enter principal: 15217.75
Enter rate: 9.2
Enter time: 3
Simple interest = 4200.098999999999
Compound interest = 4598.357987312007
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