0% found this document useful (0 votes)
5 views3 pages

Python All Questions With Answers

The document covers various Python programming concepts including mutable vs immutable data types, checking even or odd numbers, the importance of indentation, and the differences between lists and tuples. It also includes examples of recursion for calculating factorial, type casting, and the use of append() vs extend() methods. Additionally, it discusses local and global variables, object-oriented programming, file handling, exception handling, modules, pattern printing, and string operations.

Uploaded by

dclover371
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)
5 views3 pages

Python All Questions With Answers

The document covers various Python programming concepts including mutable vs immutable data types, checking even or odd numbers, the importance of indentation, and the differences between lists and tuples. It also includes examples of recursion for calculating factorial, type casting, and the use of append() vs extend() methods. Additionally, it discusses local and global variables, object-oriented programming, file handling, exception handling, modules, pattern printing, and string operations.

Uploaded by

dclover371
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

1.

Explain the difference between mutable and immutable data types in Python with
examples.

Mutable data types can be changed after creation, whereas immutable data types cannot be
modified.
# Mutable example
lst = [1, 2, 3]
lst[0] = 10
print(lst)

# Immutable example
t = (1, 2, 3)
# t[0] = 10 # Error

2. Write a Python program to check whether a number is even or odd.


num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")

3. Explain the use of indentation in Python.

Indentation defines code blocks in Python. Incorrect indentation raises IndentationError.

4. What are lists and tuples? Write two differences.

Lists are mutable collections, whereas tuples are immutable. Lists use [], tuples use ().

5. Write a Python function to find factorial using recursion.


def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n-1)

print(factorial(5))

6. Explain type casting in Python with examples.


x = "10"
y = int(x)
a = 5
b = float(a)

7. Difference between append() and extend() methods.


lst = [1, 2]
[Link]([3, 4])
print(lst)

lst = [1, 2]
[Link]([3, 4])
print(lst)

8. What are local and global variables?


x = 10
def demo():
y = 5
print(y)
demo()
print(x)

SOLVED SECTION

1. List operations program


numbers = []
for i in range(5):
[Link](int(input("Enter number: ")))

print("Sum:", sum(numbers))
print("Average:", sum(numbers)/len(numbers))
print("Largest:", max(numbers))
print("Smallest:", min(numbers))

2. Object Oriented Programming concepts


class Student:
def __init__(self, name):
[Link] = name

s = Student("Rahul")

3. File handling program


with open("[Link]", "r") as file:
lines = [Link]()

print("Lines:", len(lines))
print("Words:", sum(len([Link]()) for line in lines))
print("Characters:", sum(len(line) for line in lines))

4. Exception handling using try-except-finally


try:
a = int(input())
b = int(input())
print(a / b)
except ZeroDivisionError:
print("Division by zero")
except ValueError:
print("Invalid input")
finally:
print("Done")

5. Modules and packages


# [Link]
def greet():
print("Hello")

import mymodule
[Link]()

6. Pattern printing program


for i in range(1, 5):
for j in range(1, i+1):
print(j, end=" ")
print()

7. String operations in Python


text = "Python"
print(text[1:4])
print([Link]())
print([Link]())
print([Link]("P", "J"))

You might also like