Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 9

CSET101-2023-Lab01-Monday

1. Raghav has been given the challenge to write a python program to add 5 and 7. How can
he do so?

Code: print(5+7)

2.Vigyan was asked to print the following using the print function. Help him in writing a
python program for the same. (The Golden Ratio has the same letters as The God
Relation.)

Code:print ("The Golden Ratio has the same letters as The God Relation.")

3.Indentation refers to the spaces at the beginning of a code line. Whereas in other
programming languages the indentation in code is for readability only, the indentation in
Python is very important.
When the following code is run, it gives an IndentationError which is a compile-time error.
Fix the code below and produce the intended output

i=4

j=3

if (i>j):

print(i)

Code:i = 4
j=3
if (i > j):
print(i)

4.Mohit is a very naughty child, when his brother was away from his laptop, he changed
his original program to the following:

x = input()

y = input()

if x>y:

output("x is greater than y")

else:

print("y is greater than x")

This program does not throw an error when it is run, rather it throws an error during
runtime. These kinds of errors are known as runtime errors.
If we give x=1, and y=2, the program runs fine, but when we give x=2 and y=1, the program
will throw an error.
Correct this code so that it is error-free.

Code:x = input()
y = input()
if x > y:
print("x is greater than y")
else:
print("y is greater than x")

5.Vishal was designing the front page of a website and he wrote the following code to
display a message to the user when he visits the website.

name = input("What is your name?")

print ('Hi', name, ',')

print("How are you doing?")

Vishal intended to print output as:


Hi <name>,How are you doing?
But it is printing the output as :
Hi<name> ,
How are you doing?

Correct Vishal’s code as the deadline is near and he is not able to solve the problem.

Code:

name = input("What is your name?: ")


print ('Hi', name, end =',')
print("How are you doing?")

6.A friend told Dev that he can use the Python prompt to do some basic arithmetic
operations without using the input and output functions. Explain how he will perform the
operation 50 + 40 -35 in python.

Code: print(50+40-35)

7. The id() can be used to get the memory address of a variable.

Create a variable named num1 initialized with the value 13.


Create another variable named num2 and assign to it the value of num1.
Print whether the memory addresses (IDs) of num1 and num2 are the same.
Create two more variables named num3 and num4. Initialize num3 with the value 13
again, and num4 with the value 12.
Create another two variables num5 and num6 and assign the string value "Code" to both
variables using chained assignment.
Print whether the memory addresses (IDs) of num3 and num4 are the same.
Code: num1 = 13
num2 = num1
print(id(num1)==id(num2))
num3 = 13
num4 = 12
print(id(num3)==id(num4))

num5 = num6 = "Code"


print(id(num5)==id(num6))

CSET101-2023-Lab01-Tuesday

1.Smita does not know about the input and output functions in Python. How will she
multiply 12 by 10 and display the output?

Code:print(12*10)

2.Misha wants to create a script file in Python by the name of Misha.py which prints (My
name is Misha.). Help her to do so using the print() function with double quotes.

Code:print("My name is Misha.")

3.Your teacher has edited the correct program to test your knowledge. Now when you run
the following code, it gives a compile-time error. Fix this error and output the correct
string.

Code:print ("Hello World")

4.Amaan and Ayaan were given x and y apples respectively and then they were asked to
write a python program that would input x and y and display how many apples each
person had. You must write such a program.

Code:x = input()
y = input()
print("Amaan has",x,"apples.")
print("Ayaan has",y,"apples.")

5.Kushal is new to python programming and he was experimenting with the python
multiple assignment features. He wrote a program that would take in and assign 3
variables to 3 values in only 1 line of code. But while printing the values, he made an
error. Can you find and fix the error?

Code:a,b,c = 2,8,9
print (a,b,c)
c,b,a = a,b,c
print (a,b,c)

6.Kabir bought 20 mangoes and 23 oranges from a shop. Store these values in variables a
and b in a program and using print function display their respective values.

Code:a = 20
b = 23
print(a,b)

7.Correct the following code to remove syntax errors.

Code:a,b,c = 10,20,30
p,q,r = c-5,a+3,b-4
print ('a,b,c:', a,b,c, end ='')
print('p,q,r:',p,q,r)

CSET101-2023-Lab01-Wednesday

1.A friend told Dev that he can use the Python prompt to do some basic arithmetic
operations without using the input and output functions. Explain how he will perform the
operation 50 + 40 -35 in python.

Code:print(50+40-35)

2. Karan runs a restaurant and he wants to calculate price of 1 steak and 1 ratatouille
which cost x and y respectively, input x and y from the user and display total cost
customer has to pay.

Code:x = int(input())
y = int(input())

ans = x+y
print(ans)

3.As a software engineer in a good MNC, you must make students understand the
importance of mathematics in computer science (which is often overlooked). Do so by
creating a fun program.

