INTRODUCTION TO PYTHON
Q1: Write a code in Python editor which will allow you to input 5 facts about you and
print them on the screen.
CODE:
# Input 5 facts about yourself
fact1 = input("Enter fact 1 about yourself: ")
fact2 = input("Enter fact 2 about yourself: ")
fact3 = input("Enter fact 3 about yourself: ")
fact4 = input("Enter fact 4 about yourself: ")
fact5 = input("Enter fact 5 about yourself: ")
# Print the facts
print("\nHere are 5 facts about you:")
print("Fact 1:", fact1)
print("Fact 2:", fact2)
print("Fact 3:", fact3)
print("Fact 4:", fact4)
print("Fact 5:", fact5)
Q2: Write a Python script for taking 3 numbers as input and calculate and print their
sum and difference.
CODE:
# Input 3 numbers and find their sum and differences
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
# Calculate the sum and difference
sum_result = num1 + num2 + num3
difference_result = num1 - num2 - num3
# Print the results
print("The sum of the numbers is:", sum_result)
print("The difference of the numbers is:", difference_result)
Q3: Write a Python script for taking 5 numbers as input and calculate and print their
Average.
CODE:
# Input 5 numbers and find their average
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
num4 = int(input("Enter the fourth number: "))
num5 = int(input("Enter the fifth number: "))
# Calculate the average
average = (num1 + num2 + num3 + num4 + num5) / 5
# Print the average
print("The average of the 5 numbers is:", average)
Q4: Write a Python script for taking the length and width of a rectangle and calculate
and prints the Area and Perimeter of rectangle.
Area = length*width Perimeter=2*(length+width)
CODE:
# Input the length and width of the rectangle and find the perimeter
length = int(input("Enter the length of the rectangle: "))
width = int(input("Enter the width of the rectangle: "))
# Calculate the area and perimeter
area = length * width
perimeter = 2 * (length + width)
# Print the results
print("The area of the rectangle is:", area)
print("The perimeter of the rectangle is:", perimeter)
Q5: Write a Python script for finding the volume of a cube.
CODE:
# Program to calculate the side length of a cube from its volume
# Input the volume of the cube
volume = float(input("Enter the volume of the cube: "))
# Calculate the side length (cube root of the volume)
side_length = volume ** (1/3)
# Display the result
print(f"The side length of the cube is: {side_length}")
EXERCISE:
1) Write a Python script to input the names of 3 of your favorite foods and
print them.
2) Write a Python script to input 3 numbers and calculate their sum,
product, and difference.
3) Write a Python script to input 4 test scores and calculate their average.
4) Write a Python script to input the base and height of a triangle and
calculate its area
5) Write a Python script to input the radius of a sphere and calculate its
volume
VIVA QUESTIONS:
Q1: What is Python, and why is it called an interpreted language?
A: Python is a high-level, general-purpose programming language. It is called an
interpreted language because its code is executed line by line by the Python
interpreter.
Q2: What are the basic data types available in Python?
A: The basic data types in Python are: int (integer), float (floating-point numbers),
str (string), bool (Boolean), and complex (complex numbers).
Q3: What is the difference between `print()` and `input()` functions?
A: `print()` is used to display output to the user, while `input()` is used to take
input from the user.
Q4: How do you write a comment in Python?
A: Comments in Python start with a `#` symbol. For example:
`# This is a comment.`
Q:5 What is the purpose of indentation in Python?
A: Indentation is used in Python to define blocks of code, such as loops,
conditionals, and function definitions.
Q6: What are variables, and how are they declared in Python?
A: Variables are used to store data in Python. They are declared by assigning a
value to a name. For example:
`x = 10`
Q7: What will be the output of the following code?**
print("Hello" + "World")
Answer: “HelloWorld”
Q8: Identify the error in this code:
num = "5"
result = num + 10
print(result)
Answer:
The error is a TypeError because you cannot add a string (`"5"`) to an integer
(`10`).
Q9: Predict the output:
x=5
x=x+3
print(x)
Answer: `8`
Q10: Predict the output of the following:**
name = "Alice"
print("Hello, " + name + "!")
Answer: `Hello, Alice!`
Q11: Identify the error, if any
print("The result is:" 42)
Answer: There is a Syntax Error because a comma is missing between the string
and the number. The corrected code is: print("The result is:", 42)
Q12: What will be the result of this code?
a = 10
b=3
print(a // b)
Answer: `3` (integer division)