Model questions on INtroduction to Programming
Model questions on INtroduction to Programming
1. What is Python, and how does it differ from other programming languages?
2. Discuss the history of Python and its evolution over the years.
3. How do you download and install Python on your computer? Explain the process step by
step.
4. What are the different ways to run Python code?
5. Compare Python with another programming language of your choice (e.g., Java, C++) in
terms of syntax and application.
1. List and explain the core data types in Python. Provide an example for each.
2. What is the role of tokens in Python programming? Give examples of different tokens.
3. How does the input() function work? Illustrate with an example.
4. Explain the difference between print() and eval() functions in Python with examples.
5. What are variables in Python? How are values assigned to variables, and how is this
different from statically typed languages?
1. Describe the different decision-making statements in Python with an example (e.g., if-
else, if-elif-else).
2. What is the difference between a while loop and a for loop in Python? Provide use case
examples.
3. How do the continue, assert, pass, and return statements work in Python? Provide
examples of their usage.
4. What is the purpose of the break statement in Python? How does it differ from
continue?
5. Write a Python program to demonstrate the use of nested if-else statements and loops.
1. Define a function in Python. How do you pass parameters to a function and return values
from a function?
2. What is the difference between a local and a global variable in Python? Provide
examples.
3. Explain recursive functions in Python. Write a program to find the factorial of a number
using recursion.
4. What are lambda functions in Python? Give an example of a lambda function that adds
two numbers.
5. How do you create and manipulate lists in Python? Explain list slicing and provide an
example of list manipulation.
1. How do you create and access elements in a tuple? What are the benefits of using tuples
over lists?
2. Discuss the set data structure in Python. What are some common set operations such as
union, intersection, and difference?
3. What is the difference between a tuple and a list in Python? When would you use one
over the other?
4. How do you create a dictionary in Python? Provide examples of traversing and modifying
a dictionary.
5. Explain the concept of nested dictionaries in Python. Write a program to access values
from a nested dictionary.
1. How do you open and close a file in Python? Write a program to demonstrate reading
from and writing to a text file.
2. Explain how to write numbers to a file in Python. Give an example using the write()
function.
3. How can you read numbers from a file in Python? Provide a code example to read
integers from a file.
4. What is the seek() function, and how is it used in file handling? Provide an example.
5. Write a Python program to append data to an existing file.
Unit 1: Introduction to Python [4 Hrs]
1. What is Python, and how does it differ from other programming languages?
2. Discuss the history of Python and its evolution over the years.
3. How do you download and install Python on your computer? Explain the process
step by step.
4. What are the different ways to run Python code?
5. Compare Python with another programming language of your choice (e.g., Java, C+
+) in terms of syntax and application.
Python Programs:
python
Copy
# This Python program adds two numbers
num1 = 5
num2 = 3
sum = num1 + num2
print("The sum is:", sum)
1. List and explain the core data types in Python. Provide an example for each.
2. What is the role of tokens in Python programming? Give examples of different
tokens.
3. How does the input() function work? Illustrate with an example.
4. Explain the difference between print() and eval() functions in Python with
examples.
5. What are variables in Python? How are values assigned to variables, and how is this
different from statically typed languages?
Python Programs:
python
Copy
# This program takes user input and prints it
name = input("Enter your name: ")
print("Hello, " + name + "!")
python
Copy
# Using eval() to evaluate a mathematical expression
expression = input("Enter a mathematical expression (e.g., 3+4*2): ")
result = eval(expression)
print("The result is:", result)
Python Programs:
python
Copy
# Simple arithmetic operations
a = 10
b = 5
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
python
Copy
# Using relational operators to compare values
a = 10
b = 20
if a < b:
print("a is less than b")
else:
print("a is not less than b")
Unit 4: Control Statements [7 Hrs]
Python Programs:
python
Copy
# Using if-elif-else for decision making
num = int(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
python
Copy
# Print all numbers from 1 to 10, but skip even numbers using continue
for num in range(1, 11):
if num % 2 == 0:
continue
print(num)
1. Define a function in Python. How do you pass parameters to a function and return
values from a function?
2. What is the difference between a local and a global variable in Python? Provide
examples.
3. Explain recursive functions in Python. Write a program to find the factorial of a
number using recursion.
4. What are lambda functions in Python? Give an example of a lambda function that
adds two numbers.
5. How do you create and manipulate lists in Python? Explain list slicing and provide
an example of list manipulation.
Python Programs:
python
Copy
# Recursive function to find factorial
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
python
Copy
# List slicing example
my_list = [1, 2, 3, 4, 5, 6]
print("First three elements:", my_list[:3])
print("Last three elements:", my_list[-3:])
1. How do you create and access elements in a tuple? What are the benefits of using
tuples over lists?
2. Discuss the set data structure in Python. What are some common set operations
such as union, intersection, and difference?
3. What is the difference between a tuple and a list in Python? When would you use
one over the other?
4. How do you create a dictionary in Python? Provide examples of traversing and
modifying a dictionary.
5. Explain the concept of nested dictionaries in Python. Write a program to access
values from a nested dictionary.
Python Programs:
python
Copy
# Creating and accessing elements in a tuple
my_tuple = (1, 2, 3, 4)
print("First element:", my_tuple[0])
print("Last element:", my_tuple[-1])
python
Copy
# Creating and modifying a dictionary
my_dict = {"name": "Alice", "age": 25}
print("Name:", my_dict["name"])
my_dict["age"] = 26 # Updating value
print("Updated Age:", my_dict["age"])
1. How do you open and close a file in Python? Write a program to demonstrate
reading from and writing to a text file.
2. Explain how to write numbers to a file in Python. Give an example using the
write() function.
3. How can you read numbers from a file in Python? Provide a code example to read
integers from a file.
4. What is the seek() function, and how is it used in file handling? Provide an
example.
5. Write a Python program to append data to an existing file.
Python Programs:
python
Copy
# Writing to a file
with open("example.txt", "w") as file:
file.write("Hello, this is a test file.\n")
python
Copy
# Using seek() to read specific position in a file
with open("example.txt", "r") as file:
file.seek(6) # Move to the 6th character
content = file.read()
print(content)
These questions and programs are designed to help students practice key Python concepts and
improve their programming skills.
You said:
Certainly! Here's another set of questions with Python programs for each unit:
1. What is Python, and what are its key features that make it popular among
developers?
2. What are some common applications of Python in different fields such as web
development, data science, and artificial intelligence?
3. Explain how Python's simplicity and readability contribute to its popularity.
4. Discuss Python's cross-platform compatibility. How does this benefit developers?
5. Compare Python 2.x with Python 3.x. What are the main differences?
Python Programs:
python
Copy
# Simple Python program to print a message
print("Welcome to Python Programming!")
python
Copy
# Python program to perform simple arithmetic
num1 = 8
num2 = 4
result = num1 / num2
print("The result of division is:", result)
Python Programs:
python
Copy
# Taking user input and displaying the result
age = int(input("Enter your age: "))
print("You are", age, "years old.")
python
Copy
# Type casting in Python
float_value = 5.5
int_value = int(float_value)
print("The integer value is:", int_value)
Python Programs:
python
Copy
# Program to find even or odd using modulo operator
num = int(input("Enter a number: "))
if num % 2 == 0:
print(f"{num} is even.")
else:
print(f"{num} is odd.")
Program 2: Using Logical Operators
python
Copy
# Using logical operators
a = True
b = False
if a and not b:
print("Condition is True")
else:
print("Condition is False")
Python Programs:
python
Copy
# Nested if-else example
num = int(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
python
Copy
# Program to print numbers from 1 to 10, skipping multiples of 3
for num in range(1, 11):
if num % 3 == 0:
continue
print(num)
Python Programs:
python
Copy
# Recursive function to find Fibonacci numbers
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
python
Copy
# Sorting a list of tuples based on the second element using lambda
data = [(1, 4), (2, 3), (3, 2), (4, 1)]
data.sort(key=lambda x: x[1])
print("Sorted list:", data)
1. Explain the advantages of using tuples over lists. How do you create a tuple and
access its elements?
2. What are Python sets? How do they differ from lists and tuples? Discuss common
operations such as union and intersection.
3. What is a dictionary in Python? How do you create and modify dictionaries?
Provide an example with nested dictionaries.
4. How do you loop through tuples, sets, and dictionaries? Provide an example for
each.
5. Explain the zip() function in Python. Write a program to demonstrate its usage.
Python Programs:
python
Copy
# Using sets for union and intersection
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union = set1 | set2
intersection = set1 & set2
print("Union:", union)
print("Intersection:", intersection)
python
Copy
# Accessing values from a nested dictionary
person = {"name": "John", "address": {"city": "New York", "zip":
"10001"}}
print("City:", person["address"]["city"])
1. What is file handling in Python? How do you open, read, and write to a file?
Provide an example.
2. What are the different file modes in Python (e.g., 'r', 'w', 'a')? How do they affect
file operations?
3. Explain the use of the with statement in file handling. Why is it recommended?
4. How can you handle exceptions while working with files in Python?
5. Write a program to read a file line by line and count the number of words in the file.
Python Programs:
python
Copy
# Writing to a file using 'with' statement
with open("sample.txt", "w") as file:
file.write("This is a sample text.\n")
file.write("Python is amazing!")
This set of questions and programs covers the key concepts and practical skills related to each
unit, offering students a chance to deepen their understanding of Python programming.
Theoretical Questions:
1. What are the key features of Python that make it suitable for beginners and
professionals alike?
2. Explain the significance of Python's interpreted nature. How does it affect the way
programs are executed?
Programming Questions:
3. Write a Python program that prints a personalized greeting message for a user
based on their name entered as input.
4. Write a Python program that performs basic arithmetic operations (addition,
subtraction, multiplication, and division) based on user input.
Theoretical Questions:
1. What are the main data types in Python? Explain each one with an example.
2. What is the role of the input() function in Python? How can you handle different
types of data input?
Programming Questions:
3. Write a Python program to take two numbers as input and print their sum,
difference, and product.
4. Write a Python program that takes a string as input and prints the reverse of that
string.
Theoretical Questions:
Programming Questions:
3. Write a Python program that checks whether a given number is divisible by both 5
and 10.
4. Write a Python program that calculates the area of a circle, rectangle, and triangle
based on user input. Use appropriate operators.
Theoretical Questions:
1. What is the difference between a while loop and a for loop in Python? Explain
when you would use each one.
2. Explain the use of continue, break, and pass statements in Python. Provide an
example for each.
Programming Questions:
3. Write a Python program using a for loop to print all prime numbers between 1 and
50.
4. Write a Python program to find the largest of three numbers using if-elif-else
statements.
Theoretical Questions:
1. What is the difference between local and global variables in Python? Provide an
example demonstrating both.
2. Explain the concept of recursion in Python. Write a recursive function to calculate
the factorial of a number.
Programming Questions:
3. Write a Python program to define a function that takes a list of numbers as input
and returns the sum of all the numbers.
4. Write a Python program that creates a list of integers and sorts the list in
descending order.
Theoretical Questions:
1. What is the difference between a tuple and a list in Python? Discuss the advantages
of using tuples over lists.
2. How do sets in Python differ from lists and dictionaries? Explain the basic set
operations with examples.
Programming Questions:
3. Write a Python program to create a dictionary of students with their names as keys
and their scores as values. Display the dictionary.
4. Write a Python program to create a set of unique numbers from a list of numbers,
and perform set operations like union and intersection with another set.
Theoretical Questions:
1. What are file modes in Python? Explain the difference between modes such as 'r',
'w', and 'a'.
2. Explain the concept of file handling in Python. Why is the with statement used when
working with files?
Programming Questions:
3. Write a Python program to create a file and write multiple lines of text into it. Then,
read the content of the file and print it to the console.
4. Write a Python program to read a file line by line and count the number of
occurrences of a specific word in the file.
This set of questions balances both theoretical and practical aspects of Python, allowing students
to demonstrate their understanding of key concepts and their ability to apply these concepts
through programming.