pwp solved qb
pwp solved qb
1. Interactive Mode
2. Script Mode
3. IDLE Mode
This is a
multi-line comment
'''
3) Write basic operations of list
my_list = [1, 2, 3, 4, 5]
1. AND (and)
2. OR (or)
3. NOT (not)
1. Equal to (==)
🚀
These operators are mostly used in conditions, loops, and decision-making in Python
programs.
8) Explain four Built-in tuple functions python with example
Tuples have several built-in functions to help manipulate and analyze data. Here are four
important ones:
Example:
my_tuple = (10, 20, 30)
print(len(my_tuple)) # Output: 3
Example:
my_tuple = (1, 2, 2, 3, 2)
print(my_tuple.count(2)) # Output: 3
Example:
my_tuple = (5, 10, 15, 20)
print(my_tuple.index(15)) # Output: 2
Example:
my_tuple = (3, 8, 1, 6)
print(max(my_tuple)) # Output: 8
Example:
python
CopyEdit
for number in range(1, 6):
if number == 3:
break # Stops the loop at 3
print(number)
Output:
CopyEdit
1
2
● Skips the current loop iteration and moves to the next one.
Example:
python
CopyEdit
for number in range(1, 6):
if number == 3:
continue # Skips 3 and continues
print(number)
Output:
CopyEdit
1
2
4
5
Key Difference
CopyEdit
my_list = [1, 2, 3]
CopyEdit
my_tuple = (1, 2, 3)
Python sets support several operations for adding, removing, and manipulating elements.
1. Adding Elements
2. Removing Elements
3. Set Operations
4. Other Operations
1. Numeric Types
● Integer (int)
● Floating Point (float)
● Complex Number (complex)
2. Sequence Types
● String (str)
● List (list)
● Tuple (tuple)
3. Set Types
● Set (set)
● Frozen Set (frozenset)
4. Mapping Type
● Dictionary (dict)
5. Boolean Type
● Boolean (bool)
6. Binary Types
● Bytes (bytes)
● Bytearray (bytearray)
● Memoryview (memoryview)
Example of List
# A list of fruits
fruits = ["Apple", "Banana", "Cherry"]
print(fruits)
# Output: ['Apple', 'Banana', 'Cherry']
DICTIONARY
A dictionary stores data in key-value pairs. Each key has a value.
Dictionaries are written inside curly braces { }, and each key is
separated from its value using a colon :.
Features of Dictionary
✔ Stores data as key-value pairs
✔ Keys must be unique, but values can be duplicates
✔ Can store different data types (keys and values)
✔ Mutable (can add, remove, and update values)
Example of Dictionary
# Creating a dictionary
student = {"name": "John", "age": 20, "grade": "A"}
print(student)
# Output: {'name': 'John', 'age': 20, 'grade': 'A'}
13) Write a Python Program to accept values from user in a list and find
the largest number and smallest number in a list.
nums = list(map(int, input("Enter numbers: ").split()))
print("Max:", max(nums))
print("Min:", min(nums))
O/P:
Enter numbers: 10 25 5 40 15
Max: 40
Min: 5
● These operators check whether a value exists in a sequence (such as a list, tuple, or string).
● in returns True if the value is present.
● not in returns True if the value is not present.
● Example: Checking if a number exists in a list or if a word exists in a sentence.
i)Create set
iii)Update set
iv)Delete set
# i) Create a Set
my_set = {1, 2, 3, 4, 5}
# ii) Access Set Elements (using loop since sets are unordered)
print("Set Elements:")
print(item)
if-else Statement
Explanation:
O/P:
if-elif-else Statement
if num > 0:
print("The number is Positive.")
elif num < 0:
print("The number is Negative.")
else:
print("The number is Zero.")
Explanation:
● If the number is greater than 0, the program prints "The number is Positive."
● If the number is less than 0, it prints "The number is Negative."
● Otherwise, it prints "The number is Zero."
Example Output:
Enter a number: -5
Python programs are made up of basic components that help in writing structured and efficient code.
The main building blocks are:
2. Operators
4. Loops
5. Functions
● Functions allow code reusability by grouping instructions that can be used multiple times.
● A function is defined once and can be called whenever needed.
● Functions make the code organized, readable, and efficient.
● get() is safer because if the key does not exist, it returns None instead of an error.
20) Write a Python program to find the factorial of a number provided by the
user
# Get input from the user
num = int(input("Enter a number: "))
# Initialize factorial variable
factorial = 1
# Calculate factorial using a loop
for i in range(1, num + 1):
factorial *= i
# Print the result
print("Factorial of", num, "is", factorial)
OP:
Enter a number: 5
Factorial of 5 is 120