Assessment of Python

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

1.

Variables and Data Types

• Explain why Python is a dynamically typed language. How


does this impact variable declarations?

• Create a Python program that assigns your name to a variable


and then prints the variable and its type.

• You have a variable price = 23.17. Write a Python program


that checks the type of this variable and prints an appropriate message
based on whether it’s an integer or a float datatype.

2. Type Conversions

• Why is type conversion necessary in Python? What could


happen if it’s not done properly?

• Write a program that asks for two user inputs, converts them
to integers, adds them, and displays the result.

• Imagine you’re reading user input as a string but need to


perform arithmetic operations on it. What would be the best way to handle
this situation? Provide an example.

3. Lists and Dictionaries

• How would you explain the difference between mutable and


immutable data types using lists and dictionaries as examples?

• Write a Python program that creates a list of favorite movies,


then updates the list by adding another movie. Also, create a dictionary
for a movie with attributes like title, genre, and rating.

• You’re building an inventory system. Use a list to keep track of


product categories and a dictionary to store product details (e.g., name,
price). How would you add new products and categories?

4. Control Structures
• In Python, how do control structures like if-else and elif
improve the decision-making process in a program?

• Write a Python program using control structures that


determines whether a number is divisible by both 3 and 5, only by 3, or
only by 5.

• Suppose you’re developing a game where players earn points.


If the score is greater than 1500, they level up. If the score is less than
350, they lose a life. Write a Python control structure to handle these
game mechanics.

5. Loops

• Why would you choose a while loop over a for loop in Python?
Provide a situation where one is more appropriate than the other.

• Write a Python program that prints all the numbers from 10 to


1 in reverse order using a loop.

• You’re tasked with collecting user feedback continuously until


they decide to stop by entering ‘quit’. Use a loop to implement this and
display their feedback after each input.

6. Functions

• What is the purpose of defining functions in programming?


How do they enhance code reusability?

• Define a Python function that takes a person’s name and age


as arguments and prints a message saying whether they are eligible to
vote.

• You are creating an application that calculates the area of


different shapes. Define a function calculate_area() that takes shape type
and dimensions as arguments and returns the area of a rectangle or circle.

7. List Comprehensions
• How do list comprehensions improve the readability and
efficiency of code? Compare a traditional loop with a list comprehension.

• Use a list comprehension to create a new list that contains the


squares of all even numbers between 1 and 10.

• You have a list of students’ scores. Write a list comprehension


to filter out scores that are below 45 and then print only the passing
scores.

You might also like