0% found this document useful (0 votes)
175 views9 pages

Chapter 5 Getting Started With Python

The document contains questions and answers related to Python programming. Some key points: - It discusses valid and invalid identifier names in Python and why some are invalid based on syntax rules. - Code examples are provided to demonstrate Python variable assignments, logical expressions, and built-in functions. - The expected output is given for code snippets involving arithmetic, logical, comparison, and string operations in Python. - Data types that would be suitable to represent different data values are explained based on the nature of the data. So in summary, the document covers Python syntax rules for identifiers, provides code examples for common operations, and explains appropriate data types for different data values.

Uploaded by

Seema Sehdev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
175 views9 pages

Chapter 5 Getting Started With Python

The document contains questions and answers related to Python programming. Some key points: - It discusses valid and invalid identifier names in Python and why some are invalid based on syntax rules. - Code examples are provided to demonstrate Python variable assignments, logical expressions, and built-in functions. - The expected output is given for code snippets involving arithmetic, logical, comparison, and string operations in Python. - Data types that would be suitable to represent different data values are explained based on the nature of the data. So in summary, the document covers Python syntax rules for identifiers, provides code examples for common operations, and explains appropriate data types for different data values.

Uploaded by

Seema Sehdev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

NCERT : CHAPTER 5 : GETTING STARTED WITH PYTHON

Question 1:

Which of the following identifier names are invalid and why?


i Serial_no. v Total_Marks
ii 1st_Room vi total-Marks
iii Hundred$ vii _Percentage
i
Total Marks viii True
v
ANSWER:

i) Serial_no.: Invalid - Identifier in python cannot contain any special


character except underscore(_).

ii) 1st_Room: Invalid - Identifier in Python cannot start with a number.

iii) Hundred$:  Invalid - Identifier in Python cannot contain any special


character except underscore(_).

iv) Total Marks: Invalid - Identifier in Python cannot contain any special


character except underscore(_). If more than one word is used as a variable
then it can be separated using underscore ( _ ), instead of space.

v) Total_Marks: Valid

vi) total-Marks: Invalid - Identifier in Python cannot contain any special


character except underscore(_). If more than one word is used as a variable
then it can be separated using underscore ( _ ), instead of a hyphen ( - ).

vii) _Percentage: Valid

viii) True: Invalid - Identifier in Python should not be a reserved keyword.

Question 2:

Write the corresponding Python assignment statements:


a) Assign 10 to variable length and 20 to variable breadth.

b) Assign the average of values of variables length and breadth to a variable


sum
c) Assign a list containing strings ‘Paper’, ‘Gel Pen’, and ‘Eraser’ to a variable
stationery.

d) Assign the strings ‘Mohandas’, ‘Karamchand’, and ‘Gandhi’ to variables


first, middle and last.

e) Assign the concatenated value of string variables first, middle and last to
variable fullname. Make sure to incorporate blank spaces appropriately
between different parts of names.
ANSWER:

a) length, breadth = 10,20

b) sum = (length + breadth)/2

c) stationary =['Paper','Gel Pen','Eraser']

d) first,middle,last = "Mohandas","Karamchand","Gandhi"

e) fullname = first +" "+ middle +" "+ last


Question 3:

Write logical expressions corresponding to the following statements in Python


and evaluate the expressions (assuming variables num1, num2, num3, first,
middle, last are already having meaningful values):

a) The sum of 20 and –10 is less than 12.

b) num3 is not more than 24

c) 6.75 is between the values of integers num1 and num2.

d) The string ‘middle’ is larger than the string ‘first’ and smaller than the string
‘last’

e) List Stationery is empty


ANSWER:

a) (20 + (-10)) < 12

b) num3 <= 24   or not(num3 > 24)


c) (6.75 >= num1) and (6.75 <= num2)

d) (middle > first) and (middle < last)

e) len(Stationery) == 0

Question 4:

Add a pair of parentheses to each expression so that it evaluates to True.

a) 0 == 1 == 2

b) 2 + 3 == 4 + 5 == 7

c) 1 < -1 == 3 > 4
ANSWER:

a) ( 0 == (1==2))

b) (2 + (3 == 4) + 5) == 7

c) (1 < -1) == (3 > 4 )

Question 5:

Write the output of the following.

a) num1 = 4
  num2 = num1 + 1
  num1 = 2
  print (num1, num2)

b) num1, num2 = 2, 6
  num1, num2 = num2, num1 + 2
  print (num1, num2)

c) num1, num2 = 2, 3
  num3, num2 = num1, num3 + 1
  print (num1, num2, num3)
ANSWER:

a) 2, 5

b) 6, 4

c) Error as num3 is used in RHS of line 2 (num3, num2 = num1, num3 + 1)


before defining it earlier.

Question 6:

Which data type will be used to represent the following data values and why?

a) Number of months in a year

b) Resident of Delhi or not

c) Mobile number

d) Pocket money

e) Volume of a sphere

f) Perimeter of a square

g) Name of the student

h) Address of the student


ANSWER:

a) The int data type will be used to represent 'Number of months in a year' as
it will be an integer i.e. 12.

b) The boolean data type will be used to represent 'Resident of Delhi or not'
as a person will be either a resident of Delhi or not a resident of Delhi.
Therefore, the values True or False will be sufficient to represent the values.

