0% found this document useful (0 votes)
1 views

Python_Programming_QA_Complete

The document provides a comprehensive overview of Python programming concepts, including flow control statements, built-in functions, exception handling, and user-defined functions. It includes code examples for various topics such as string manipulation, list methods, dictionaries, and statistical calculations. Additionally, it covers the scope of variables and demonstrates how to count character frequency using the Pretty Print module.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Python_Programming_QA_Complete

The document provides a comprehensive overview of Python programming concepts, including flow control statements, built-in functions, exception handling, and user-defined functions. It includes code examples for various topics such as string manipulation, list methods, dictionaries, and statistical calculations. Additionally, it covers the scope of variables and demonstrates how to count character frequency using the Pretty Print module.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Programming Full Question and Answer Set

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

3. Explain how to import modules with examples.


Answer:
import math
print(math.sqrt(16)) # 4.0
from math import pi
print(pi) # 3.1415...

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")

5. Develop a Python program for the following:


i. Factorial of a number
def factorial(n):
return 1 if n==0 else n * factorial(n-1)
print(factorial(5)) # Output: 120

ii. Binomial Coefficients


def binomial(n, k):
from math import factorial
return factorial(n) // (factorial(k) * factorial(n-k))
print(binomial(5, 2)) # Output: 10

6. What will be the output of the following Python code?

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

7. Explain String Concatenation and String Replication with suitable examples.


Answer:
Concatenation: "Hello" + "World" -> HelloWorld
Replication: "Hi" * 3 -> HiHiHi

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:

i. S[1:5] -> ['bat', 'rat', 'elephant', 'ant']


ii. S[:5] -> ['cat', 'bat', 'rat', 'elephant', 'ant']
iii. S[3:-1] -> ['elephant']
iv. S[:] -> ['cat', 'bat', 'rat', 'elephant', 'ant']

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)

14. Develop a program to find Mean, Variance and Standard Deviation.


import statistics as st
data = [10, 20, 30, 40]
print(st.mean(data))
print(st.variance(data))
print(st.stdev(data))

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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy