0% found this document useful (0 votes)
4 views12 pages

Python Programming Worksheet

This document is a practice worksheet for Python programming, covering topics such as variables, data types, input/output, operators, conditional statements, loops, functions, modules, and lists. It includes multiple-choice questions and short answer questions with answers to test knowledge on these subjects. The content is structured into sections focusing on Python basics and more advanced concepts like conditionals and functions.

Uploaded by

Shakila Khan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Python Programming Worksheet

This document is a practice worksheet for Python programming, covering topics such as variables, data types, input/output, operators, conditional statements, loops, functions, modules, and lists. It includes multiple-choice questions and short answer questions with answers to test knowledge on these subjects. The content is structured into sections focusing on Python basics and more advanced concepts like conditionals and functions.

Uploaded by

Shakila Khan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Programming

Practice Worksheet — MCQs & Short Questions with


Answers

Unit 2: Python Programming


Topics Covered: Introduction to Python • Variables & Data Types • Input/Output • Operators • Conditional
Statements • Loops • Functions • Modules & Libraries • Lists
Part A — Python Basics, Variables, Data Types,
Input/Output & Operators
Section 1: Multiple Choice Questions
1. Who is known as the developer of Python?
a) Dennis Ritchie
b) Guido van Rossum
c) James Gosling
d) Bjarne Stroustrup
Answer: b) Guido van Rossum
2. Python was named after:
a) A type of snake
b) The British comedy show "Monty Python's Flying Circus"
c) Its creator's nickname
d) A Greek mythological figure
Answer: b) The British comedy show "Monty Python's Flying Circus"
3. Which symbol is used to write a single-line comment in Python?
a) //
b) /* */
c) #
d) --
Answer: c) #
4. Which of the following is a valid variable name in Python?
a) 2age
b) user age
c) _age
d) age-1
Answer: c) _age
5. Which data type would store the value True?
a) int
b) float
c) str
d) bool
Answer: d) bool
6. What does the input() function return by default?
a) Integer
b) Float
c) String
d) Boolean
Answer: c) String
7. What is the output of 10 // 3 in Python?
a) 3.33
b) 3
c) 1
d) 0
Answer: b) 3
8. What is the output of 10 % 3?
a) 3
b) 3.33
c) 1
d) 0
Answer: c) 1
9. Which operator is used for exponentiation in Python?
a) ^
b) **
c) //
d) %%
Answer: b) **
10. Which of these is a compound assignment operator?
a) ==
b) +=
c) !=
d) <=
Answer: b) +=
11. What will print(10 == 5) output?
a) 10
b) 5
c) True
d) False
Answer: d) False
12. Triple quotes (''' ''') are used in Python for:
a) Single-line comments
b) Multi-line comments
c) Defining variables
d) Mathematical operations
Answer: b) Multi-line comments
13. What must a Python variable name begin with?
a) A digit
b) A special symbol
c) A letter or underscore
d) An uppercase letter only
Answer: c) A letter or underscore
14. Which function converts user input into a whole number?
a) str()
b) float()
c) int()
d) bool()
Answer: c) int()
15. Which of the following is NOT a logical operator in Python?
a) and
b) or
c) not
d) xor
Answer: d) xor

Section 2: Short Questions with Answers


1. Who developed Python and in which year?
Answer:
Python was developed by Guido van Rossum in 1989/1990 (released around 1991).

2. Explain the origin of the name “Python.”


Answer:
Python is not named after the snake. It was named after the British comedy show “Monty
Python’s Flying Circus,” which Guido van Rossum was a fan of.

3. What is computer programming? Briefly describe the four steps involved in writing a
program.
Answer:
Computer programming is the process of creating a set of instructions that tell a computer
how to perform a task. The four steps are:
• Write Code – Create instructions in a programming language.
• Compile/Interpret – Translate the code into a form the computer understands.
• Execute – Run the code to perform the task.
• Output – Display the results or perform actions based on the code.

4. What is an IDE? Why is it useful?


Answer:
An IDE (Integrated Development Environment) is software that provides tools to write, run,
and debug code in one place. It is useful because it makes coding easier with features like
syntax highlighting, error detection, and built-in execution tools.

5. List any three rules for naming variables in Python.


Answer:
• The name must begin with a letter (a-z, A-Z) or an underscore (_).
• Subsequent characters can include letters, digits (0-9), or underscores.
• Variable names are case-sensitive (e.g., age and Age are different).
(Also acceptable: reserved keywords like for, while, if cannot be used; spaces and special
symbols are not allowed; hyphens are not allowed.)

6. Differentiate between single-line and multi-line comments in Python, with examples.


Answer:
Single-line comment: Starts with #. Example:
# This is a single-line comment
Multi-line comment: Enclosed using triple quotes (''' ''') at the start and end, and can span
multiple lines.

