PRIP 1.1 Sumita Arora Solutions | Class 12 Computer Science

Below are PRIP 1.1 Sumita Arora Solutions for class 12 Computer Science. To view chapter 1 conceptual videos, visit here.

To view Sumita Arora solutions for all chapters, visit here.

Q1: For each of the following write single python statement:

a) Tell the user to enter your name, then read in, and assign to a variable, a string typed in by the user

Answer:

name = input("Enter your name : ")

b) Tell the user to enter your age, then read in, and assign to a variable, a integer typed in by the user

Answer:

age = input("Enter your age : ")

c) Print out, on separate lines, Your name is name and Your age is age, where name and age are the values of the two variables entered above

Answer:

print("your name is {}".format(name))
print("your age is {}".format(age))

output:

your name is Computer tutor
your age is 20
Q2: Given examples of legal variable names and two examples of illegal variable names in python. For the illegal variable names explain why they are illegal. Do not use one-letter variable name
#Legal Variables : 
my_name = "Computer Tutor"
myname = "Computer Tutor"
_my_name = "Computer Tutor"
_myname = "Computer Tutor"
myName = "Computer Tutor"
MyName = "Computer Tutor"
MYName = "Computer Tutor"
myname1 = "Computer Tutor"

#Illegal variable :
2myname = "Computer Tutor"
my-name = "Computer Tutor"
my name = "Computer Tutor"
.myName = "Computer Tutor"

Note:

In python variable name should be of alpha-numeric characters i.e.(A-Z , a-z & 0-9) and underscore ‘_’

Also variable name can not start with number i.e. 0-9   

e.g. 2name is not allowed but,

    name2 or na2me are allowed

Printing any variable with invalid syntax will raise error : “SyntaxError: invalid syntax”

Q.3: In each of the following parts there is an error in the python code. Identify the error by name and describe the problem.
(Each piece of code is prefixed with a brief description of the programmer’s intention. )

a) The following code attempts to compute the product of m and x added to b and assign that value to y. y = mx + b

Answer:

m, x, b = 3, 2, 1
y = mx + b
print(y)

Error after execution of above code is

NameError: name 'mx' is not defined

In python multiplication of two variables is calculated using * operator

in equation if we want to compute multiplication of m and x we should write m*x

so the correct statement would be

y = m * x + b 

b) The following code attempts to compute the first-order effects of some physical process.
You may assume that equation is correct.
cofactor = alpha*x*x
1storder = 1.0/cofactor
2ndorder = 2.0**1storder

Answer:

x = 5
cofactor = alpha*x*x
1storder = 1.0/cofactor
2ndorder = 2.0**1storder
Error raised by above code is:
    1storder = 1.0/cofactor
           ^
SyntaxError: invalid syntax

reason:

In python variable name should be of alpha-numeric characters i.e.(A-Z , a-z & 0-9) and underscore ‘_’

Also variable name can not start with number

i.e. 0-9  

e.g. 2name is not allowed but,

name2 or na2me are allowed

Q.4: While taking input from input(),you need to convert the input into float and int types, if you are reading numbers

a) What built-in function do you use to convert a string into a floating-point number ? Demonstrate this function by converting string ’85’ into floating-point number and assigning it to a variable x.

Answer:

In python to convert string to floating-point number float() function is used

float function takes input of string/integer datatype and outputs floating-point value

code:
str = '85'
value = float(str)
print("Type of value variable : {}".format(type(value)))

output:
Type of value variable : <class 'float'>

b) What built-in function do you use to convert a string into an integer ?

Answer:

In python to convert string to integer number int() function is used

int function takes input of string/floating-point datatype and outputs integer value

code:
str = '85'
value = int(str)
print("Type of value variable : {}".format(type(value)))

output:
Type of value variable : <class 'int'>

c) What happens when you try to convert a string into an integer though the string is not a number?

Answer:

code:
str = 'ab'
value = int(str)
print(value)
print("Type of value variable : {}".format(type(value)))

output:
      1 str = 'ab'
----> 2 value = int(str)
      3 print(value)
      4 print("Type of value variable : {}".format(type(value)))
ValueError: invalid literal for int() with base 10: 'ab'

when integer conversion of string though it is not a number

value error exception is raised

Reason with base 10 we have numbers in range 0-9

Q.5: What would be the output of following code?
    print(‘doesn\’t’)
    print(“doesn\’t”)
    print(‘”Yes,”he said.’)
    print(“\”Yes,\”he said.”)

Answer:

code:
print('doesn\'t')
print("doesn\'t")
print('"Yes,"he said.')
print("\"Yes,\"he said.")

output:

