0% found this document useful (0 votes)
201 views4 pages

Quiz 1

The document contains examples of Python code that demonstrate printing output, string operations, variable assignment and data types, user input, arithmetic operations, and the end parameter in print functions. The code samples show how to format printed output, take user input, perform calculations, and check variable types.

Uploaded by

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

Quiz 1

The document contains examples of Python code that demonstrate printing output, string operations, variable assignment and data types, user input, arithmetic operations, and the end parameter in print functions. The code samples show how to format printed output, take user input, perform calculations, and check variable types.

Uploaded by

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

QUESTION:

Write program to produce the following exact output:


Hello World!
Welcome to Python!

1000
2000
3000

ANSWER:
print("Hello World!")
print('Welcome to Python!')

print()
print()

print(1000)
print(2000)
print(3000)

#####

QUESTION:
Write a program with the following variables
first_name = "John"
virus_found = False
and display the data types of these variables

ANSWER:
first_name = "John"
virus_found = False

#display the data types of variables


print(type(first_name))
print(type(virus_found))

#####

QUESTION:
Write a program with the following variables
first_name = "John"
last_name = "Smith"

a) construct a variable full_name using string addition


b) using variable full_name to display the following message
My name is John Smith.

ANSWER:
# name details
first_name = "John"
last_name = "Smith"

# use string addition to formulate the full name


full_name = first_name + " " + last_name

# display the full name


print("My name is " + full_name + ".")
#####

QUESTION:
Write a program that uses string multiplication and display the following output
frogfrogfrog
catcatcatcatcat

ANSWER:
# display some silly strings
silly1 = "frog" * 3
silly2 = 5 * "cat"
print(silly1)
print(silly2)

#####

QUESTION:
Write a program that asks the user to enter first name and last name,
then display the full name.
The program must work exactly like the following example.
Enter your first name: Frogory
Enter your last name: Green
My name is Frogory Green.

ANSWER:
# ask the user to enter first name and last name
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")

# use string addition to formulate the full name


full_name = first_name + " " + last_name

# display the full name


print("My name is " + full_name + ".")

#####

QUESTION:
The following program generates compile error.
Fix the error.

# A program to display a favorite number


# favorite number
fav_number = 7

# display favorite number


print("My favorite number is " + fav_number)

ANSWER:
# favorite number
fav_number = 7

# display favorite number


print("My favorite number is " + str(fav_number))

#####

QUESTION:
Write a program that asks the user to enter 2 integers and display the sum.
The program must work exactly like the following example.

Enter the 1st integer: 50


Enter the 2nd integer: 7
The sum is 57

ANSWER:
# Ask the user to enter 2 integers and display the sum
user_input1 = input("Enter the 1st integer: ")
number1 = int(user_input1)

user_input2 = input("Enter the 2nd integer: ")


number2 = int(user_input2)

# calculate the sum


number_sum = number1 + number2

# display the sum


print("The sum is " + str(number_sum))

#####

QUESTION:
Write a program that asks the user to enter 2 decimal numbers and displays the sum.
The program must work exactly like the following example.

Enter the 1st number: 1.5


Enter the 2nd number: 2.6
The sum of 1.5 and 2.6 is 4.1

ANSWER:
# Ask the user to enter 2 decimal numbers and display the sum
user_input = input("Enter the 1st number: ")
number1 = float(user_input)

user_input = input("Enter the 2nd number: ")


number2 = float(user_input)

# calculate the sum


number_sum = number1 + number2

# display the sum


print("The sum of "
+ str(number1)
+ " and "
+ str(number2)
+ " is "
+ str(number_sum)
)

#####

QUESTION:
Write a program that displays the following exact output
Welcome to Unimovies!
Thursday July 30 at 7.15pm: "Inside Out"

ANSWER:
print("Welcome to Unimovies!")
print('Thursday July 30 at 7.15pm: "Inside Out"')

#####

#Program 1:
print("dog")
print("cat")
print("kangaroo")

#Program 2:
print("dog", end="*")
print("cat", end="*")
print("kangaroo", end="*")

#Program 3:
print("dog", end="XYZ")
print("cat", end="XYZ")
print("kangaroo", end="XYZ")

#Program 4:
print("dog", end="")
print("cat", end="")
print("kangaroo", end="")

#Program 5:
print("dog", end=", ")
print("cat", end=", ")
print("kangaroo", end=".")
print(2000)

#Program 6:
print("dog", end=", ")
print("cat", end=", ")
print("kangaroo", end=".")
print()
print(2000)

You might also like