Write a python program to display the following string and store the file as Mathisfun.py.

Math is Fun so don't be resistant. Just learn the rules,the rules are consistent. And most
important,you must be persistent.

Code: print("Math is Fun so don't be resistant. Just learn the rules,the rules are
consistent. And most important,you must be persistent.")

4.Kshitij runs a car dealership and he wanted to display the cost of 4 cars which he had
sold in the past week so he wrote the code given in the editor.

But it was showing “x y a b” instead of the values stored in those variables.

Your task is to identify the problem with this code and fix it to print the prices of the cars
(each in a new line)

code:x = input()
y = input()
a = input()
b = input()
print(x+"\n"+y+"\n"+a+"\n"+b)

5.Fix error in following python program to print "Hello World".

Code: print ('Hello World')

6.Fix one indentation error in the below code

code:age = 21
print ("You can vote")

7.Kabir bought 20 mangoes and 23 oranges from a shop. Store these values in variables a
and b in a program and using print function display their respective values.

Code:a = 20
b = 23
print(a,b)

CSET101-2023-Lab01-Thursday

Q1. You have been a software engineer for a decade and are used to writing programs in
python2.x. So, to print the string “Hello, World”, you wrote the following program:

print "Hello World"

This gave a SyntaxError which is a compile-time error in python3.x

Fix the code given above and print the string “Hello, World”.

Code

print ("Hello, World")

Q2. When writing a command line program for an assignment, Mr. Bruce Wayne wants to
write a program that prompts the user to input values for variables a and b, takes input,
and then displays the data. Can you help him in writing such a program?

Code

a = input("Enter the value of a: ")


b = input("Enter the value of b: ")
print("Value of a:", a)
print("Value of b:", b)

Q3. Vishal was designing the front page of a website and he wrote the following code to
display a message to the user when he visits the website.
name = input("What is your name?")
print ('Hi', name, ',')
print("How are you doing?")

Vishal intended to print output as:

Hi <name>,How are you doing?

But it is printing the output as :

Hi<name> ,

How are you doing?

Correct Vishal’s code as the deadline is near and he is not able to solve the problem.

Code

name = input("What is your name?: ")


print ('Hi', name, end =',')
print("How are you doing?")

Q4. Find errors in the following code

Code

a = b = 0;
if(a == b):
c=a+b
print(c)

Q5. Find the errors in the following code

Code

a=int(input("ENTER FIRST NUMBER: "))


b=int(input("ENTER SECOND NUMBER: "))
c=int(input("ENTER THIRD NUMBER: "))
if a>b and a>c:
print("A IS GREATER")
if b>a and b>c:
print("B IS GREATER")
if c>a and c>b:
print("C IS GREATER ")

Q6. Rishank bought 30 apples and 23 oranges from a shop. Store these values in variables
a and b in a program and using print function display their respective values.

Code

a = 30
b = 23
print(a,b)

Q7. Correct the following code to remove syntax errors.

Code

a,b,c = 10,20,30
p,q,r = c-5,a+3,b-4
print ('a,b,c:', a,b,c, end ='')
print('p,q,r:',p,q,r)

CSET101-2023-Lab01-Friday

Q1 . Nimish wrote the following code which gives a compile-time error. His teacher gave
him a hint that this error was caused because of some indentation problem. Your task is
to find that problem and fix it to produce the correct result.

Code

i=4
j=3
if (i>j):
print(i)

Q2 . Gautam wants to write a program in Python which asks for the values of d and e from
the user and then displays their values. Write a program to get input values for variables d
and e from the user and display the data.

Code

d=input("Enter the value of d: ")


e=input("Enter the value of e: ")
print("Value of d: " +d)
print("Value of e: " +e)

Q3. Vedant writes a program to ask the number of boys and girls in the class from the
user and then display their values. What is the code Vedant would have written using the
input and print functions.

Code

a = input("Enter the number of boys: ")


b = input("Enter the number of girls: ")
print("The number of boys: " +a)
print("The number of girls: " +b)

Q4. Rewrite the following code after removing all syntax errors:

Code

a = int(input("Enter any number"))


if a%2 == 0:
print("Even number")
else:
print("Odd number")

Q5. Correct the following code to remove syntax errors.

Code

a,b,c = 10,20,30
p,q,r = c-5,a+3,b-4
print ('a,b,c:', a,b,c, end ='')
print('p,q,r:',p,q,r)

Q 6 Rewrite the following code after removing all syntax errors :


Code

a = int(input("Enter any number"))


b = int(input("Enter any number"))
if a>=b:
print("First number is greater")
else:
print("Second number is greater")

Q7. Rivansh runs a restaurant and he wants to calculate price of 1 apple pie and 1
brownie which cost x and y respectively, take input x and y from the user and display
total cost customer has to paid.
Code

x = int(input())
y = int(input())
ans = x + y
print(ans)

You might also like