7. What is the difference between an int and a float data type? Give one example of each.
Answer:
• int (Integer): Stores whole numbers. Example: age = 17
• float (Floating-point): Stores decimal/fractional numbers. Example: price = 19.99

8. Explain the purpose of the input() and print() functions with an example for each.
Answer:
input(): Used to take data from the user.
name = input("Enter your name: ")
print(): Used to display output/information on the screen.
print("Hello, " + name + "!")

9. Why do we use int() or float() when taking numeric input from a user?
Answer:
Because the input() function always returns data as a string, even if the user types numbers.
To perform mathematical operations on the input, it must be converted (type cast) into an
integer using int() or a decimal number using float().

10. What are arithmetic operators? List any four with their symbols.
Answer:
Arithmetic operators perform basic mathematical operations:
• + (Addition)
• - (Subtraction)
• * (Multiplication)
• / (Division)
(Others: // floor division, % modulus, ** exponentiation)

11. What is the difference between / (division) and // (floor division)? Give an example
showing different outputs.
Answer:
/ performs normal division and returns a float result. Example: 10 / 3 → Output:
3.3333333333333335
// performs floor division and returns the whole number (rounded down) part of the result.
Example: 10 // 3 → Output: 3

12. What are comparison (relational) operators? Give two examples with their output.
Answer:
Comparison operators compare two values and return a Boolean result (True/False).
• > Greater than → 10 > 5 → Output: True
• == Equal to → 10 == 5 → Output: False

13. What is an assignment operator? Differentiate between = and += with an example.


Answer:
An assignment operator assigns a value to a variable.
= simply assigns a value. Example: a = 10 → a becomes 10.
+= is a compound assignment operator that adds a value to the variable and assigns the
result back to it. Example: a += b (where a=10, b=5) → a becomes 15 (equivalent to a = a +
b).

14. What are logical operators in Python? Name the three main ones.
Answer:
Logical operators are used to combine multiple conditions or expressions in a program. The
three main ones are: and, or, not.

15. Define “L-value” and “R-value.”


Answer:
L-value (Left value): Refers to the variable on the left side of an assignment — it must always
be a single variable (location in memory).
R-value (Right value): Refers to the value or expression on the right side of an assignment —
it can be a constant, variable, or expression.
Part B — Conditionals, Loops, Functions, Modules &
Lists
Section 1: Multiple Choice Questions
1. Which keyword is used to check an additional condition after an if statement in
Python?
a) elseif
b) elif
c) else if
d) elseif:
Answer: b) elif
2. What is the output of the following code?
temperature = 15
if temperature > 30:
print("It's a hot day")
else:
print("It's not a hot day")
a) It's a hot day
b) It's not a hot day
c) Error
d) No output
Answer: b) It's not a hot day
3. Which loop is used when the number of iterations is NOT known in advance?
a) for loop
b) while loop
c) do-while loop
d) nested loop
Answer: b) while loop
4. Which loop is used when the number of iterations is fixed/known in advance?
a) while loop
b) for loop
c) if-else
d) elif
Answer: b) for loop
5. What is the correct syntax for a short-hand if-else statement?
a) if condition: action_if_true else: action_if_false
b) action_if_true if condition else action_if_false
c) condition if action_if_true else action_if_false
d) if action_if_true else action_if_false
Answer: b) action_if_true if condition else action_if_false
6. What will the following code print?
number = 1
while number < 10:
print(number)
number += 1
a) Numbers from 1 to 9
b) Numbers from 1 to 10
c) Only 1
d) Infinite loop
Answer: a) Numbers from 1 to 9
7. Which function in Python is commonly used with a for loop to generate a sequence of
numbers?
a) sequence()
b) range()
c) loop()
d) series()
Answer: b) range()
8. Which keyword is used to define a function in Python?
a) function
b) def
c) func
d) define
Answer: b) def
9. What is a “parameter” in a function?
a) The value provided when a function is called
b) A variable defined in the function definition
c) The output of a function
d) A type of loop
Answer: b) A variable defined in the function definition
10. What is an “argument” in a function?
a) A variable defined in the function definition
b) The value provided to a function when it is called
c) The function name
d) A return statement
Answer: b) The value provided to a function when it is called
11. What will this code output?
def greet(name="Student"):
return "Hello " + name + "!"
print(greet())
a) Hello Student!
b) Hello!
c) Error
d) None
Answer: a) Hello Student!
12. Which method adds an item to the end of a list?
a) insert()
b) add()
c) append()
d) extend()
Answer: c) append()
13. Which method removes the first occurrence of a specific item from a list?
a) delete()
b) remove()
c) pop()
d) clear()
Answer: b) remove()
14. What is the index of the first item in a Python list?
a) 1
b) -1
c) 0
d) None
Answer: c) 0
15. What does numbers[1:4] do if numbers = [1, 2, 3, 4, 5]?
a) Returns items from index 1 to 4 (inclusive)
b) Returns items from index 1 to 3
c) Returns the entire list
d) Returns an error
Answer: b) Returns items from index 1 to 3
16. Which of the following is used to import a built-in library in Python?
a) include
b) import
c) using
d) require
Answer: b) import
17. Which library would you use to generate random numbers in Python?
a) datetime
b) statistics
c) random
d) math
Answer: c) random
18. What is a package in Python?
a) A single function
b) A directory containing related modules
c) A type of loop
d) A built-in data type
Answer: b) A directory containing related modules
19. Which method sorts a list in ascending order?
a) order()
b) sort()
c) arrange()
d) ascend()
Answer: b) sort()
20. What does the reverse() method do to a list?
a) Removes the last item
b) Sorts the list alphabetically
c) Reverses the order of items in the list
d) Returns the list size
Answer: c) Reverses the order of items in the list

