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

ICSE Python Code Questions Answers

Uploaded by

sm221278
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

ICSE Python Code Questions Answers

Uploaded by

sm221278
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ICSE Python Code Questions and Answers

1. Basic Concepts
 Q1. Write a Python program to take user input and print the result.

Code:
name = input("Enter your name: ")
print("Hello, " + name)
 Q2. Explain different data types in Python.

Answer: The basic data types in Python include integers, floats, strings, and booleans.

2. Operators
 Q1. What are the types of operators in Python?

Answer: Python includes arithmetic, relational, logical, assignment, and bitwise operators.
 Q2. Write a program to demonstrate arithmetic operations.

Code:
a = 10
b=5
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)

3. Control Structures
 Q1. Write a Python program to check if a number is even or odd using an if statement.

Code:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
 Q2. Explain the 'for' loop with an example.

Code:
for i in range(5):
print(i) # Prints numbers from 0 to 4

4. Functions
 Q1. Write a function to add two numbers.
Code:
def add(x, y):
return x + y

result = add(5, 10)


print("Sum:", result)
 Q2. Explain the use of return statement in Python functions.

Answer: The return statement allows a function to output a value to where it was called.

5. Lists, Tuples, and Dictionaries


 Q1. Write a program to add elements to a list.

Code:
my_list = []
my_list.append(1)
my_list.append(2)
print(my_list)
 Q2. What are the differences between lists and tuples?

Answer: Lists are mutable, meaning they can be modified, while tuples are immutable.

6. String Manipulation
 Q1. Write a Python program to reverse a string.

Code:
string = "Hello"
reversed_string = string[::-1]
print("Reversed:", reversed_string)
 Q2. Explain string slicing with an example.

Code:
string = "Hello"
print(string[1:4]) # Output: 'ell'

7. File Handling
 Q1. Write a Python program to write to a file.

Code:
with open("output.txt", "w") as file:
file.write("Hello, world!")
 Q2. How do you read from a file in Python?
Code:
with open("output.txt", "r") as file:
content = file.read()
print(content)

8. Object-Oriented Concepts
 Q1. Define a class with a simple method.

Code:
class MyClass:
def greet(self):
print("Hello")

obj = MyClass()
obj.greet()
 Q2. Explain inheritance with an example.

Code:
class Parent:
def func1(self):
print("Parent method")

class Child(Parent):
def func2(self):
print("Child method")

obj = Child()
obj.func1() # Calls method from Parent class
obj.func2() # Calls method from Child class

You might also like