Python_Programming_QA_Complete
Python_Programming_QA_Complete
1. Discuss the different flow control statements available in Python. Describe each type with example code to show how
they work.
Answer:
Flow control statements in Python:
- Conditional Statements: if, elif, else
Example:
x = 10
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
- Looping Statements: for, while
Example:
for i in range(3):
print(i)
i=0
while i < 3:
print(i)
i += 1
- Jump Statements: break, continue, pass
Example:
for i in range(5):
if i == 3:
break
print(i)
2. Explain print(), input(), len(), str(), int(), and float() functions with examples.
Answer:
print("Hello") # Outputs: Hello
name = input("Enter name: ") # User input
print(len("Python")) # Outputs: 6
print(str(10)) # Converts to '10'
print(int("5")) # Converts to 5
print(float("3.14")) # Converts to 3.14
4. Define exception. Explain the different ways to handle the exception with example.
Answer:
An exception is an error during execution.
Example:
try:
x = 10 / 0
except ZeroDivisionError:
print("Can't divide by zero")
finally:
print("Finished")
i.
def update_global_variable():
global eggs
eggs = 'spam'
eggs = 'bacon'
print("Before func on call - eggs:", eggs)
update_global_variable()
print("After func on call - eggs:", eggs)
Output:
Before func on call - eggs: bacon
After func on call - eggs: spam
ii.
def func_on_bacon():
eggs = 'bacon'
print("Inside the func on - eggs:", eggs)
eggs = 'spam'
print("Before func on call - eggs:", eggs)
func_on_bacon()
print("After func on call - eggs:", eggs)
Output:
Before func on call - eggs: spam
Inside the func on - eggs: bacon
After func on call - eggs: spam
8. What are user defined functions? How can we pass parameters in user defined functions? Explain with suitable
example.
Answer:
User-defined functions are functions created by the user.
Example:
def greet(name):
print("Hello", name)
greet("Alice")
9. Explain the use of elif, for, while, break, and continue statements in Python with suitable code examples.
Answer:
# elif
x=3
if x > 5:
print("Big")
elif x == 3:
print("Equal")
# for
for i in range(3):
print(i)
# while
i=0
while i < 3:
print(i)
i += 1
# break
for i in range(5):
if i == 2: break
print(i)
# continue
for i in range(5):
if i == 2: continue
print(i)
10. Define Local and Global scope. Explain the different scenarios for using local and global scopes with examples.
Answer:
x = 10 # Global
def func():
x = 5 # Local
print(x)
func()
print(x)
11. Explain the methods of list data type in python with suitable code snippet for each:
i. Adding:
lst = [1, 2]
lst.append(3)
ii. Removing:
lst.remove(2)
iii. Finding:
print(3 in lst)
iv. Sorting:
lst.sort()
v. Reversing:
lst.reverse()
12. If S = ['cat', 'bat', 'rat', 'elephant', 'ant'], explain and write the output of the following:
13. What is dictionary in Python? How is it different from list data type? Explain with example.
Answer:
Dictionary: key-value pairs
List: ordered items
Example:
d = {'a': 1, 'b': 2}
for k in d:
print(k)
15. Explain keys(), values(), and items() methods of dictionaries with examples.
d = {'x': 10, 'y': 20}
print(d.keys()) # dict_keys(['x', 'y'])
print(d.values()) # dict_values([10, 20])
print(d.items()) # dict_items([('x', 10), ('y', 20)])
16. Develop a python program to create a dictionary of 10 key-value pairs and print only keys.
d = {i: chr(65+i) for i in range(10)}
print(d.keys())
17. Develop a program to count the frequency of characters using module PPrint (Pretty Printing).
from pprint import pprint
s = "apple"
freq = {}
for ch in s:
freq[ch] = freq.get(ch, 0) + 1
pprint(freq)