Section 2: Short Questions with Answers


1. What is the difference between a while loop and a for loop?
Answer:
A while loop runs as long as a condition is true and is used when the number of iterations is
not known in advance. A for loop repeats a block of code a specific (fixed) number of times
and is commonly used to iterate over a sequence like a list, tuple, or string.

2. Write the syntax of an if-elif-else statement.


Answer:
if condition1:
# code to run if condition1 is true
elif condition2:
# code to run if condition2 is true
else:
# code to run if none of the conditions are true

3. What is a short-hand if-else statement? Give an example.


Answer:
It is a way of writing an if-else statement in a single line. Example:
temperature = 15
m = "It's a hot day" if (temperature > 30) else "It's not a hot day"
print(m)

4. Explain how a while loop works with an example.


Answer:
A while loop checks the condition before each iteration and keeps running as long as the
condition is true. It stops when the condition becomes false. Example:
number = 1
while number < 10:
print(number)
number += 1

5. What is the purpose of the range() function in a for loop?


Answer:
The range() function generates a sequence of numbers, which is commonly used with a for
loop to repeat an action a specific number of times.

6. What is a function in Python? Why are functions useful?


Answer:
A function is a reusable block of code defined using the def keyword that performs a specific
task. Functions are useful because they let you encapsulate reusable blocks of code, avoid
repetition, and make programs more organized and easier to manage.

7. Differentiate between a “parameter” and an “argument.”


Answer:
Parameter: A variable defined in the function definition (at the time of definition).
Argument: The actual value provided to the function when it is called.

8. What is a default parameter? Give an example.


Answer:
A default parameter is a value assigned to a function parameter that is used automatically if
no argument is passed during the function call. Example:
def greet(name="Student"):
return "Hello " + name + "!"
print(greet()) # Output: Hello Student!
print(greet("Umer")) # Output: Hello Umer!

9. What is the difference between a library and a module?


Answer:
A module is a single file containing Python code (functions, variables) that can be reused. A
library is a collection of modules bundled together, like a toolkit, that provides pre-built code so
you don't have to write everything from scratch.

10. What is a package in Python? Give an example.


Answer:
A package is a directory containing related modules, used to organize large projects.
Example: an ecommerce package could contain modules like [Link], [Link], and
[Link].

11. How do you import and use a library in Python? Give an example.
Answer:
You use the import keyword followed by the library name. Example:
import random
number = [Link](1, 10)
print("The random number is:", number)

12. What is a list in Python? How is it created?


Answer:
A list is a versatile data structure that can hold a collection of items (numbers, strings, or even
other lists). It is created by placing items inside square brackets [ ], separated by commas.
Example:
fruits = ["Mango", "Apple", "Banana"]

13. How do you access an item in a list? Give an example.


Answer:
You access list items by referring to their index, starting from 0. Example:
fruits = ["Mango", "Apple", "Banana"]
print(fruits[1]) # Output: Apple

14. List any four built-in methods used with Python lists and their purpose.
Answer:
• append(item) – Adds an item to the end of the list.
• remove(item) – Removes the first occurrence of an item from the list.
• sort() – Sorts the list in ascending order.
• reverse() – Reverses the order of the list.

15. What is list slicing? Give an example.


Answer:
List slicing means extracting a portion of a list using index ranges. Example:
numbers = [1, 2, 3, 4, 5]
slice = numbers[1:4] # Gets items from index 1 to 3
print(slice) # Output: [2, 3, 4]

16. What is list concatenation? Give an example.


Answer:
List concatenation means joining two lists together using the + operator. Example:
slice = [2, 3, 4]
extra_numbers = [6, 7]
combined = slice + extra_numbers
print(combined) # Output: [2, 3, 4, 6, 7]

You might also like