0% found this document useful (0 votes)
4 views3 pages

PY Assignment 2

The document provides a series of Python programming concepts and examples, including set operations, if-else statements, arithmetic operators, tuples, modules, string manipulation functions, and exception handling. Each section includes explanations and code snippets to illustrate the concepts. The document serves as a basic guide for understanding fundamental Python programming techniques.

Uploaded by

jayeshrane450
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)
4 views3 pages

PY Assignment 2

The document provides a series of Python programming concepts and examples, including set operations, if-else statements, arithmetic operators, tuples, modules, string manipulation functions, and exception handling. Each section includes explanations and code snippets to illustrate the concepts. The document serves as a basic guide for understanding fundamental Python programming techniques.

Uploaded by

jayeshrane450
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/ 3

Q1: Write a Python program to perform the following operations on a set.

Ans:
-The following program demonstrates basic set operations like adding elements, removing elements, and
performing union and intersection:
-Example:-
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

set1.add(6)
print("Set after adding an element:", set1)
set1.remove(3)
print("Set after removing an element:", set1)
union_set = set1.union(set2)
print("Union of sets:", union_set)
intersection_set = set1.intersection(set2)
print("Intersection of sets:", intersection_set)

Q2: Explain If-Else statement with an example.


Ans:
-The `if-else` statement in Python is used for decision-making.
-It checks a condition and executes different blocks of code based on whether the condition is `True` or
`False`.
-Syntax:
if condition:
# Code block executed if condition is True
else:
# Code block executed if condition is False

-Example:
num = int(input("Enter a number: "))

if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")

Q3: Explain about Arithmetic Operators in Python.


Ans:
-Arithmetic operators in Python are used to perform mathematical operations.
-The main arithmetic operators are:

- `+` (Addition): Adds two numbers.


- `-` (Subtraction): Subtracts one number from another.
- `*` (Multiplication): Multiplies two numbers.
- `/` (Division): Divides one number by another and returns a float.
- `//` (Floor Division): Divides two numbers and returns the integer part of the quotient.
- `%` (Modulus): Returns the remainder of a division.
- `**` (Exponentiation): Raises a number to the power of another.
Q4: What is a tuple in Python? How to create and access it?
Ans:
-A tuple in Python is an immutable sequence used to store multiple items in a single variable.
-Unlike lists, tuples cannot be modified after creation.

Creation: A tuple is created using parentheses `()` and separating elements with commas:
eg.
my_tuple = (1, 2, 3, "Python")

Accessing Elements: Elements in a tuple can be accessed using indexing, just like lists:
eg.
print(my_tuple[0])
print(my_tuple[3])

Q5: Define Module? What are the Advantages of Using Modules?


Ans:
-A module in Python is a file that contains Python code (functions, classes, or variables) and can be imported
into other programs.
-Modules help in organizing and reusing code efficiently.
Advantages of Using Modules:
1. Code Reusability: Functions and classes defined in a module can be reused in multiple programs.
2. Modularity: Helps in organizing large codebases by breaking them into smaller, manageable files.
3. Maintainability: Since code is modular, it becomes easier to update and debug.
4. Built-in Libraries: Python provides many useful built-in modules like `math`, `os`, `random`, etc., for
different functionalities.

Q6: List and explain any four built-in string manipulation functions supported by
Python.
Ans:
-Python provides various built-in functions to manipulate strings.
-Here are four commonly used ones:
1. `uppe – Converts all characters of a string to uppercase.
Eg.,
text = "hello"
print(text.upper()) # Output: HELLO
2. `lower()` – Converts all characters of a string to lowercase.
Eg.,
text = "HELLO"
print(text.lower()) # Output: hello

3. `replace(old, new)` – Replaces occurrences of a substring with another substring.


Eg.,
text = "Python is fun"
print(text.replace("fun", "awesome")) # Output: Python is awesome

4. `strip()` – Removes leading and trailing whitespace from a string.


Eg.,
text = " Hello World "
print(text.strip()) # Output: "Hello World"
Q7: Explain the term exception handling in detail.
Ans:
-Exception handling in Python is a mechanism that allows a program to handle errors gracefully instead of
crashing.
-Exceptions occur when the program encounters an unexpected situation, such as division by zero or
accessing an undefined variable.
-Python provides the `try-except` block to handle exceptions:
-Syntax:
try:
# Code that may raise an exception
except ExceptionType:
# Code that executes if an exception occurs

-Example:
try:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
result = num1 / num2
print("Result:", result)
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
except ValueError:
print("Error: Invalid input, please enter a number.")

You might also like