Python Programming Assignment Detailed
Python Programming Assignment Detailed
Example:
x = 10
y = 'Hello'
Here, the integer 10 is assigned to variable x and the string 'Hello' is assigned to variable y.
Example:
a = [1, 2, 3]
b=a
c = [1, 2, 3]
print(a is b) # True (both refer to the same object)
print(a is c) # False (different objects with same content)
Conclusion:
- Use '=' to assign values.
- Use 'is' to check if two variables refer to the same object (not just equal values).
Q2. Explain how to define a list in Python. Write a Python program to
remove duplicates from a list and print the resulting value.
A list in Python is a built-in data type that is used to store multiple items in a single variable.
Lists are ordered, mutable (changeable), and allow duplicate values.
Defining a List:
Lists are defined using square brackets []. Items are separated by commas.
Example:
my_list = [10, 20, 30, 40]
You can store any data type: numbers, strings, or even other lists.
Removing Duplicates:
We can remove duplicates by converting the list to a set (since sets only keep unique
elements), and then converting it back to a list.
Python Program:
original_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(set(original_list))
print("List after removing duplicates:", unique_list)
Explanation:
- set(original_list): Converts list to a set (removes duplicates)
- list(...): Converts set back to list
- Note: The order may change, as sets are unordered
Q3. Explain the concept of a set in Python and its characteristics. How
elements are added and removed in a set?
A set is another built-in data structure in Python. It is used to store multiple unique values
in a single variable.
Characteristics of a Set:
1. Unordered: The items in a set do not maintain a specific order.
2. No Duplicate Items: Sets automatically remove duplicate values.
3. Mutable: You can add or remove elements from a set.
4. Immutable Elements: Elements must be of immutable types like int, float, string, or tuple.
Creating a Set:
my_set = {1, 2, 3}
or
my_set = set([1, 2, 3, 2]) # Duplicate 2 will be removed
Adding Elements:
my_set.add(4) # Adds 4 to the set
Removing Elements:
my_set.remove(2) # Removes 2; raises error if not found
my_set.discard(5) # Removes 5 if present; does nothing if not found
my_set.pop() # Removes a random item
my_set.clear() # Empties the entire set
Example Program:
fruits = {'apple', 'banana'}
fruits.add('orange')
fruits.remove('banana')
print(fruits)
Output:
{'orange', 'apple'}
Q4. Write a Python program to find the sum of all the items in a
dictionary.
A dictionary in Python stores data in key-value pairs. We can find the sum of all the values
using the values() method with the built-in sum() function.
Python Program:
my_dict = {'a': 100, 'b': 200, 'c': 300}
total = sum(my_dict.values())
print("Sum of all items:", total)
Explanation:
- my_dict.values() returns: [100, 200, 300]
- sum() adds up the values and returns: 600
Note:
- This method only works when dictionary values are numeric.
Q5. Explain numerical data types with the help of examples.
Python supports different numerical data types to handle numeric values. These are
essential for mathematical operations.
1. int (Integer):
- Represents whole numbers without a fractional part.
- Examples: 0, 5, -10, 999999
- Operations: Addition, Subtraction, etc.
x = 10
print(type(x)) # <class 'int'>