Python Programming (BCC 402) -
Assignment 01 [Detailed Answers]
Q1. Give the difference between '=' and 'is' operator.
In Python, '=' and 'is' are two different types of operators with distinct purposes:
'=' Operator (Assignment Operator):
- This operator is used to assign a value to a variable.
- It does not compare values or objects.
- The right-hand side of the '=' is evaluated first, and the result is assigned to the variable on
the left-hand side.
- It is used to store data in a variable for future use.
Example:
x = 10
y = 'Hello'
Here, the integer 10 is assigned to variable x and the string 'Hello' is assigned to variable y.
'is' Operator (Identity Operator):
- The 'is' operator is used to check whether two variables point to the same object in
memory.
- It returns True if both variables refer to the same memory location (i.e., are identical
objects).
- It is mainly used for comparing reference-type objects like lists, sets, and dictionaries.
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.
Accessing List Elements:
Use indexing to access elements. Index starts from 0.
my_list[0] → 10
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'>
2. float (Floating Point):
- Represents real numbers with a decimal point.
- Examples: 3.14, -0.001, 9.81
- Useful in scientific and financial calculations.
y = 3.14
print(type(y)) # <class 'float'>
3. complex (Complex Number):
- Numbers with real and imaginary parts.
- Syntax: a + bj (where j is the imaginary unit)
- Used in advanced mathematics and engineering.
z = 2 + 3j
print(type(z)) # <class 'complex'>
Python automatically detects the type based on the value assigned.