doesn't
doesn't
"Yes,"he said.
"Yes,"he said.
Q.6: Write a program to print the acrostic poem as shown below:
In the output, you need not to print bigger letters for P E A C E, but make sure these letters make a verticle word in output.
(Acrostic poems are literary composition in which certain letters in each line form a word vertically. This acrostic poem for Peace was made using Bible verse, John 16:33)
     These things I have sPoken unto you,
                   that in me yE might have peace.
            The world ye shAll have tribulation:
           but be good for Cheer;
             I have overcomE the world.

Answer:

print("These things I have sPoken unto you,")
print("         that in me yE might have peace.")
print("      The world ye shAll have tribulation:")
print("     but be good for Cheer;")
print("       I have overcomE the world.")
Q.7: Write A program to print ASCII art using print statement
a)
Code:
print("    ______________                                ________________")
print("   /              \                              / /            \ \ ")
print("  / /        \ \   \                            |    -    -        \ ")
print("  |                |                            | /          -  \  | ")
print("  /                /                            \                  \ ")
print("  |  __ \  \|| /  /                              \_____________  \  \ ")
print("  |  /           |                               |             \    | ")
print("  |  |     __    |                               |              \   \ ")
print(" /   |       \   |                               |               \  | ")
print(" |   |        \  |                               |  ====          | | ")
print(" |   |       __  |                               |  (o-)      _   | | ")
print(" |  __\     (_o) |                              /             \  |  | ")
print(" | |             |        Heh Heh Heh          /             ) ) |  | ")
print(" \ ||             \       /      Huh Huh Huh  /              ) / |  | ")
print(" | |__             \     /                \   |___             - |  | ")
print(" | |           (*___\   /                  \     *'              |  | ")
print(" | |     _      |      /                    \   |____            |  | ")
print(" | |   //_______|                                ####\           |  | ")
print(" | /     |_|_|_|___/\                            ------          |_/  ")
print(" \|     \ -         |                            |                |   ")
print("  |     _----_______/                            \_____           |   ")
print("  |    /                                              \           |   ")
print("  |   /                                                \          |   ")
                    
 
output:
    ______________                                ________________
   /              \                              / /            \ \
  / /        \ \   \                            |    -    -        \
  |                |                            | /          -  \  |
  /                /                            \                  \
  |  __ \  \|| /  /                              \_____________  \  \
  |  /           |                               |             \    |
  |  |     __    |                               |              \   \
 /   |       \   |                               |               \  |
 |   |        \  |                               |  ====          | |
 |   |       __  |                               |  (o-)      _   | |
 |  __\     (_o) |                              /             \  |  |
 | |             |        Heh Heh Heh          /             ) ) |  |
 \ ||             \       /      Huh Huh Huh  /              ) / |  |
 | |__             \     /                \   |___             - |  |
 | |           (*___\   /                  \     *'              |  |
 | |     _      |      /                    \   |____            |  |
 | |   //_______|                                ####\           |  |
 | /     |_|_|_|___/\                            ------          |_/ 
 \|     \ -         |                            |                |  
  |     _----_______/                            \_____           |  
  |    /                                              \           |  
  |   /                                                \          |  
b)
code:
print("            .- - - - -.  ")
print("           ( #-... ' ` \  ")
print("            \ #        | ")
print("            _)\" ==== \"|_ ")
print("           (_`\"======\"`_) ")
print("            /`\"\"\"\"\"\"\"\"` \ ")
print("           |        o  _o\ ")
print("           |        ( _> |  ")
print("           \       ' .___/--# ")
print("            '.     ;-._:'\ ")
print("              )===\   <>_/     __ ")
print("       .---\"\" `====` - ` \__. ' `| ")
print("      /                ()\      / ")
print("      \___..--'           \_.-' ")
print("         |              ()| ")
print("         ;                ; ")
print("         \             ()/ ")
print("          \         '.  / ")
print("       _.' `\         '; ")
print("      (      `\        \_ ")
print("       \    .- `\        `\ ")
print("        \____)   `._____ .' ")
output:
            .- - - - -. 
           ( #-... ' ` \ 
            \ #        |
            _)" ==== "|_
           (_`"======"`_)
            /`""""""""` \
           |        o  _o\
           |        ( _> | 
           \       ' .___/--#
            '.     ;-._:'\
              )===\   <>_/     __
       .---"" `====` - ` \__. ' `|
      /                ()\      /
      \___..--'           \_.-'
         |              ()|
         ;                ;
         \             ()/
          \         '.  /
       _.' `\         ';
      (      `\        \_
       \    .- `\        `\
        \____)   `._____ .'

Clear Doubts with Computer Tutor
In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You cannot copy content of this page