Answer The 10 Mark Questions
Answer The 10 Mark Questions
Use Cases:
● Data validation: Ensuring input is in the correct format.
● Mathematical operations: Performing calculations with mixed data types.
● String formatting: Combining strings with numbers or other data.
27. Write a note on data types in Python.
Data types are fundamental to any programming language, defining the kind of values that can
be stored and manipulated. Python offers a rich set of built-in data types:
Numeric Types:
● int: Represents integers (whole numbers), e.g., 10, -5, 0.
● float: Represents floating-point numbers (numbers with decimals), e.g., 3.14, -2.5.
● complex: Represents complex numbers with a real and imaginary part, e.g., 2 + 3j.
String Type:
● str: Represents sequences of characters, enclosed in single quotes (' ') or double quotes
(" "), e.g., "hello", 'world'. Strings are immutable (cannot be changed after creation).
Boolean Type:
● bool: Represents truth values, either True or False. Booleans are often used in
conditional statements and logical operations.
Sequence Types:
● list: Ordered, mutable (changeable) sequences of items. Lists are defined using square
brackets [ ], e.g., [1, 2, 'a'].
● tuple: Ordered, immutable sequences of items. Tuples are defined using parentheses ( ),
e.g., (1, 2, 'a').
● range: Generates a sequence of numbers within a specified range, often used in loops.
Mapping Type:
● dict: Represents key-value pairs, where each key is unique and associated with a value.
Dictionaries are defined using curly braces { }, e.g., {"name": "Alice", "age": 30}.
Set Type:
● set: Represents an unordered collection of unique elements. Sets are defined using curly
braces { }, e.g., {1, 2, 3}. Sets do not allow duplicate values.
28. Write a program to read two integers and perform arithmetic operations on them (add,
sub, mul, div).
# Get input from the user (as strings)
num1_str = input("Enter the first integer: ")
num2_str = input("Enter the second integer: ")
# Convert strings to integers
num1 = int(num1_str)
num2 = int(num2_str)
# Perform arithmetic operations
sum_result = num1 + num2
diff_result = num1 - num2
prod_result = num1 * num2
# Handle potential division by zero error
if num2 == 0:
div_result = "Division by zero not allowed"
else:
div_result = num1 / num2
# Print the results
print("Sum:", sum_result)
print("Difference:", diff_result)
print("Product:", prod_result)
print("Quotient:", div_result)
29. Write a program to read the marks of three subjects and find their average.
# Get input for marks (as strings)
mark1_str = input("Enter marks for subject 1: ")
mark2_str = input("Enter marks for subject 2: ")
mark3_str = input("Enter marks for subject 3: ")
# Convert strings to floats (to allow for decimal marks)
mark1 = float(mark1_str)
mark2 = float(mark2_str)
mark3 = float(mark3_str)
# Calculate the average
total_marks = mark1 + mark2 + mark3
average_marks = total_marks / 3
# Print the average
print("Average marks:", average_marks)
30. Write a program to find the number of years, weeks, and days from a given number of
days.
# Get input for total days
total_days_str = input("Enter the total number of days: ")
total_days = int(total_days_str)
# Calculate years, weeks, and remaining days
years = total_days // 365 # Integer division to get the number of
years
remaining_days_after_years = total_days % 365
weeks = remaining_days_after_years // 7 # Integer division for weeks
days = remaining_days_after_years % 7 # Remaining days
# Print the results
print("Years:", years)
print("Weeks:", weeks)
print("Days:", days)