Python Programming Important Questions and Answers
Q1 b) List and explain with example different comparison and Boolean operators in Python.
Comparison Operators:
- == : Equal to
- != : Not equal to
- > : Greater than
- < : Less than
- >= : Greater than or equal to
- <= : Less than or equal to
Example:
a=5
b=3
print(a == b) # False
print(a != b) # True
Boolean Operators:
- and : Returns True if both conditions are true
- or : Returns True if at least one is true
- not : Reverses the result
Example:
a=5
b=3
print(a > 2 and b < 5) # True
Q2 b) Demonstrate with example print(), input() and string replication function in Python.
print(): Prints output
Example: print("Hello")
input(): Takes input from user
Example:
name = input("Enter name: ")
print("Hello", name)
String Replication:
Example:
print("Hi " * 3) # Hi Hi Hi
Q3 b) Explain the methods of list data types in Python for the following operations.
i) Adding to list:
[Link](4), [Link](1, 10), [Link]([5, 6])
ii) Removing from list:
[Link](2), [Link](0), [Link]()
iii) Finding in list:
20 in list, [Link](30)
iv) Sorting:
[Link](), sorted(list)
v) Reversing:
[Link](), list(reversed(list))
Q4 b) How is tuple different from list? Which function is used to convert list to tuple?
List is mutable, tuple is immutable.
List: [1, 2, 3]
Tuple: (1, 2, 3)
List can be modified using append(), pop() etc.
Tuple has no such methods.
Convert list to tuple:
my_list = [10, 20]
my_tuple = tuple(my_list)