I Year Python
I Year Python
PYTHON PROGRAMMING
I Score 1 Questiions
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. == 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: 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.
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).
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.
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.
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")
Answer: The return statement is used to exit a function and optionally return a value to the
caller.
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
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
Answer: File I/O (Input/Output) refers to reading data from and writing data to files.
Answer: You can use methods like read(), readline(), or iterate over the file object.
Answer: You open the file in write mode ("w") or append mode ("a") and use the write()
method.