[go: up one dir, main page]

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

Answer The 10 Mark Questions

The document discusses various Python programming concepts including type conversion functions, data types, arithmetic operations, and input/output statements. It explains type conversion with examples of int(), float(), str(), and bool(), and outlines Python's built-in data types such as int, float, str, and dict. Additionally, it provides sample programs for arithmetic operations, calculating averages, determining leap years, and demonstrates the use of different operators.

Uploaded by

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

Answer The 10 Mark Questions

The document discusses various Python programming concepts including type conversion functions, data types, arithmetic operations, and input/output statements. It explains type conversion with examples of int(), float(), str(), and bool(), and outlines Python's built-in data types such as int, float, str, and dict. Additionally, it provides sample programs for arithmetic operations, calculating averages, determining leap years, and demonstrates the use of different operators.

Uploaded by

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

Let's delve into these more extensive Python questions, aiming for comprehensive answers

suitable for 10 marks each.


26. Explain Type conversion functions in Python with examples.
Type conversion, also known as type casting, is the process of changing a value from one data
type to another. Python provides several built-in functions for this purpose:
●​ int(): Converts a value to an integer.​
x = "123"​
y = int(x) # y becomes 123 (integer)​

z = 3.14​
w = int(z) # w becomes 3 (integer part)​

●​ float(): Converts a value to a floating-point number.​


a = "3.14"​
b = float(a) # b becomes 3.14 (float)​

c = 10​
d = float(c) # d becomes 10.0 (float)​

●​ str(): Converts a value to a string.​


num = 123​
text = str(num) # text becomes "123" (string)​

pi = 3.14159​
pi_str = str(pi) # pi_str becomes "3.14159" (string)​

●​ bool(): Converts a value to a boolean.​


val = 10​
bool_val = bool(val) # bool_val becomes True (non-zero number)​

empty_str = ""​
empty_bool = bool(empty_str) # empty_bool becomes False (empty
string)​

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)​

31. Write a program to check if a given year is a leap year or not.


# Get input for the year​
year_str = input("Enter the year: ")​
year = int(year_str)​

# Leap year conditions​
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):​
print(year, "is a leap year")​
else:​
print(year, "is not a leap year")​

32. Explain about input and output statements in Python.


Input:
●​ input() function: Used to get input from the user. It takes an optional prompt message as
an argument and returns the user's input as a string.Example:​
name = input("Enter your name: ")​
print("Hello, " + name)​
Output:
●​ print() function: Used to display output to the console. It can take multiple arguments,
separated by commas.Example:​
age = 30​
print("Your age is", age)​
print("Name:", "Alice", "Age:", age) # Multiple arguments​

●​ String formatting: Allows you to create formatted strings using placeholders.Example:​


name = "Bob"​
age = 25​
print("Name: %s, Age: %d" % (name, age)) # Using % formatting​
print("Name: {}, Age: {}".format(name, age)) # Using str.format()​
print(f"Name: {name}, Age: {age}") # Using f-strings (Python
3.6+)​

33. Explain various types of operators with examples.


Python offers several types of operators:
Arithmetic Operators:
●​ + (Addition)
●​ - (Subtraction)
●​ * (Multiplication)
●​ / (Division)
●​ // (Floor Division)
●​ % (Modulus)
●​ ** (Exponentiation)
Comparison Operators:
●​ == (Equal to)
●​ != (Not equal to)
●​ > (Greater than)
●​ < (Less than)
●​ >= (Greater than or equal to)
●​ <= (Less than or equal to)
Logical Operators:
●​ and
●​ or
●​ not
Assignment Operators:
●​ = (Assignment)
●​ += (Add and assign)
●​ -= (Subtract and assign)
●​ *= (

You might also like