0% found this document useful (0 votes)
13 views6 pages

I Year Python

The document is a question bank for Python programming, categorized into three sections with varying scores. It includes multiple-choice questions, coding exercises, and theoretical questions covering topics such as data types, functions, loops, and file I/O. Each question is followed by its corresponding answer, providing a comprehensive overview of fundamental Python concepts.

Uploaded by

businessd610
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)
13 views6 pages

I Year Python

The document is a question bank for Python programming, categorized into three sections with varying scores. It includes multiple-choice questions, coding exercises, and theoretical questions covering topics such as data types, functions, loops, and file I/O. Each question is followed by its corresponding answer, providing a comprehensive overview of fundamental Python concepts.

Uploaded by

businessd610
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/ 6

JSD I YEAR QUESTION BANK

PYTHON PROGRAMMING
I Score 1 Questiions

1. What is the symbol used for single-line comments in Python?


2. Which keyword is used to define a function in Python?
3. What is the output of 3 + 5 * 2?
4. What is the data type of "Hello" in Python?
5. How do you check the type of a variable in Python?
6. What does the len() function do?
7. What is the index of the first element in a Python list?
8. How do you access the last element of a list my_list?
9. What is the output of True and False?
10. What is the output of 5 == "5"?
11. Which operator is used for exponentiation in Python?
12. How do you convert an integer to a string in Python?
13. What does the range(5) function generate?
14. Which keyword is used to start an if statement?
15. Which keyword is used to specify an alternative condition in an if statement?
16. What is the purpose of the for loop in Python?
17. How do you exit a loop prematurely in Python?
18. What is a dictionary in Python?
19. How do you access the value associated with the key "name" in a dictionary my_dict?
20. What is the output of [1, 2, 3] + [4, 5, 6]?

Answers:

1. #
2. def
3. 13
4. str (string)
5. type()
6. Returns the length of a sequence (string, list, tuple, etc.)
7. 0
8. my_list[-1]
9. False
10. False
11. **
12. str()
13. A sequence of numbers from 0 to 4 (0, 1, 2, 3, 4)
14. if
15. elif
16. To iterate over a sequence (string, list, tuple, etc.)
17. break
18. A collection of key-value pairs
19. my_dict["name"]
20. [1, 2, 3, 4, 5, 6]
II Score 2 Questions

1. Explain the difference between == and is in Python.


2. Explain the difference between a list and a tuple in Python. Give one example of
when you would use each.
3. Write a code to check if a number is even or odd.
4. Explain what is slicing in Python and give an example using a string.
5. Write a code to find the largest number in a list.
6. Write a function that takes two arguments and returns their sum.
7. Explain the use of the in operator in Python. Give two examples.
8. Write a code that prints numbers from 1 to 10 using a while loop.
9. Write a code to reverse a string.
10. Explain the purpose of the continue statement in a loop.
11. Write a function to calculate the factorial of a given number.
Answers:

1. == checks for value equality, while is checks for object identity (whether they refer to the
same memory location). Example: a = [1, 2]; b = [1, 2]; a == b is True, a is b is False.
2. Lists are mutable (can be changed), while tuples are immutable. Lists are used for
collections of items that might need modification, while tuples are used for fixed collections
of items.
3. num = 10
if num % 2 == 0:
print("Even")
else:
print("Odd")
4. Slicing extracts a part of a sequence. Example: s = "Hello"; s[1:4] returns "ell".
Python
5. num = [3, 1, 4, 1, 5, 9]
largest = max(num)
print(largest)
6. def add(x, y):
return x + y
7. The in operator checks if a value is present in a sequence. Example: 3 in [1, 2, 3] is True.
"apple" in "pineapple" is also True.
8. i = 1
while i <= 10:
print(i)
i += 1
9. s = "hello"
reversed_s = s[::-1]
print(reversed_s)
10. The continue statement skips the rest of the current iteration of a loop and proceeds to the
next iteration.
11. def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
III Score 3 Questions

Question 1: What are variables in Python? How do you declare and assign values to them?

Answer: Variables are used to store data values. They are like containers in memory that hold
information. In Python, you declare a variable by simply assigning a value to it using the
assignment operator (=).
Example:-
name = "Alice" # String variable
age = 30 # Integer variable
height = 5.8 # Float variable

Question 2: Explain the difference between print() and input() functions in Python.

