Looking for a Tutor Near You?

Post Learning Requirement »
x

Choose Country Code

x

Direction

x

Ask a Question

x

x
x
x
Hire a Tutor

Note On Python Fundamentals

Loading...

Including questions and answers with MCQ's and codes solved.

Prantik S / Kolkata

11 years of teaching experience

Qualification: MCA (Jaipur National University - [JNU], Jaipur - 2017)

Teaches: Basic Computer, Computer for official job, MS Office, School Level Computer, ICT Training, Computer Science, Information Practice, IT & Computer Subjects, BCA Tuition, IT, Computer, C / C++, C# (C Sharp), Java And J2EE, Python Programming, Visual Basic, BCA Subjects, Hardware Training, Networking, Java Script

Contact this Tutor
  1. Multiple Choice Questions Question 1 Special meaning words of Pythons, fixed for specific functionality are called Identifiers 2. functions 3. Keywords v/ 4. literals Question 2 Names given to different parts of a Python program are 1. Identifiers 2. functions 3. Keywords 4. literals Question 3 Data items having fixed value are called Identifiers 2. functions 3. Keywords 4. literals Question 4 Which of the following is/are correct ways of creating strings ? 1. name = Jiya 2. name = 'Jiya' 3. name = "Jiya" ../ = (Jiya) 4. name Question 5 Which of the following are keyword(s) ? 1. name
  2. 2. 3. 4. Print print input v/ Question 6 Which of the following are valid identifiers ? 1. 2. 3. 4. my name _myname v/ myname my-name Question 7 Which of the following are literals ? 1. 2. 3. 4. myname "Radha" v/ 24.5 24A Question 8 Escape sequences are treated as 1. 2. 3. 4. strings characters v/ integers none of these Question 9 Which of the following is an escape sequence for a tab character ? 2. 3. 4. \n \b Question 10 Which of the following is an escape sequence for a newline character ?
  3. 2. 3. 4. \t \b Question 11 Which of the following is not a legal integer type value in Python ? 1. 2. 3. 4. Decimal Octal Hexadecimal Roman v/ Question 12 Which of the following symbols are not legal in an octal value ? 7 2. 3. 4. Question 13 Value 17.25 is equivalent to 1. 2. 3. 4. 0.172±-2 0.1725E+2 1725E2 0.17252 Question 14 Value 0.000615 is equivalent to 1. 2. 3. 4. 61 5E3 61 5E-3 0.61 5E3 0.615E-3 Question 15 Which of the following is/are expression(s) ?
  4. Multiple Choice Questions Question 1 Special meaning words of Pythons, fixed for specific functionality are called Identifiers 2. functions 3. Keywords v/ 4. literals Question 2 Names given to different parts of a Python program are 1. Identifiers 2. functions 3. Keywords 4. literals Question 3 Data items having fixed value are called Identifiers 2. functions 3. Keywords 4. literals Question 4 Which of the following is/are correct ways of creating strings ? 1. name = Jiya 2. name = 'Jiya' 3. name = "Jiya" ../ = (Jiya) 4. name Question 5 Which of the following are keyword(s) ? 1. name
  5. 1. 2. 3. 4. a+b-5 -5 Question 16 The lines beginning with a certain character, and which are ignored by a compiler and not executed, are called 1. 2. 3. 4. operators operands functions comments v/ Question 17 Which of the following can be used to create comments ? 2. 3. 4. Question 18 Which of the following functions print the output to the console ? 1. Output() 2. Print() 3. Echo() 4. print() v/ Question 19 Select the reserved keyword in Python. 1. 2. 3. 4. else import print all of these ../
  6. 2. 3. 4. Print print input v/ Question 6 Which of the following are valid identifiers ? 1. 2. 3. 4. my name _myname v/ myname my-name Question 7 Which of the following are literals ? 1. 2. 3. 4. myname "Radha" v/ 24.5 24A Question 8 Escape sequences are treated as 1. 2. 3. 4. strings characters v/ integers none of these Question 9 Which of the following is an escape sequence for a tab character ? 2. 3. 4. \n \b Question 10 Which of the following is an escape sequence for a newline character ?
  7. Question 20 The input( ) returns the value as type. integer 2. string v/ 3. floating point 4. none of these Question 21 To convert the read value through input( ) into integer type ( ) is used. 2. 3. 4. floating float int integer Question 22 To convert the read value through input( ) into a floating point number ( ) is used. 2. 3. 4. floating float v/ int integer Question 23 argument is used with print( ) To print a line a text without ending it with a newline 1. 2. 3. 4. sep newline end next Question 24 The default separator character of print( ) is 2. 3. 4. tab space v/ newline dot
  8. 2. 3. 4. \t \b Question 11 Which of the following is not a legal integer type value in Python ? 1. 2. 3. 4. Decimal Octal Hexadecimal Roman v/ Question 12 Which of the following symbols are not legal in an octal value ? 7 2. 3. 4. Question 13 Value 17.25 is equivalent to 1. 2. 3. 4. 0.172±-2 0.1725E+2 1725E2 0.17252 Question 14 Value 0.000615 is equivalent to 1. 2. 3. 4. 61 5E3 61 5E-3 0.61 5E3 0.615E-3 Question 15 Which of the following is/are expression(s) ?
  9. Question 25 To give a different separator with print( ) argument is used. 1. 2. 3. 4. sep ../ separator end tab Fill in the Blanks Question 1 A keyword is a reserved word carrying special meaning and purpose. Question 2 Identifiers are the user defined names for different parts of a program. Question 3 Literals are the fixed values. Question 4 Operators are the symbols that trigger some computation or action. Question 5 An expression is a legal combination of symbols that represents a value. Question 6 Non-executable, additional lines added to a program, are known as comments. Question 7 In Python, the comments begin with # character. Question 8 Python is a case sensitive language.
  10. 1. 2. 3. 4. a+b-5 -5 Question 16 The lines beginning with a certain character, and which are ignored by a compiler and not executed, are called 1. 2. 3. 4. operators operands functions comments v/ Question 17 Which of the following can be used to create comments ? 2. 3. 4. Question 18 Which of the following functions print the output to the console ? 1. Output() 2. Print() 3. Echo() 4. print() v/ Question 19 Select the reserved keyword in Python. 1. 2. 3. 4. else import print all of these ../
  11. Question 9 The print( ) function prints the value of a variable/expression. Question 10 The input( ) function gets the input from the user. Question 11 The input( ) function returns the read value as of string type. Question 12 To convert an input( value in integer type, int( ) function is used. Question 13 To convert an input( value in floating-point type, float( ) function is used. Question 14 Strings can be created with single quotes, double quotes and triple quotes. True/False Questions Question 1 Keywords can be used as identifier names. False Question 2 The identifiers in Python can begin with an underscore. True Question 3 0128 is a legal literal value in Python. False Question 4 Oxl 2EFG is a legal literal value in Python. False
  12. Question 20 The input( ) returns the value as type. integer 2. string v/ 3. floating point 4. none of these Question 21 To convert the read value through input( ) into integer type ( ) is used. 2. 3. 4. floating float int integer Question 22 To convert the read value through input( ) into a floating point number ( ) is used. 2. 3. 4. floating float v/ int integer Question 23 argument is used with print( ) To print a line a text without ending it with a newline 1. 2. 3. 4. sep newline end next Question 24 The default separator character of print( ) is 2. 3. 4. tab space v/ newline dot
  13. Question 5 0123 is a legal literal value in Python. True Question 6 Variables once assigned a value can be given any other value. True Question 7 Variables are created when they are first assigned their value. True Question 8 Python variables support dynamic typing. True Question 9 You can rename a keyword. False Question 10 String values in Python can be single line strings, and multi-line strings. True Question 11 A variable can contain values of different types at different times. True Question 12 Expressions contain values/variables along with operators. True Type A : Short Answer Questions/Conceptual Questions Question 1 What are tokens in Python ? How many types of tokens are allowed in Python ? Examplify your answer.
  14. Question 25 To give a different separator with print( ) argument is used. 1. 2. 3. 4. sep ../ separator end tab Fill in the Blanks Question 1 A keyword is a reserved word carrying special meaning and purpose. Question 2 Identifiers are the user defined names for different parts of a program. Question 3 Literals are the fixed values. Question 4 Operators are the symbols that trigger some computation or action. Question 5 An expression is a legal combination of symbols that represents a value. Question 6 Non-executable, additional lines added to a program, are known as comments. Question 7 In Python, the comments begin with # character. Question 8 Python is a case sensitive language.
  15. Answer The smallest individual unit in a program is known as a Token. Python has following tokens: 1. 2. 3. 4. 5. Keywords — Examples are import, for, in, while, etc. Identifiers - Examples are MyFile, _DS, etc. Literals — Examples are "abc", 5, 28.5, etc. Operators — Examples are +, -, >, or, etc. # () etc. Punctuators Question 2 How are keywords different from identifiers ? 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. 2. 3. 4. 5. String literals Numeric literals Boolean literals Special literal None 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.
  16. Question 9 The print( ) function prints the value of a variable/expression. Question 10 The input( ) function gets the input from the user. Question 11 The input( ) function returns the read value as of string type. Question 12 To convert an input( value in integer type, int( ) function is used. Question 13 To convert an input( value in floating-point type, float( ) function is used. Question 14 Strings can be created with single quotes, double quotes and triple quotes. True/False Questions Question 1 Keywords can be used as identifier names. False Question 2 The identifiers in Python can begin with an underscore. True Question 3 0128 is a legal literal value in Python. False Question 4 Oxl 2EFG is a legal literal value in Python. False
  17. 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: 1. Fractional Form - 2.0, 17.5, -13.0, -0.00625 2. Exponent form - 152E05, 1.52E07, 0.152E08, -0.172+3 Question 6 How are string-literals represented and implemented in Python ? Answer A string-literal is represented as a sequence of characters surrounded by quotes (single, double or triple quotes). String-literals in Python are implemented using Unicode. Question 7 Which of these is not a legal numeric type in Python ? (a) int (b) float (c) decimal. Answer decimal is not a legal numeric type in Python. Question 8 Which argument of print( ) would you set for: (i) changing the default separator (space) ? (ii) printing the following line in current line ? 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 (N), Logical negation
  18. Question 5 0123 is a legal literal value in Python. True Question 6 Variables once assigned a value can be given any other value. True Question 7 Variables are created when they are first assigned their value. True Question 8 Python variables support dynamic typing. True Question 9 You can rename a keyword. False Question 10 String values in Python can be single line strings, and multi-line strings. True Question 11 A variable can contain values of different types at different times. True Question 12 Expressions contain values/variables along with operators. True Type A : Short Answer Questions/Conceptual Questions Question 1 What are tokens in Python ? How many types of tokens are allowed in Python ? Examplify your answer.
  19. (not) are a few examples of unary operators. Examples of binary operators are Addition (+), Subtraction (-), Multiplication (*), Division (/). Question 10 What is an expression and a statement ? Answer An expression is any legal combination of symbols that represents a value. For example, 2.9, a + 5, A statement is a programming instruction that does something i.e. some action takes place. For example: print("Hello") a = 15 b = a-IO Question 11 What all components can a Python program contain ? Answer A Python program can contain various components like expressions, statements, comments, functions, blocks and indentation. Question 12 What do you understand by block/code block/suite in Python ? Answer A block/code block/suite is a group of statements that are part of another statement. For example: if print ("Value of 'b' is less than 5. print ("Thank you. ") Question 13 What is the role of indentation in Python ? Answer Python uses indentation to create blocks of code. Statements at same indentation level are part of same block/suite.
  20. Answer The smallest individual unit in a program is known as a Token. Python has following tokens: 1. 2. 3. 4. 5. Keywords — Examples are import, for, in, while, etc. Identifiers - Examples are MyFile, _DS, etc. Literals — Examples are "abc", 5, 28.5, etc. Operators — Examples are +, -, >, or, etc. # () etc. Punctuators Question 2 How are keywords different from identifiers ? 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. 2. 3. 4. 5. String literals Numeric literals Boolean literals Special literal None 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.
  21. Question 14 What are variables ? How are they important for a program ? 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 What do you understand by undefined variable in Python ? 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: print (x) #This statement will cause an error for undefined variable x - 20 x print (x) The first line of the above code snippet will cause an undefined variable error as we are trying to use x before assigning a value to it. Question 16 What is Dynamic Typing feature of Python ? Answer A variable pointing to a value of a certain type can be made to point to a value/object of different type. This is called Dynamic Typing. For example: 10 x print (x) "Hello World" x print (x) Question 17 What would the following code do : X = Y = 7? Answer It will assign a value of 7 to the variables X and Y.
  22. 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: 1. Fractional Form - 2.0, 17.5, -13.0, -0.00625 2. Exponent form - 152E05, 1.52E07, 0.152E08, -0.172+3 Question 6 How are string-literals represented and implemented in Python ? Answer A string-literal is represented as a sequence of characters surrounded by quotes (single, double or triple quotes). String-literals in Python are implemented using Unicode. Question 7 Which of these is not a legal numeric type in Python ? (a) int (b) float (c) decimal. Answer decimal is not a legal numeric type in Python. Question 8 Which argument of print( ) would you set for: (i) changing the default separator (space) ? (ii) printing the following line in current line ? 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 (N), Logical negation
  23. Question 18 What is the error in following code : X, Y = 7? 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: Question 19 Following variable definition is creating problem X = 0281, find reasons. 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 understandabil ity 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: # This program shows a program's components # Definition of function SeeYou() follows def SeeYou print ("Time to say Good Bye! ! print ( "Value print ( "Value # Main program-code 15 a b 10 a (a print if else. SeeYou 3) follows now colon means it's a block of of a' was a' was calling more than 15 initially. 15 or less initially. above defined function SeeYou()
  24. (not) are a few examples of unary operators. Examples of binary operators are Addition (+), Subtraction (-), Multiplication (*), Division (/). Question 10 What is an expression and a statement ? Answer An expression is any legal combination of symbols that represents a value. For example, 2.9, a + 5, A statement is a programming instruction that does something i.e. some action takes place. For example: print("Hello") a = 15 b = a-IO Question 11 What all components can a Python program contain ? Answer A Python program can contain various components like expressions, statements, comments, functions, blocks and indentation. Question 12 What do you understand by block/code block/suite in Python ? Answer A block/code block/suite is a group of statements that are part of another statement. For example: if print ("Value of 'b' is less than 5. print ("Thank you. ") Question 13 What is the role of indentation in Python ? Answer Python uses indentation to create blocks of code. Statements at same indentation level are part of same block/suite.
  25. Type B: Application Based Questions 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 = 0098 (d) 56thnumber = 3300 (e) length = 450.17 (f) ! Taylor = 'Instantl (g) this variable = 87.E02 (h) float = .17E-03 (i) FLOAT = 0.17E - 03 Answer 1. 2. 3. 4. 5. 6. 7. y = 037 (option b) will give an error as decimal integer literal cannot start with a 0. z = 0098 (option c) will give an error as 0098 is an octal integer literal due to the 00 prefix and 8 & 9 are invalid digits in an octal number. 56thnumber = 3300 (option d) will give an error as 56thnumber is an invalid identifier because it starts with a digit. ! Taylor = 'Instantl (option f) will give an error as ! Taylor is an invalid identifier because it contains the special character !. this variable = 87.E02 (option g) will give an error due to the space present between this and variable. Identifiers cannot contain any space. 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. FLOAT = 0.17E - 03 (option i) will give an error due to the spaces present in exponent part (E - 03). Question 2 How will Python evaluate the following expression ? (i) 20 + 30 * 40 Answer 20 + 30 * 40 20 + 1200 (ii) 20 - 30 + 40
  26. Question 14 What are variables ? How are they important for a program ? 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 What do you understand by undefined variable in Python ? 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: print (x) #This statement will cause an error for undefined variable x - 20 x print (x) The first line of the above code snippet will cause an undefined variable error as we are trying to use x before assigning a value to it. Question 16 What is Dynamic Typing feature of Python ? Answer A variable pointing to a value of a certain type can be made to point to a value/object of different type. This is called Dynamic Typing. For example: 10 x print (x) "Hello World" x print (x) Question 17 What would the following code do : X = Y = 7? Answer It will assign a value of 7 to the variables X and Y.
  27. Answer 20 -30 + 40 (iii) (20 + 30) * 40 Answer (20 + 30) * 40 2000 (iv) 15.0 / 4 + (8 + 3.0) Answer 15.0 / 4+ (8 + 3.0) 14.75 Question 3 Find out the error(s) in following code fragments: temperature _ 90 print temperature Answer The call to print function is missing parenthesis. The correct way to call print function is this: print (temperature) 30 a b=a+b (a b) print And Answer There are two errors in this code fragment: In the statement b=a+b variable b is undefined. 2. In the statement print (a And b), And should be written as and. (iii)
  28. Question 18 What is the error in following code : X, Y = 7? 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: Question 19 Following variable definition is creating problem X = 0281, find reasons. 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 understandabil ity 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: # This program shows a program's components # Definition of function SeeYou() follows def SeeYou print ("Time to say Good Bye! ! print ( "Value print ( "Value # Main program-code 15 a b 10 a (a print if else. SeeYou 3) follows now colon means it's a block of of a' was a' was calling more than 15 initially. 15 or less initially. above defined function SeeYou()
  29. (a, print (a print Answer 2, b, a, b c) c) In the statement print (a c) use of semicolon will give error. In place of semicolon, we b must use comma like this print (a, b, c) 24 Answer The statement 4 = print ("X - x) Answer X is incorrect as 4 cannot be a Lvalue. It is a Rvalue. There are two errors in this code fragment: 1. Variable X is undefined 2. "X =" and X should be separated by a comma like this print( "X - x) (vi) else Answer 21 5 else is a keyword in Python so it cant be used as a variable name. Question 4 What will be the output produced by following code fragment (s) ? x x 10 -x + 10 5 print print Output 22
  30. Type B: Application Based Questions 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 = 0098 (d) 56thnumber = 3300 (e) length = 450.17 (f) ! Taylor = 'Instantl (g) this variable = 87.E02 (h) float = .17E-03 (i) FLOAT = 0.17E - 03 Answer 1. 2. 3. 4. 5. 6. 7. y = 037 (option b) will give an error as decimal integer literal cannot start with a 0. z = 0098 (option c) will give an error as 0098 is an octal integer literal due to the 00 prefix and 8 & 9 are invalid digits in an octal number. 56thnumber = 3300 (option d) will give an error as 56thnumber is an invalid identifier because it starts with a digit. ! Taylor = 'Instantl (option f) will give an error as ! Taylor is an invalid identifier because it contains the special character !. this variable = 87.E02 (option g) will give an error due to the space present between this and variable. Identifiers cannot contain any space. 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. FLOAT = 0.17E - 03 (option i) will give an error due to the spaces present in exponent part (E - 03). Question 2 How will Python evaluate the following expression ? (i) 20 + 30 * 40 Answer 20 + 30 * 40 20 + 1200 (ii) 20 - 30 + 40
  31. 15 13 22 Explanation 1. 2. 3. 4. 5. 6. X = 10 assigns an initial value of 10 to X. X = X + 10 10+10 = 20. so value of X is now 20. X = X - now 15. print (X) print the value of X which is 15. X, Y = X - 2, 22 -X, Y print (X, Y) prints the value of X which is 13 and Y which is 22. first second third print first third print Output 236 2 3 - first second (first, second, third) - first third second first second (first, second, third) 11 3 33 Explanation 1. 2. 3. 4. 5. 6. 7. (iii) side area print Output side7 7 49 first = 2 assigns an initial value of 2 to first. 3 assigns an initial value of 3 to second. second = third - — first * second third = 2* 3 = 6. So variable third is initialized with a value of 6. print (first, second, third) prints the value of first, second, third as 2, 3 and 6 respectively. first = first + second + third *first = 2+3+6=11 third = second * first •third =3*11 = 33 print (first, second, 33 respectively. int (input( ' side') side side (side, area) third) prints the value of first, second, third as 11, 3 and #side given as 7
  32. Answer 20 -30 + 40 (iii) (20 + 30) * 40 Answer (20 + 30) * 40 2000 (iv) 15.0 / 4 + (8 + 3.0) Answer 15.0 / 4+ (8 + 3.0) 14.75 Question 3 Find out the error(s) in following code fragments: temperature _ 90 print temperature Answer The call to print function is missing parenthesis. The correct way to call print function is this: print (temperature) 30 a b=a+b (a b) print And Answer There are two errors in this code fragment: In the statement b=a+b variable b is undefined. 2. In the statement print (a And b), And should be written as and. (iii)
  33. Explanation 1. 2. 3. int (input( ' side') ) This statements asks the user to enter the side. We side - enter 7 as the value of side. side = 49. area print (side, area) prints the value of side and area as 7 and 49 respectively. Question 5 What is the problem with the following code fragments ? a b s 3 print (b) print b a print(s) Answer The problem with the above code is inconsistent indentation. The statements print (a), print (b), print(s) are indented but they are not inside a suite. In Python, we cannot indent a statement unless it is inside a suite and we can indent only as much is required. name age 'Prej ith " 26 print ("Your name & age are age) name 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 s a q 3 'New" / 10 a Answer The statement a The statement q in Python. "New" converts a to string type from numeric type due to dynamic typing. a / 10 is trying to divide a string with a number which is an invalid operation
  34. (a, print (a print Answer 2, b, a, b c) c) In the statement print (a c) use of semicolon will give error. In place of semicolon, we b must use comma like this print (a, b, c) 24 Answer The statement 4 = print ("X - x) Answer X is incorrect as 4 cannot be a Lvalue. It is a Rvalue. There are two errors in this code fragment: 1. Variable X is undefined 2. "X =" and X should be separated by a comma like this print( "X - x) (vi) else Answer 21 5 else is a keyword in Python so it cant be used as a variable name. Question 4 What will be the output produced by following code fragment (s) ? x x 10 -x + 10 5 print print Output 22
  35. Question 6 Predict the output: - 40 x y x 20, print Output (20, 81) 41 Explanation 1. 2. 3. 4. (b) print Output 50 30 x = 40 assigns an initial value of 40 to x. y = x + = 41. Soy becomes 41. - 20, y + x •x = 20, 41 + 40 •x = 20, 81 . This makes xa Tuple of 2 elements (20, x 81). print (x, y) prints the tuple x and the integer variable y as (20, 81) and 41 respectively. 20, y (x, 60 y) - 20, - 10, x Explanation 1. 2. 3. (c) print 60 assigns an initial value of 20 to x and 60 to y. 10, x + 10 y, x, y = 20, 60-10, 20 +10 First RHS value 20 is assigned to first LHS variable y. After that second RHS value 50 is assigned to second LHS variable x. Finally third RHS value 30 is assigned to third LHS variable which is again y. After this assignment, x becomes 50 and y becomes 30. print (x, y) prints the value of x and y as 50 and 30 respectively. 12, 13
  36. 15 13 22 Explanation 1. 2. 3. 4. 5. 6. X = 10 assigns an initial value of 10 to X. X = X + 10 10+10 = 20. so value of X is now 20. X = X - now 15. print (X) print the value of X which is 15. X, Y = X - 2, 22 -X, Y print (X, Y) prints the value of X which is 13 and Y which is 22. first second third print first third print Output 236 2 3 - first second (first, second, third) - first third second first second (first, second, third) 11 3 33 Explanation 1. 2. 3. 4. 5. 6. 7. (iii) side area print Output side7 7 49 first = 2 assigns an initial value of 2 to first. 3 assigns an initial value of 3 to second. second = third - — first * second third = 2* 3 = 6. So variable third is initialized with a value of 6. print (first, second, third) prints the value of first, second, third as 2, 3 and 6 respectively. first = first + second + third *first = 2+3+6=11 third = second * first •third =3*11 = 33 print (first, second, 33 respectively. int (input( ' side') side side (side, area) third) prints the value of first, second, third as 11, 3 and #side given as 7
  37. Output 12 6.0 24 Explanation ('a, ('p, 1. 2. 3. (d) print Output 25 None 12, 13 assigns an initial value of 12 to a and 13 to b. a* 2, a/ 2 c, b = 12*2, 12/2 c, b = 24, 6.0. So c has a value of 24 and b has a value of 6.0. print (a, b, c) prints the value of a, b, cas 12, 6.0 and 24 respectively. 12, 13 b)) print Explanation 12, 13 assigns an initial value of 12 to a and 13 to b. 2. print (print (a + b)) First print(a + b) function is called which prints 25. After that, the outer print statement prints the value returned by print(a + b) function call. As print function does not return any value so outer print function prints None. Question 7 Predict the output print print Output c c 10, c b, 10 20, c 20 30 a, p, b b, q, c, end r) 25 13 16 Explanation 30p, q, - 10, 20, 1. 2. 3. 30 assigns initial value of 10 to a, 20 to b and 30 to c. c , , =30-5, 10+3, 20-4 , , = 25, 13, 16. So p is 25, q is 13 and ris 16. print ('a, b, c 10 20 30. As we have given end = same line. c, ) This statement prints a, b, c end = so output of next print statement will start in the
  38. Multiple Choice Questions Question 1 Special meaning words of Pythons, fixed for specific functionality are called Identifiers 2. functions 3. Keywords v/ 4. literals Question 2 Names given to different parts of a Python program are 1. Identifiers 2. functions 3. Keywords 4. literals Question 3 Data items having fixed value are called Identifiers 2. functions 3. Keywords 4. literals Question 4 Which of the following is/are correct ways of creating strings ? 1. name = Jiya 2. name = 'Jiya' 3. name = "Jiya" ../ = (Jiya) 4. name Question 5 Which of the following are keyword(s) ? 1. name
  39. Explanation 1. 2. 3. int (input( ' side') ) This statements asks the user to enter the side. We side - enter 7 as the value of side. side = 49. area print (side, area) prints the value of side and area as 7 and 49 respectively. Question 5 What is the problem with the following code fragments ? a b s 3 print (b) print b a print(s) Answer The problem with the above code is inconsistent indentation. The statements print (a), print (b), print(s) are indented but they are not inside a suite. In Python, we cannot indent a statement unless it is inside a suite and we can indent only as much is required. name age 'Prej ith " 26 print ("Your name & age are age) name 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 s a q 3 'New" / 10 a Answer The statement a The statement q in Python. "New" converts a to string type from numeric type due to dynamic typing. a / 10 is trying to divide a string with a number which is an invalid operation
  40. 4. print ('p, Question 8 p, q, r) This statement prints p, 25 13 16 Find the errors in following code fragment (x, print Answer There are two errors in this code fragment: 1. x is undefined in the statement y = x + 5 2. Yis undefined in the statement print (x, Y). As Python is case-sensitive hence y and Y will be treated as two different variables. (b) print Answer 5) Python doesn't allow assignment of variables while they are getting printed. (c) a b input ( "value" ) print 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 Xl) int (input 'Enter your class") ) c print ("Your class is" c) Answer The input value Xl is not int type compatible.
  41. Question 6 Predict the output: - 40 x y x 20, print Output (20, 81) 41 Explanation 1. 2. 3. 4. (b) print Output 50 30 x = 40 assigns an initial value of 40 to x. y = x + = 41. Soy becomes 41. - 20, y + x •x = 20, 41 + 40 •x = 20, 81 . This makes xa Tuple of 2 elements (20, x 81). print (x, y) prints the tuple x and the integer variable y as (20, 81) and 41 respectively. 20, y (x, 60 y) - 20, - 10, x Explanation 1. 2. 3. (c) print 60 assigns an initial value of 20 to x and 60 to y. 10, x + 10 y, x, y = 20, 60-10, 20 +10 First RHS value 20 is assigned to first LHS variable y. After that second RHS value 50 is assigned to second LHS variable x. Finally third RHS value 30 is assigned to third LHS variable which is again y. After this assignment, x becomes 50 and y becomes 30. print (x, y) prints the value of x and y as 50 and 30 respectively. 12, 13
  42. 2. 3. 4. Print print input v/ Question 6 Which of the following are valid identifiers ? 1. 2. 3. 4. my name _myname v/ myname my-name Question 7 Which of the following are literals ? 1. 2. 3. 4. myname "Radha" v/ 24.5 24A Question 8 Escape sequences are treated as 1. 2. 3. 4. strings characters v/ integers none of these Question 9 Which of the following is an escape sequence for a tab character ? 2. 3. 4. \n \b Question 10 Which of the following is an escape sequence for a newline character ?
  43. Output 12 6.0 24 Explanation ('a, ('p, 1. 2. 3. (d) print Output 25 None 12, 13 assigns an initial value of 12 to a and 13 to b. a* 2, a/ 2 c, b = 12*2, 12/2 c, b = 24, 6.0. So c has a value of 24 and b has a value of 6.0. print (a, b, c) prints the value of a, b, cas 12, 6.0 and 24 respectively. 12, 13 b)) print Explanation 12, 13 assigns an initial value of 12 to a and 13 to b. 2. print (print (a + b)) First print(a + b) function is called which prints 25. After that, the outer print statement prints the value returned by print(a + b) function call. As print function does not return any value so outer print function prints None. Question 7 Predict the output print print Output c c 10, c b, 10 20, c 20 30 a, p, b b, q, c, end r) 25 13 16 Explanation 30p, q, - 10, 20, 1. 2. 3. 30 assigns initial value of 10 to a, 20 to b and 30 to c. c , , =30-5, 10+3, 20-4 , , = 25, 13, 16. So p is 25, q is 13 and ris 16. print ('a, b, c 10 20 30. As we have given end = same line. c, ) This statement prints a, b, c end = so output of next print statement will start in the
  44. Question 10 Consider the following code : input ("What is your name?" ) name ('Hi' print name, ("How are you doing?" ) print was intended to print output as Hi , How are you doing ? But it is printing the output as . Hi , 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' ',1, end = name, Question 11 Find the errors in following code fragment : input( "Enter your class" ) c ("Last year you were in class") c print Answer There are two errors in this code fragment: 1 1 is outside the parenthesis of print function. It should be specified as one of the arguments of print function. 2. cis a string as input function returns a string. With c 1, we are trying to subtract a integer from a string which is an invalid operation in Python. The corrected program is like this: int (input "Enter your c ("Last year you were print class" ) ) in class" c 1)
  45. 2. 3. 4. \t \b Question 11 Which of the following is not a legal integer type value in Python ? 1. 2. 3. 4. Decimal Octal Hexadecimal Roman v/ Question 12 Which of the following symbols are not legal in an octal value ? 7 2. 3. 4. Question 13 Value 17.25 is equivalent to 1. 2. 3. 4. 0.172±-2 0.1725E+2 1725E2 0.17252 Question 14 Value 0.000615 is equivalent to 1. 2. 3. 4. 61 5E3 61 5E-3 0.61 5E3 0.615E-3 Question 15 Which of the following is/are expression(s) ?
  46. 4. print ('p, Question 8 p, q, r) This statement prints p, 25 13 16 Find the errors in following code fragment (x, print Answer There are two errors in this code fragment: 1. x is undefined in the statement y = x + 5 2. Yis undefined in the statement print (x, Y). As Python is case-sensitive hence y and Y will be treated as two different variables. (b) print Answer 5) Python doesn't allow assignment of variables while they are getting printed. (c) a b input ( "value" ) print 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 Xl) int (input 'Enter your class") ) c print ("Your class is" c) Answer The input value Xl is not int type compatible.
  47. 1. 2. 3. 4. a+b-5 -5 Question 16 The lines beginning with a certain character, and which are ignored by a compiler and not executed, are called 1. 2. 3. 4. operators operands functions comments v/ Question 17 Which of the following can be used to create comments ? 2. 3. 4. Question 18 Which of the following functions print the output to the console ? 1. Output() 2. Print() 3. Echo() 4. print() v/ Question 19 Select the reserved keyword in Python. 1. 2. 3. 4. else import print all of these ../
  48. Question 12 What will be returned by Python as result of following statements? (a) type(0) Answer (b) >>> type(int(0)) Answer Answer SyntaxError: invalid syntax (d) >>> type(101) Answer (e) type(l .0) Answer (f) type(int(l .0)) Answer (g) >>>type(float(0)) Answer (h) >>> type(float(l .0)) Answer
  49. Question 20 The input( ) returns the value as type. integer 2. string v/ 3. floating point 4. none of these Question 21 To convert the read value through input( ) into integer type ( ) is used. 2. 3. 4. floating float int integer Question 22 To convert the read value through input( ) into a floating point number ( ) is used. 2. 3. 4. floating float v/ int integer Question 23 argument is used with print( ) To print a line a text without ending it with a newline 1. 2. 3. 4. sep newline end next Question 24 The default separator character of print( ) is 2. 3. 4. tab space v/ newline dot
  50. Question 10 Consider the following code : input ("What is your name?" ) name ('Hi' print name, ("How are you doing?" ) print was intended to print output as Hi , How are you doing ? But it is printing the output as . Hi , 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' ',1, end = name, Question 11 Find the errors in following code fragment : input( "Enter your class" ) c ("Last year you were in class") c print Answer There are two errors in this code fragment: 1 1 is outside the parenthesis of print function. It should be specified as one of the arguments of print function. 2. cis a string as input function returns a string. With c 1, we are trying to subtract a integer from a string which is an invalid operation in Python. The corrected program is like this: int (input "Enter your c ("Last year you were print class" ) ) in class" c 1)
  51. (i) >>> type( 3/2) Answer Question 13 What will be the output produced by following code ? (a) >>> str(print())+"One" Output INoneOneI 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 'Nonel and addition operator joins 'Nonel and 'One' to give the final output as INoneOneI. (b) >>> str(print("hello"))+"One" Output hello INoneOneI 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 'Nonel and 'One' to give the second line of the output as INoneOneI. (c) print(print("Hola")) 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. (d) >>> print (print ("Hola", end = Output HolaNone
  52. Question 25 To give a different separator with print( ) argument is used. 1. 2. 3. 4. sep ../ separator end tab Fill in the Blanks Question 1 A keyword is a reserved word carrying special meaning and purpose. Question 2 Identifiers are the user defined names for different parts of a program. Question 3 Literals are the fixed values. Question 4 Operators are the symbols that trigger some computation or action. Question 5 An expression is a legal combination of symbols that represents a value. Question 6 Non-executable, additional lines added to a program, are known as comments. Question 7 In Python, the comments begin with # character. Question 8 Python is a case sensitive language.
  53. 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 ? >>> >>> 10 >>> >>> - 0012 a print (a) b - 0013 - 0078 c " " , File - 0078 c line 1 SyntaxError Answer invalid syntax Due to the prefix 00, 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 Predict the output a*c print (a, Output Explanation 1. 2. 3. = 2*2, 2*3, print (a, 4 assigns initial value of 2 to a, 3 to b and 4 to c. a *b, a*c prints values of a, b, c as 4, 6 and 8 respectively.
  54. Question 12 What will be returned by Python as result of following statements? (a) type(0) Answer (b) >>> type(int(0)) Answer Answer SyntaxError: invalid syntax (d) >>> type(101) Answer (e) type(l .0) Answer (f) type(int(l .0)) Answer (g) >>>type(float(0)) Answer (h) >>> type(float(l .0)) Answer
  55. Question 9 The print( ) function prints the value of a variable/expression. Question 10 The input( ) function gets the input from the user. Question 11 The input( ) function returns the read value as of string type. Question 12 To convert an input( value in integer type, int( ) function is used. Question 13 To convert an input( value in floating-point type, float( ) function is used. Question 14 Strings can be created with single quotes, double quotes and triple quotes. True/False Questions Question 1 Keywords can be used as identifier names. False Question 2 The identifiers in Python can begin with an underscore. True Question 3 0128 is a legal literal value in Python. False Question 4 Oxl 2EFG is a legal literal value in Python. False
  56. (i) >>> type( 3/2) Answer Question 13 What will be the output produced by following code ? (a) >>> str(print())+"One" Output INoneOneI 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 'Nonel and addition operator joins 'Nonel and 'One' to give the final output as INoneOneI. (b) >>> str(print("hello"))+"One" Output hello INoneOneI 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 'Nonel and 'One' to give the second line of the output as INoneOneI. (c) print(print("Hola")) 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. (d) >>> print (print ("Hola", end = Output HolaNone
  57. 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 print num print num print num print Answer num print num print num print num print 13 Id(num) num + 3 Id(num) 3 num Id(num) "Hello" Id(num) 13 Id(num) num + 3 Id(num) 3 num Id(num) "Hello" Id(num) # print 1 # print 2 # print 3 # 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 (num - float (input ("valuel: " ) ) ) valuel:67 num' is an invalid keyword argument for this function TypeError: (b) >>> print (float )
  58. Question 5 0123 is a legal literal value in Python. True Question 6 Variables once assigned a value can be given any other value. True Question 7 Variables are created when they are first assigned their value. True Question 8 Python variables support dynamic typing. True Question 9 You can rename a keyword. False Question 10 String values in Python can be single line strings, and multi-line strings. True Question 11 A variable can contain values of different types at different times. True Question 12 Expressions contain values/variables along with operators. True Type A : Short Answer Questions/Conceptual Questions Question 1 What are tokens in Python ? How many types of tokens are allowed in Python ? Examplify your answer.
  59. 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 ? >>> >>> 10 >>> >>> - 0012 a print (a) b - 0013 - 0078 c " " , File - 0078 c line 1 SyntaxError Answer invalid syntax Due to the prefix 00, 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 Predict the output a*c print (a, Output Explanation 1. 2. 3. = 2*2, 2*3, print (a, 4 assigns initial value of 2 to a, 3 to b and 4 to c. a *b, a*c prints values of a, b, c as 4, 6 and 8 respectively.
  60. valuel: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 Predict the output of the following code : int (input ("Input days days * 3600 * 24 int(input("lnput hours hours * 3600 int (input ("Input minutes: minutes * 60 int (input ("Input seconds seconds time minutes - days hours + seconds print ("Total number of seconds" , time) If the input given is in this order : 1, 2, 3, 4 Output Input Input Input Input Total days 1 hours: 2 minutes: 3 seconds: 4 number of seconds 93784 Question 19 What will the following code result into ? nl, n2 nl + n2 print(nl, n2, n3, n4) Answer The code will result into an error as in the statement n4 = n4 + 2, variable n4 is undefined.
  61. Answer The smallest individual unit in a program is known as a Token. Python has following tokens: 1. 2. 3. 4. 5. Keywords — Examples are import, for, in, while, etc. Identifiers - Examples are MyFile, _DS, etc. Literals — Examples are "abc", 5, 28.5, etc. Operators — Examples are +, -, >, or, etc. # () etc. Punctuators Question 2 How are keywords different from identifiers ? 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. 2. 3. 4. 5. String literals Numeric literals Boolean literals Special literal None 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.
  62. 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 print num print num print num print Answer num print num print num print num print 13 Id(num) num + 3 Id(num) 3 num Id(num) "Hello" Id(num) 13 Id(num) num + 3 Id(num) 3 num Id(num) "Hello" Id(num) # print 1 # print 2 # print 3 # 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 (num - float (input ("valuel: " ) ) ) valuel:67 num' is an invalid keyword argument for this function TypeError: (b) >>> print (float )
  63. input( "Enter a value") val nval val + 30 print(nval) Answer Below is the corrected program: int (input ("Enter a value")) val Question 20 Correct the following program so that it displays 33 when 30 is input. int (input ("Enter total #used int() to convert input value into integer nval val print(nval) #changed 30 to 3 Type C : Programming Practice/Knowledge based Questions 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 print ("Why is 6 afraid of 7?" ) input ("Press Enter") print ("Because 7 8(ate) 9 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 int (input( "Enter day totalDays daysLeft totalDays day part of day today's date number of days in this month
  64. 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: 1. Fractional Form - 2.0, 17.5, -13.0, -0.00625 2. Exponent form - 152E05, 1.52E07, 0.152E08, -0.172+3 Question 6 How are string-literals represented and implemented in Python ? Answer A string-literal is represented as a sequence of characters surrounded by quotes (single, double or triple quotes). String-literals in Python are implemented using Unicode. Question 7 Which of these is not a legal numeric type in Python ? (a) int (b) float (c) decimal. Answer decimal is not a legal numeric type in Python. Question 8 Which argument of print( ) would you set for: (i) changing the default separator (space) ? (ii) printing the following line in current line ? 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 (N), Logical negation
  65. print (daysLeft, Output "days are left in current month") Enter day part of today's date: 16 Enter total number of days in this month: 15 days are left in current month Question 3 Write a program that generates the following output : 5 10 9 31 Assign value 5 to a variable using assignment operator (=) Multiply it with 2 to generate 10 and subtract 1 to generate 9. Solution 5 a print (a) a a print (a) 1 a a print (a) Output 5 10 9 Question 4 Modify above program so as to print output as 5@10@9. Solution 5 a print (a, end= a print (a, end= 1 a print (a)
  66. valuel: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 Predict the output of the following code : int (input ("Input days days * 3600 * 24 int(input("lnput hours hours * 3600 int (input ("Input minutes: minutes * 60 int (input ("Input seconds seconds time minutes - days hours + seconds print ("Total number of seconds" , time) If the input given is in this order : 1, 2, 3, 4 Output Input Input Input Input Total days 1 hours: 2 minutes: 3 seconds: 4 number of seconds 93784 Question 19 What will the following code result into ? nl, n2 nl + n2 print(nl, n2, n3, n4) Answer The code will result into an error as in the statement n4 = n4 + 2, variable n4 is undefined.
  67. (not) are a few examples of unary operators. Examples of binary operators are Addition (+), Subtraction (-), Multiplication (*), Division (/). Question 10 What is an expression and a statement ? Answer An expression is any legal combination of symbols that represents a value. For example, 2.9, a + 5, A statement is a programming instruction that does something i.e. some action takes place. For example: print("Hello") a = 15 b = a-IO Question 11 What all components can a Python program contain ? Answer A Python program can contain various components like expressions, statements, comments, functions, blocks and indentation. Question 12 What do you understand by block/code block/suite in Python ? Answer A block/code block/suite is a group of statements that are part of another statement. For example: if print ("Value of 'b' is less than 5. print ("Thank you. ") Question 13 What is the role of indentation in Python ? Answer Python uses indentation to create blocks of code. Statements at same indentation level are part of same block/suite.
  68. input( "Enter a value") val nval val + 30 print(nval) Answer Below is the corrected program: int (input ("Enter a value")) val Question 20 Correct the following program so that it displays 33 when 30 is input. int (input ("Enter total #used int() to convert input value into integer nval val print(nval) #changed 30 to 3 Type C : Programming Practice/Knowledge based Questions 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 print ("Why is 6 afraid of 7?" ) input ("Press Enter") print ("Because 7 8(ate) 9 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 int (input( "Enter day totalDays daysLeft totalDays day part of day today's date number of days in this month
  69. Output 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 int a print (a, Output input ("Enter a number a a 2 4, a Enter a number: 246 8 10 Question 6 Write a Python program that accepts radius of a circle and prints its area. Solution - float (input ("Enter radius of circle: 3 14159 * a print ("Area of circle - 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 ml m2 m3 avg int(input("Enter first subject marks int(input("Enter second subject marks int(input("Enter third subject marks int(input("Enter fourth subject marks int(input("Enter fifth subject marks (ml m3+ m4 + m5) m2
  70. Question 14 What are variables ? How are they important for a program ? 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 What do you understand by undefined variable in Python ? 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: print (x) #This statement will cause an error for undefined variable x - 20 x print (x) The first line of the above code snippet will cause an undefined variable error as we are trying to use x before assigning a value to it. Question 16 What is Dynamic Typing feature of Python ? Answer A variable pointing to a value of a certain type can be made to point to a value/object of different type. This is called Dynamic Typing. For example: 10 x print (x) "Hello World" x print (x) Question 17 What would the following code do : X = Y = 7? Answer It will assign a value of 7 to the variables X and Y.
  71. print( "Average Marks Output avg) Enter first subject marks: 65 Enter second subject marks: 78 Enter third subject marks: 79 Enter fourth subject marks: 80 Enter fifth subject marks: 85 Average Marks - 77.4 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 int (input ("Enter your height in centimeters ht htlnlnch - ht / 2.54; feet htlnlnch // 12; inch % 12; htlnlnch print ("Your height is" , Output feet, "feet and" , inch, 'inches " ) Enter your height in centimeters: 162 Your height is 5.0 feet and 3.7795275590551185 inches Question 9 Write a program to read a number n and print n2, n3 and n4. Solution n n2, int (input ( " Enter n3, n4 n n) n n print( "n print( "nA2 print( "nA3 Output Enter n: -2 n nA2 = 4 2
  72. print (daysLeft, Output "days are left in current month") Enter day part of today's date: 16 Enter total number of days in this month: 15 days are left in current month Question 3 Write a program that generates the following output : 5 10 9 31 Assign value 5 to a variable using assignment operator (=) Multiply it with 2 to generate 10 and subtract 1 to generate 9. Solution 5 a print (a) a a print (a) 1 a a print (a) Output 5 10 9 Question 4 Modify above program so as to print output as 5@10@9. Solution 5 a print (a, end= a print (a, end= 1 a print (a)
  73. Question 18 What is the error in following code : X, Y = 7? 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: Question 19 Following variable definition is creating problem X = 0281, find reasons. 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 understandabil ity 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: # This program shows a program's components # Definition of function SeeYou() follows def SeeYou print ("Time to say Good Bye! ! print ( "Value print ( "Value # Main program-code 15 a b 10 a (a print if else. SeeYou 3) follows now colon means it's a block of of a' was a' was calling more than 15 initially. 15 or less initially. above defined function SeeYou()
  74. nA3 -8 nA4 - 16 Question 10 Write a program to find area of a triangle. Solution = float (input ("Enter height of the triangle h = float (input ("Enter base of the triangle b 0.5 * b h area print ("Area of triangle area) Output Enter height of the triangle: 2.5 Enter base of the triangle: 5 Area of triangle 6.25 Question 11 Write a program to compute simple interest and compound interest. Solution si ci = float (input ("Enter principal = float (input ("Enter rate int (input ("Enter tlme (p t) / 100 / 100 )) t) print ("Simple interest = si) print( "Compound interest = 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.
  75. Output 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 int a print (a, Output input ("Enter a number a a 2 4, a Enter a number: 246 8 10 Question 6 Write a Python program that accepts radius of a circle and prints its area. Solution - float (input ("Enter radius of circle: 3 14159 * a print ("Area of circle - 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 ml m2 m3 avg int(input("Enter first subject marks int(input("Enter second subject marks int(input("Enter third subject marks int(input("Enter fourth subject marks int(input("Enter fifth subject marks (ml m3+ m4 + m5) m2
  76. Solution int (input ("Enter number n print ("First five multiples of" , n, 'are") print 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 n c input ("Enter name of student int (input ("Enter class of student: int (input ("Enter age of student: print( "Name: " print print print( "Name: " print( "Class : " print ( " Age : "Class: ' c, "Age: n, n) c) a) Output Enter Enter Enter Name: Name: Class: name of student: Kavya class of student: 11 age of student: 17 Kavya Class: 11 Age: 17 Kavya 11 Age: 17 Question 14 Write a program to input a single digit(n) and print a 3 digit number created as e.g., if you input 7, then it should print 789. Assume that the input digit is in range 1-7.
  77. print( "Average Marks Output avg) Enter first subject marks: 65 Enter second subject marks: 78 Enter third subject marks: 79 Enter fourth subject marks: 80 Enter fifth subject marks: 85 Average Marks - 77.4 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 int (input ("Enter your height in centimeters ht htlnlnch - ht / 2.54; feet htlnlnch // 12; inch % 12; htlnlnch print ("Your height is" , Output feet, "feet and" , inch, 'inches " ) Enter your height in centimeters: 162 Your height is 5.0 feet and 3.7795275590551185 inches Question 9 Write a program to read a number n and print n2, n3 and n4. Solution n n2, int (input ( " Enter n3, n4 n n) n n print( "n print( "nA2 print( "nA3 Output Enter n: -2 n nA2 = 4 2
  78. Type B: Application Based Questions 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 = 0098 (d) 56thnumber = 3300 (e) length = 450.17 (f) ! Taylor = 'Instantl (g) this variable = 87.E02 (h) float = .17E-03 (i) FLOAT = 0.17E - 03 Answer 1. 2. 3. 4. 5. 6. 7. y = 037 (option b) will give an error as decimal integer literal cannot start with a 0. z = 0098 (option c) will give an error as 0098 is an octal integer literal due to the 00 prefix and 8 & 9 are invalid digits in an octal number. 56thnumber = 3300 (option d) will give an error as 56thnumber is an invalid identifier because it starts with a digit. ! Taylor = 'Instantl (option f) will give an error as ! Taylor is an invalid identifier because it contains the special character !. this variable = 87.E02 (option g) will give an error due to the space present between this and variable. Identifiers cannot contain any space. 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. FLOAT = 0.17E - 03 (option i) will give an error due to the spaces present in exponent part (E - 03). Question 2 How will Python evaluate the following expression ? (i) 20 + 30 * 40 Answer 20 + 30 * 40 20 + 1200 (ii) 20 - 30 + 40
  79. Solution d n n int (input ("Enter a digit in range 1-7 print ("3 digit Output number n) 7 Enter a digit in range 1-7: 3 digit number 789 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 b c int (input ("Enter first number int (input ("Enter second number int (input ("Enter third number print ("The three number are" c print ("Numbers after swapping are" Output Enter first number: 10 Enter second number: 15 Enter third number: 20 The three number are 10 15 20 Numbers after swapping are 25 35 20 c) c)
  80. Answer 20 -30 + 40 (iii) (20 + 30) * 40 Answer (20 + 30) * 40 2000 (iv) 15.0 / 4 + (8 + 3.0) Answer 15.0 / 4+ (8 + 3.0) 14.75 Question 3 Find out the error(s) in following code fragments: temperature _ 90 print temperature Answer The call to print function is missing parenthesis. The correct way to call print function is this: print (temperature) 30 a b=a+b (a b) print And Answer There are two errors in this code fragment: In the statement b=a+b variable b is undefined. 2. In the statement print (a And b), And should be written as and. (iii)
  81. nA3 -8 nA4 - 16 Question 10 Write a program to find area of a triangle. Solution = float (input ("Enter height of the triangle h = float (input ("Enter base of the triangle b 0.5 * b h area print ("Area of triangle area) Output Enter height of the triangle: 2.5 Enter base of the triangle: 5 Area of triangle 6.25 Question 11 Write a program to compute simple interest and compound interest. Solution si ci = float (input ("Enter principal = float (input ("Enter rate int (input ("Enter tlme (p t) / 100 / 100 )) t) print ("Simple interest = si) print( "Compound interest = 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.
  82. Solution int (input ("Enter number n print ("First five multiples of" , n, 'are") print 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 n c input ("Enter name of student int (input ("Enter class of student: int (input ("Enter age of student: print( "Name: " print print print( "Name: " print( "Class : " print ( " Age : "Class: ' c, "Age: n, n) c) a) Output Enter Enter Enter Name: Name: Class: name of student: Kavya class of student: 11 age of student: 17 Kavya Class: 11 Age: 17 Kavya 11 Age: 17 Question 14 Write a program to input a single digit(n) and print a 3 digit number created as e.g., if you input 7, then it should print 789. Assume that the input digit is in range 1-7.
  83. (a, print (a print Answer 2, b, a, b c) c) In the statement print (a c) use of semicolon will give error. In place of semicolon, we b must use comma like this print (a, b, c) 24 Answer The statement 4 = print ("X - x) Answer X is incorrect as 4 cannot be a Lvalue. It is a Rvalue. There are two errors in this code fragment: 1. Variable X is undefined 2. "X =" and X should be separated by a comma like this print( "X - x) (vi) else Answer 21 5 else is a keyword in Python so it cant be used as a variable name. Question 4 What will be the output produced by following code fragment (s) ? x x 10 -x + 10 5 print print Output 22
  84. Solution d n n int (input ("Enter a digit in range 1-7 print ("3 digit Output number n) 7 Enter a digit in range 1-7: 3 digit number 789 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 b c int (input ("Enter first number int (input ("Enter second number int (input ("Enter third number print ("The three number are" c print ("Numbers after swapping are" Output Enter first number: 10 Enter second number: 15 Enter third number: 20 The three number are 10 15 20 Numbers after swapping are 25 35 20 c) c)
  85. 15 13 22 Explanation 1. 2. 3. 4. 5. 6. X = 10 assigns an initial value of 10 to X. X = X + 10 10+10 = 20. so value of X is now 20. X = X - now 15. print (X) print the value of X which is 15. X, Y = X - 2, 22 -X, Y print (X, Y) prints the value of X which is 13 and Y which is 22. first second third print first third print Output 236 2 3 - first second (first, second, third) - first third second first second (first, second, third) 11 3 33 Explanation 1. 2. 3. 4. 5. 6. 7. (iii) side area print Output side7 7 49 first = 2 assigns an initial value of 2 to first. 3 assigns an initial value of 3 to second. second = third - — first * second third = 2* 3 = 6. So variable third is initialized with a value of 6. print (first, second, third) prints the value of first, second, third as 2, 3 and 6 respectively. first = first + second + third *first = 2+3+6=11 third = second * first •third =3*11 = 33 print (first, second, 33 respectively. int (input( ' side') side side (side, area) third) prints the value of first, second, third as 11, 3 and #side given as 7
  86. Explanation 1. 2. 3. int (input( ' side') ) This statements asks the user to enter the side. We side - enter 7 as the value of side. side = 49. area print (side, area) prints the value of side and area as 7 and 49 respectively. Question 5 What is the problem with the following code fragments ? a b s 3 print (b) print b a print(s) Answer The problem with the above code is inconsistent indentation. The statements print (a), print (b), print(s) are indented but they are not inside a suite. In Python, we cannot indent a statement unless it is inside a suite and we can indent only as much is required. name age 'Prej ith " 26 print ("Your name & age are age) name 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 s a q 3 'New" / 10 a Answer The statement a The statement q in Python. "New" converts a to string type from numeric type due to dynamic typing. a / 10 is trying to divide a string with a number which is an invalid operation
  87. Question 6 Predict the output: - 40 x y x 20, print Output (20, 81) 41 Explanation 1. 2. 3. 4. (b) print Output 50 30 x = 40 assigns an initial value of 40 to x. y = x + = 41. Soy becomes 41. - 20, y + x •x = 20, 41 + 40 •x = 20, 81 . This makes xa Tuple of 2 elements (20, x 81). print (x, y) prints the tuple x and the integer variable y as (20, 81) and 41 respectively. 20, y (x, 60 y) - 20, - 10, x Explanation 1. 2. 3. (c) print 60 assigns an initial value of 20 to x and 60 to y. 10, x + 10 y, x, y = 20, 60-10, 20 +10 First RHS value 20 is assigned to first LHS variable y. After that second RHS value 50 is assigned to second LHS variable x. Finally third RHS value 30 is assigned to third LHS variable which is again y. After this assignment, x becomes 50 and y becomes 30. print (x, y) prints the value of x and y as 50 and 30 respectively. 12, 13
  88. Output 12 6.0 24 Explanation ('a, ('p, 1. 2. 3. (d) print Output 25 None 12, 13 assigns an initial value of 12 to a and 13 to b. a* 2, a/ 2 c, b = 12*2, 12/2 c, b = 24, 6.0. So c has a value of 24 and b has a value of 6.0. print (a, b, c) prints the value of a, b, cas 12, 6.0 and 24 respectively. 12, 13 b)) print Explanation 12, 13 assigns an initial value of 12 to a and 13 to b. 2. print (print (a + b)) First print(a + b) function is called which prints 25. After that, the outer print statement prints the value returned by print(a + b) function call. As print function does not return any value so outer print function prints None. Question 7 Predict the output print print Output c c 10, c b, 10 20, c 20 30 a, p, b b, q, c, end r) 25 13 16 Explanation 30p, q, - 10, 20, 1. 2. 3. 30 assigns initial value of 10 to a, 20 to b and 30 to c. c , , =30-5, 10+3, 20-4 , , = 25, 13, 16. So p is 25, q is 13 and ris 16. print ('a, b, c 10 20 30. As we have given end = same line. c, ) This statement prints a, b, c end = so output of next print statement will start in the
  89. 4. print ('p, Question 8 p, q, r) This statement prints p, 25 13 16 Find the errors in following code fragment (x, print Answer There are two errors in this code fragment: 1. x is undefined in the statement y = x + 5 2. Yis undefined in the statement print (x, Y). As Python is case-sensitive hence y and Y will be treated as two different variables. (b) print Answer 5) Python doesn't allow assignment of variables while they are getting printed. (c) a b input ( "value" ) print 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 Xl) int (input 'Enter your class") ) c print ("Your class is" c) Answer The input value Xl is not int type compatible.
  90. Question 10 Consider the following code : input ("What is your name?" ) name ('Hi' print name, ("How are you doing?" ) print was intended to print output as Hi , How are you doing ? But it is printing the output as . Hi , 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' ',1, end = name, Question 11 Find the errors in following code fragment : input( "Enter your class" ) c ("Last year you were in class") c print Answer There are two errors in this code fragment: 1 1 is outside the parenthesis of print function. It should be specified as one of the arguments of print function. 2. cis a string as input function returns a string. With c 1, we are trying to subtract a integer from a string which is an invalid operation in Python. The corrected program is like this: int (input "Enter your c ("Last year you were print class" ) ) in class" c 1)
  91. Question 12 What will be returned by Python as result of following statements? (a) type(0) Answer (b) >>> type(int(0)) Answer Answer SyntaxError: invalid syntax (d) >>> type(101) Answer (e) type(l .0) Answer (f) type(int(l .0)) Answer (g) >>>type(float(0)) Answer (h) >>> type(float(l .0)) Answer
  92. (i) >>> type( 3/2) Answer Question 13 What will be the output produced by following code ? (a) >>> str(print())+"One" Output INoneOneI 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 'Nonel and addition operator joins 'Nonel and 'One' to give the final output as INoneOneI. (b) >>> str(print("hello"))+"One" Output hello INoneOneI 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 'Nonel and 'One' to give the second line of the output as INoneOneI. (c) print(print("Hola")) 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. (d) >>> print (print ("Hola", end = Output HolaNone
  93. 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 ? >>> >>> 10 >>> >>> - 0012 a print (a) b - 0013 - 0078 c " " , File - 0078 c line 1 SyntaxError Answer invalid syntax Due to the prefix 00, 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 Predict the output a*c print (a, Output Explanation 1. 2. 3. = 2*2, 2*3, print (a, 4 assigns initial value of 2 to a, 3 to b and 4 to c. a *b, a*c prints values of a, b, c as 4, 6 and 8 respectively.
  94. 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 print num print num print num print Answer num print num print num print num print 13 Id(num) num + 3 Id(num) 3 num Id(num) "Hello" Id(num) 13 Id(num) num + 3 Id(num) 3 num Id(num) "Hello" Id(num) # print 1 # print 2 # print 3 # 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 (num - float (input ("valuel: " ) ) ) valuel:67 num' is an invalid keyword argument for this function TypeError: (b) >>> print (float )
  95. valuel: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 Predict the output of the following code : int (input ("Input days days * 3600 * 24 int(input("lnput hours hours * 3600 int (input ("Input minutes: minutes * 60 int (input ("Input seconds seconds time minutes - days hours + seconds print ("Total number of seconds" , time) If the input given is in this order : 1, 2, 3, 4 Output Input Input Input Input Total days 1 hours: 2 minutes: 3 seconds: 4 number of seconds 93784 Question 19 What will the following code result into ? nl, n2 nl + n2 print(nl, n2, n3, n4) Answer The code will result into an error as in the statement n4 = n4 + 2, variable n4 is undefined.
  96. input( "Enter a value") val nval val + 30 print(nval) Answer Below is the corrected program: int (input ("Enter a value")) val Question 20 Correct the following program so that it displays 33 when 30 is input. int (input ("Enter total #used int() to convert input value into integer nval val print(nval) #changed 30 to 3 Type C : Programming Practice/Knowledge based Questions 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 print ("Why is 6 afraid of 7?" ) input ("Press Enter") print ("Because 7 8(ate) 9 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 int (input( "Enter day totalDays daysLeft totalDays day part of day today's date number of days in this month
  97. print (daysLeft, Output "days are left in current month") Enter day part of today's date: 16 Enter total number of days in this month: 15 days are left in current month Question 3 Write a program that generates the following output : 5 10 9 31 Assign value 5 to a variable using assignment operator (=) Multiply it with 2 to generate 10 and subtract 1 to generate 9. Solution 5 a print (a) a a print (a) 1 a a print (a) Output 5 10 9 Question 4 Modify above program so as to print output as 5@10@9. Solution 5 a print (a, end= a print (a, end= 1 a print (a)
  98. Output 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 int a print (a, Output input ("Enter a number a a 2 4, a Enter a number: 246 8 10 Question 6 Write a Python program that accepts radius of a circle and prints its area. Solution - float (input ("Enter radius of circle: 3 14159 * a print ("Area of circle - 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 ml m2 m3 avg int(input("Enter first subject marks int(input("Enter second subject marks int(input("Enter third subject marks int(input("Enter fourth subject marks int(input("Enter fifth subject marks (ml m3+ m4 + m5) m2
  99. print( "Average Marks Output avg) Enter first subject marks: 65 Enter second subject marks: 78 Enter third subject marks: 79 Enter fourth subject marks: 80 Enter fifth subject marks: 85 Average Marks - 77.4 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 int (input ("Enter your height in centimeters ht htlnlnch - ht / 2.54; feet htlnlnch // 12; inch % 12; htlnlnch print ("Your height is" , Output feet, "feet and" , inch, 'inches " ) Enter your height in centimeters: 162 Your height is 5.0 feet and 3.7795275590551185 inches Question 9 Write a program to read a number n and print n2, n3 and n4. Solution n n2, int (input ( " Enter n3, n4 n n) n n print( "n print( "nA2 print( "nA3 Output Enter n: -2 n nA2 = 4 2
  100. nA3 -8 nA4 - 16 Question 10 Write a program to find area of a triangle. Solution = float (input ("Enter height of the triangle h = float (input ("Enter base of the triangle b 0.5 * b h area print ("Area of triangle area) Output Enter height of the triangle: 2.5 Enter base of the triangle: 5 Area of triangle 6.25 Question 11 Write a program to compute simple interest and compound interest. Solution si ci = float (input ("Enter principal = float (input ("Enter rate int (input ("Enter tlme (p t) / 100 / 100 )) t) print ("Simple interest = si) print( "Compound interest = 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.
  101. Solution int (input ("Enter number n print ("First five multiples of" , n, 'are") print 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 n c input ("Enter name of student int (input ("Enter class of student: int (input ("Enter age of student: print( "Name: " print print print( "Name: " print( "Class : " print ( " Age : "Class: ' c, "Age: n, n) c) a) Output Enter Enter Enter Name: Name: Class: name of student: Kavya class of student: 11 age of student: 17 Kavya Class: 11 Age: 17 Kavya 11 Age: 17 Question 14 Write a program to input a single digit(n) and print a 3 digit number created as e.g., if you input 7, then it should print 789. Assume that the input digit is in range 1-7.
  102. Solution d n n int (input ("Enter a digit in range 1-7 print ("3 digit Output number n) 7 Enter a digit in range 1-7: 3 digit number 789 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 b c int (input ("Enter first number int (input ("Enter second number int (input ("Enter third number print ("The three number are" c print ("Numbers after swapping are" Output Enter first number: 10 Enter second number: 15 Enter third number: 20 The three number are 10 15 20 Numbers after swapping are 25 35 20 c) c)