Python Question & Answer Practice
Python Practice – Questions & Answers
Beginner Level
Q1. What is Python?
Python is a high-level, interpreted programming language.
Q2. Python file extension?
.py
Q3. Print text.
print("Hello World")
Q4. Declare a variable.
x = 10
Q5. Data types in Python.
int, float, str, list, tuple, dict, set
Q6. Take user input.
name = input("Enter name: ")
Q7. Type conversion.
int("10")
Q8. Single-line comment.
# comment
Q9. Multi-line comment.
'''
comment
'''
Q10. Create a list.
nums = [1,2,3]
Q11. Access list item.
nums[0]
Q12. Create a tuple.
t = (1,2)
Q13. Create dictionary.
d = {"a":1}
Q14. If condition.
if x > 5:
print("Yes")
Q15. For loop.
for i in range(3):
print(i)
Q16. While loop.
while x > 0:
x -= 1
Q17. Function.
def add(a,b):
return a+b
Q18. Default argument.
def greet(name="User"):
print(name)
Q19. Length of string.
len("Python")
Q20. String slicing.
"Python"[0:3]
Q21. List append.
nums.append(4)
Q22. Remove list item.
nums.remove(2)
Q23. Check data type.
type(x)
Q24. Boolean values.
True, False
Q25. Python is case sensitive?
Yes
Intermediate Level
Q26. List comprehension.
[x*x for x in range(5)]
Q27. Lambda function.
lambda a,b: a+b
Q28. Map function.
map(lambda x:x*2, nums)
Q29. Filter function.
filter(lambda x:x>2, nums)
Q30. Reduce function.
from functools import reduce
reduce(lambda a,b:a+b, nums)
Q31. Try-except.
try:
x=10/0
except:
print("Error")
Q32. Finally block.
Always executes.
Q33. File write.
f=open("a.txt","w")
f.write("Hello")
f.close()
Q34. File read.
f=open("a.txt")
print(f.read())
Q35. with statement.
with open("a.txt") as f:
print(f.read())
Q36. Import module.
import math
Q37. Random number.
import random
random.randint(1,10)
Q38. Date & time.
import datetime
datetime.datetime.now()
Q39. Dictionary keys.
d.keys()
Q40. Dictionary values.
d.values()
Q41. Set.
{1,2,3}
Q42. Remove duplicates.
list(set(nums))
Q43. String split.
"a,b".split(",")
Q44. Join string.
"-".join(["a","b"])
Q45. Check key exists.
"a" in d
Q46. Pass statement.
Placeholder.
Q47. Break statement.
Stops loop.
Q48. Continue statement.
Skips iteration.
Q49. Global keyword.
Access global variable.
Q50. pip.
Python package manager.
Q51. Virtual environment.
Isolated Python setup.
Q52. Requirements file.
requirements.txt
Q53. JSON conversion.
import json
json.dumps(data)
Q54. Read JSON.
json.loads(text)
Q55. Exception types.
ValueError, TypeError
Q56. Assert statement.
assert x > 0
Q57. Docstring.
Documentation string.
Q58. enumerate().
enumerate(nums)
Q59. zip().
zip(a,b)
Q60. Sorted list.
sorted(nums)
Advanced & Interview Level
Q61. OOP in Python.
Object-oriented programming.
Q62. Create class.
class User:
pass
Q63. Constructor.
def __init__(self):
pass
Q64. Inheritance.
class B(A):
pass
Q65. Polymorphism.
Same method, different behavior.
Q66. Encapsulation.
Data hiding.
Q67. Abstraction.
Hiding implementation.
Q68. __str__ method.
def __str__(self):
return "User"
Q69. Generator.
def gen():
yield 1
Q70. Iterator.
__iter__ and __next__
Q71. Decorator.
Function wrapper.
Q72. Multithreading.
Concurrent execution.
Q73. Multiprocessing.
Parallel execution.
Q74. GIL.
Global Interpreter Lock.
Q75. Raise exception.
raise ValueError()
Q76. Custom exception.
class MyError(Exception):
pass
Q77. Shallow copy.
Copies reference.
Q78. Deep copy.
Copies object.
Q79. Memory management.
Automatic garbage collection.
Q80. Garbage collector.
Frees unused memory.
Q81. API request.
import requests
requests.get(url)
Q82. Flask.
Python web framework.
Q83. Django.
Full-stack framework.
Q84. Unit testing.
Testing code.
Q85. pytest.
Testing framework.
Q86. Logging.
Track program execution.
Q87. Thread-safe.
Safe for multi-thread.
Q88. Mutable types.
list, dict
Q89. Immutable types.
int, tuple, str
Q90. Name mangling.
__var
Q91. Python interpreter.
Executes Python code.
Q92. PEP8.
Python coding standard.
Q93. Virtualenv.
Isolated environment.
Q94. List vs Tuple.
Mutable vs Immutable.
Q95. Exception vs Error.
Recoverable vs fatal.
Q96. pass vs continue.
Placeholder vs skip loop.
Q97. Python use cases.
Web, AI, Data Science.
Q98. Python speed.
Slower than C.
Q99. Python advantages.
Easy, powerful, versatile.
Q100. Python best practices.
Readable, modular, PEP8.
Beginner Level
Q1. What is Python?
Python is a high-level, interpreted programming language.
Q2. Python file extension?
.py
Q3. Print text.
print("Hello World")Q4. Declare a variable.
x = 10
Q5. Data types in Python.
int, float, str, list, tuple, dict, set
Q6. Take user input.
name = input("Enter name: ")Q7. Type conversion.
int("10")Q8. Single-line comment.
# comment
Q9. Multi-line comment.
''' comment '''
Q10. Create a list.
nums = [1,2,3]
Q11. Access list item.
nums[0]
Q12. Create a tuple.
t = (1,2)
Q13. Create dictionary.
d = {"a":1}Q14. If condition.
if x > 5:
print("Yes")
Q15. For loop.
for i in range(3):
print(i)
Q16. While loop.
while x > 0:
x -= 1
Q17. Function.
def add(a,b):
return a+b
Q18. Default argument.
def greet(name="User"):
print(name)
Q19. Length of string.
len("Python")Q20. String slicing.
"Python"[0:3]
Q21. List append.
nums.append(4)
Q22. Remove list item.
nums.remove(2)
Q23. Check data type.
type(x)
Q24. Boolean values.
True, False
Q25. Python is case sensitive?
Yes
Intermediate Level
Q26. List comprehension.
[x*x for x in range(5)]
Q27. Lambda function.
lambda a,b: a+b
Q28. Map function.
map(lambda x:x*2, nums)
Q29. Filter function.
filter(lambda x:x>2, nums)
Q30. Reduce function.
from functools import reduce reduce(lambda a,b:a+b, nums)
Q31. Try-except.
try:
x=10/0
except:
print("Error")
Q32. Finally block.
Always executes.
Q33. File write.
f=open("a.txt","w")
f.write("Hello")
f.close()
Q34. File read.
f=open("a.txt")
print(f.read())
Q35. with statement.
with open("a.txt") as f:
print(f.read())
Q36. Import module.
import math
Q37. Random number.
import random random.randint(1,10)
Q38. Date & time.
import datetime datetime.datetime.now()
Q39. Dictionary keys.
d.keys()
Q40. Dictionary values.
d.values()
Q41. Set.
{1,2,3}Q42. Remove duplicates.
list(set(nums))
Q43. String split.
"a,b".split(",")Q44. Join string.
"-".join(["a","b"])
Q45. Check key exists.
"a" in d
Q46. Pass statement.
Placeholder.
Q47. Break statement.
Stops loop.
Q48. Continue statement.
Skips iteration.
Q49. Global keyword.
Access global variable.
Q50. pip.
Python package manager.
Q51. Virtual environment.
Isolated Python setup.
Q52. Requirements file.
requirements.txt
Q53. JSON conversion.
import json json.dumps(data)
Q54. Read JSON.
json.loads(text)
Q55. Exception types.
ValueError, TypeError
Q56. Assert statement.
assert x > 0
Q57. Docstring.
Documentation string.
Q58. enumerate().
enumerate(nums)
Q59. zip().
zip(a,b)
Q60. Sorted list.
sorted(nums)
Advanced & Interview Level
Q61. OOP in Python.
Object-oriented programming.
Q62. Create class.
class User:
pass
Q63. Constructor.
def __init__(self):
pass
Q64. Inheritance.
class B(A):
pass
Q65. Polymorphism.
Same method, different behavior.
Q66. Encapsulation.
Data hiding.
Q67. Abstraction.
Hiding implementation.
Q68. __str__ method.
def __str__(self):
return "User"
Q69. Generator.
def gen():
yield 1
Q70. Iterator.
__iter__ and __next__
Q71. Decorator.
Function wrapper.
Q72. Multithreading.
Concurrent execution.
Q73. Multiprocessing.
Parallel execution.
Q74. GIL.
Global Interpreter Lock.
Q75. Raise exception.
raise ValueError()
Q76. Custom exception.
class MyError(Exception):
pass
Q77. Shallow copy.
Copies reference.
Q78. Deep copy.
Copies object.
Q79. Memory management.
Automatic garbage collection.
Q80. Garbage collector.
Frees unused memory.
Q81. API request.
import requests requests.get(url)
Q82. Flask.
Python web framework.
Q83. Django.
Full-stack framework.
Q84. Unit testing.
Testing code.
Q85. pytest.
Testing framework.
Q86. Logging.
Track program execution.
Q87. Thread-safe.
Safe for multi-thread.
Q88. Mutable types.
list, dict
Q89. Immutable types.
int, tuple, str
Q90. Name mangling.
__var
Q91. Python interpreter.
Executes Python code.
Q92. PEP8.
Python coding standard.
Q93. Virtualenv.
Isolated environment.
Q94. List vs Tuple.
Mutable vs Immutable.
Q95. Exception vs Error.
Recoverable vs fatal.
Q96. pass vs continue.
Placeholder vs skip loop.
Q97. Python use cases.
Web, AI, Data Science.
Q98. Python speed.
Slower than C.
Q99. Python advantages.
Easy, powerful, versatile.
Q100. Python best practices.
Readable, modular, PEP8.
No comments:
Post a Comment