Answer:
print() is used to display output to the console. It can take various arguments, including
strings, numbers, and variables.
input() is used to get input from the user. It takes an optional prompt message as an argument
and returns the user's input as a string.
Example:
name = input("Enter your name: ")
print("Hello, " + name + "!")

Question 3: What are the basic data types in Python? Give examples.

Answer: Python has several built-in data types:

1 .Integer (int): Whole numbers (e.g., 10, -5, 0)


2. Float (float): Decimal numbers (e.g., 3.14, -2.5, 0.0)
3. String (str): Textual data (e.g., "Hello", 'Python')
4. Boolean (bool): True or False values

Question 4: What are operators in Python? List a few arithmetic operators.

Answer: Operators are symbols that perform operations on values and variables. Arithmetic
operators include:

1. + (addition)
2. - (subtraction)
3. * (multiplication)
4. / (division)
5. // (floor division)
6. % (modulus)
7. ** (exponentiation)

Question 5: Explain the use of if, elif, and else statements in Python.

Answer: These are conditional statements used to execute different blocks of code based on
whether a condition is true or false.

if executes a block of code if a condition is true.


elif (else if) checks another condition if the previous if condition is false.
else executes a block of code if all preceding conditions are false.
Python

age = 20
if age < 18:
print("Minor")
elif age < 60:
print("Adult")
else:
print("Senior")

Question 6: What are loops in Python? Explain for loop with an example.

Answer: Loops are used to repeat a block of code multiple times.The for loop iterates over a
sequence (like a list, tuple, or string).

fruits = ["apple", "banana", "cherry"]


for i in fruits:
print(i)

Question 7: Explain the while loop with an example.

Answer: The while loop repeats a block of code as long as a condition is true.

count = 0
while count < 5:
print(count)
count += 1

Question 8: What are lists in Python? How do you access elements in a list?

Answer: Lists are ordered, mutable sequences of items. You access elements using indexing,
starting from 0.

my_list = [10, 20, 30, 40]


print(my_list[0]) # Output: 10
print(my_list[2]) # Output: 30

Question 9: What are tuples in Python? How do they differ from lists?

Answer: Tuples are ordered, immutable sequences of items. They are similar to lists but
cannot be modified after creation. Tuples are defined using parentheses (), while lists use
square brackets [].

Question 10: What are dictionaries in Python? How do you access values in a dictionary?

Answer: Dictionaries are unordered collections of key-value pairs. You access values using
their keys.

my_dict = {"name": "Bob", "age": 25}


print(my_dict["name"]) # Output: Bob

Question 11: What are functions in Python? How do you define a function?
Answer: Functions are reusable blocks of code that perform a specific task. You define a
function using the def keyword.

def greet(name):
print("Hello, " + name + "!")
greet("Charlie")

Question 12: What is the return statement used for in a function?

Answer: The return statement is used to exit a function and optionally return a value to the
caller.

def add(x, y):


return x + y
result = add(5, 3)
print(result) # Output: 8

Question 13: What are modules in Python? How do you import a module?

Answer: Modules are files containing Python code that can be reused in other programs. You
import a module using the import keyword.

import math
print(math.sqrt(16)) # Output: 4.0

Question 14: Explain the use of the for loop with range() function.

Answer: The range() function generates a sequence of numbers, which can be used in a for
loop to iterate a specific number of times.

Python

for i in range(5): # Iterates from 0 to 4


print(i)
Question 15: What are string methods in Python? Give an example.

Answer: String methods are built-in functions that operate on strings. For example, upper()
converts a string to uppercase.

text = "hello"
print(text.upper()) # Output: HELLO

Question 16: How do you handle exceptions in Python?

Answer: Exceptions are errors that occur during program execution.

Question 17: What is file I/O in Python?

Answer: File I/O (Input/Output) refers to reading data from and writing data to files.

Question 18: How do you open a file in Python?


Answer: You use the open() function to open a file.

file = open("my_file.txt", "r") # Opens the file in read mode

Question 19: How do you read data from a file in Python?

Answer: You can use methods like read(), readline(), or iterate over the file object.

file = open("my_file.txt", "r")


content = file.read()
print(content)
file.close()

Question 20: How do you write data to a file in Python?

Answer: You open the file in write mode ("w") or append mode ("a") and use the write()
method.

file = open("my_file.txt", "w")


file.write("Hello, file!")
file.close()

You might also like