Python Programming Test
Name: _______________________________
Date: ________________________________
Instructions:
1. Answer all questions.
2. Write your code or explanation as needed.
3. Code should be written in Python.
4. Be concise and clear in your responses.
Question 1: Basic Syntax (1 point)
Write a Python program to print "Hello, World!"
Question 2: Variables and Data Types (2 points)
a) Declare a variable num and assign it the value 5.
b) Convert num to a float and store it in a new variable float_num.
Question 3: Lists (2 points)
Create a list named fruits containing the elements "apple," "banana," and "orange."
Append "grape" to the list.
Question 4: Control Flow (3 points)
Write a Python function that takes an integer as an argument and returns "Even" if
the number is even and "Odd" if it's odd.
Question 5: Functions (2 points)
Define a function called cube that takes a number as a parameter and returns its
cube.
Question 6: String Manipulation (2 points)
Given the string sentence = "Python programming is fun!", extract and print
only the word "programming."
Question 7: Dictionaries (2 points)
Create a dictionary named person with keys "name" and "age," and values "John"
and 25, respectively.
Question 8: File Handling (2 points)
Write Python code to open a file named "example.txt" in write mode, write the string
"Hello, file handling!" to it, and then close the file.
Question 9: Exception Handling (2 points)
Write a try-except block to catch a ZeroDivisionError and print "Cannot divide by
zero!" if it occurs.
Question 10: Classes and Objects (2 points)
Create a class Circle with a constructor that takes the radius as a parameter. Add a
method area that calculates and returns the area of the circle.
Question 11: List Comprehension (2 points)
Generate a list containing the squares of the numbers from 1 to 5 using list
comprehension.
Question 12: Modules and Libraries (2 points)
Import the math module and use it to calculate the square root of 16.
Question 13: Lambda Functions (2 points)
Write a lambda function that squares its input.
Question 14: Decorators (3 points)
Create a decorator named logger that prints the name of the function being called
before calling it. Apply this decorator to a sample function.
Question 15: NumPy (3 points)
Install and import the NumPy library. Create a NumPy array with values ranging from
1 to 5.
Question 16: Pandas (3 points)
Install and import the Pandas library. Create a Pandas DataFrame with columns
"Name" and "Age" and add two rows of data.
Question 17: Regular Expressions (3 points)
Write a regular expression to validate a phone number in the format (XXX) XXX-
XXXX.
Question 18: Flask (3 points)
Install the Flask framework and create a simple web application with a route that
returns "Hello, Flask!"
Question 19: API Request (3 points)
Use the requests library to make a GET request to a public API (e.g.,
https://jsonplaceholder.typicode.com/todos/1) and print the response.
Question 20: Database Connection (3 points)
Install the sqlite3 library, create a SQLite database, and insert a record into a table
named "students" with columns "id," "name," and "age."
End of Test