c) The integer data type will be used to represent 'Mobile number' as it will be
a ten-digit integer only.
d) The float data type will be used to represent 'Pocket money' as it can be in
decimal. e.g Rs. 250.50 i.e 250 rupees and 50 paise.

e) The float data type will be used to represent 'Volume of a sphere'.


The formula for the volume of a sphere:
Volume of sphere, V=43πr3
Even if 'r' is a whole number, the value of volume can be a fraction which can
be represented in a decimal form easily by float data type.

f) The float data type will be used to represent 'Perimeter of a square'.


The perimeter of a square:
Perimeter = 4 × side length
If the side length is a decimal number, the result may come out to be a
decimal number which can be easily represented by float data type.

Note:- If the side length is a whole number, the perimeter will always be an
integer, however, we should be open to the possibility that the side length
can be in decimal as well.

g) The string data type will be used to represent 'Name of the student'.

h)The string data type will be used to represent 'Address of the student'.
However, if we have to store the address in a more structured format, we can
use dictionary data type as well. e.g. Address = { 'Line1': ‘Address line 1',
'Line2':'Address Line2', 'Locality':'Locality Name', 'City':'City Name',
'Pincode':110001, 'Country':'India'}

Question 7:

Give the output of the following when num1 = 4, num2 = 3, num3 = 2

a) num1 += num2 + num3


   print (num1)

b) num1 = num1 ** (num2 + num3)


  print (num1)

c) num1 **= num2 + num3

d) num1 = '5' + '5'


  print(num1)
e) print(4.00/(2.0+2.0))

f) num1 = 2+9*((3*12)-8)/10
  print(num1)

g) num1 = 24 // 4 // 2
  print(num1)

h) num1 = float(10)
    print (num1)

i) num1 = int('3.14')
  print (num1)

j) print('Bye' == 'BYE')

k) print(10 != 9 and 20 >= 20)

l) print(10 + 6 * 2 ** 2 != 9//4 -3 and 29>= 29/9)

m) print(5 % 10 + 10 < 50 and 29 <= 29)

n) print((0 < 6) or (not (10 == 6) and (10<0)))


ANSWER:

a) num1 += 3 + 2
The above statement can be written as
num1 = num1 + 3 + 2 = 4 + 3 + 2 = 9
Therefore, print(num1) will give the output 9.

b) num1 = num1 ** (num2 + num3)


The above statement will be executed as per the following steps.
num1 = 4 ** (3 + 5) = 4 ** 5 = 1024
Therefore, print(num1) will give the output 1024.

c) num1 **= num2 + num3


The above statement can be written as
num1 **= 5
num1 = num1 ** 5
num1 = 4 ** 5
num1 = 1024
Therefore, the output will be 1024.
d) num1 = '5' + '5'
The RHS in the above statement is '5' + '5'. Please note that 5 is enclosed in
quotes and hence will be treated as a string. Therefore, the first line is just a
string concatenation which will give the output 55. The type of output will be a
string, not an integer.

e) print(4.00/(2.0 + 2.0))
The numbers written in the statement are in float data type therefore, the
output will be also in float data type.
print(4.00/(2.0 + 2.0))
print(4.0/4.0)
1.0
Therefore, the output will be 1.0.

f) num1 = 2 + 9 * ((3 * 12) - 8) /10


 # The expression within inner brackets will be evaluated first
num1 = 2 + 9 * (36 - 8) /10 
# The expression within outer brackets will be evaluated next         
num1 = 2 + 9 * 28/10
# * and / are of same precedence, hence, left to right order is followed
num1 = 2 + 252/10           
num1 = 2 + 25.2           
num1 = 27.2
Therefore, the output will be 27.2.

g) num1 = 24 // 4 // 2
#When the operators are same, left to right order will be followed for
operation
num1 = 6 // 2  
#When floor division is used, return value will be int data type     
num1 = 3       
Therefore, the output will be 3

h) num1 = float(10)
float(10) will convert integer value to float value and therefore, the output will
be 10.0.

i) num1 = int('3.14')
This will result in an error as we cannot pass string representation of float to
an int function.
j) print('Bye' == 'BYE')
As Python compares string character to character and when different
characters are found then their Unicode value is compared. The character
with lower Unicode value is considered to be smaller. Here, 'y' has Unicode
121 and 'Y' has 89. Therefore, the output will be 'False'.

k) print(10 != 9 and 20 >= 20)


print(True and True)
print(True)
Therefore, the output will be 'True'.

l) print(10 + 6 * 2 ** 2 != 9//4 -3 and 29>= 29/9)


Taking the above statement in two separate parts

LHS:
10 + 6 * 2 ** 2 != 9//4 - 3
10 + 6 * 4 != 2 - 3
10 + 24 != -1
34 != -1
True

RHS:
29 >= 29/9
True

Now the complete equation can be written as


print(True and True)
Therefore, the output will be 'True'.

m) print(5 % 10 + 10 < 50 and 29 <= 29)


Taking the above statement in two separate parts

LHS :
5 % 10 + 10 < 50
5 + 10 < 50
15 < 50
True

RHS:
29 <= 29
True
Now, the complete equation can be written as
print(True and True)
Therefore, the output will be 'True'.

n) print( (0 < 6) or (not (10 == 6) and (10<0) ) )


print(True or (not False and False))
print(True or (True and False))
# not will be evaluated before and/or.
print(True or False)
print(True)
Therefore, the output will be 'True'.

You might also like