1. In Python, the print() function is used to display output on the console.
2. A variable in Python can only store whole numbers (integers).
3. Comments in Python begin with the // symbol.
4. The input() function in Python is used to get data from the user.
5. String values in Python must be enclosed in single quotes or double quotes.
✏️ 2. Fill in the Blanks
1. A is a named storage location in a computer's memory that holds a value.
2. The function is used to display text or variable values to the user.
3. Data that consists of a sequence of characters, like 'Hello World', is called a .
4. To convert a user's input to a whole number, you can use the () function.
5. The () function pauses the program and waits for the user to type something and press Enter.
🔗 3. Matching
« A name that refers to a value stored in memory.
« A sequence of characters, such as letters, numbers, or symbols, enclosed in quotes.
« A whole number (positive, negative, or zero) without any decimal part.
« Outputs information to the console for the user to see.
« Allows a program to receive data from the user during execution.
✍️ 4. Short Answer Questions
1. What is the primary purpose of using comments in Python code?
💡 Suggested Answer: The primary purpose of comments in Python code is to make the code more readable and understandable for humans. They are ignored by the Python interpreter and are used to explain what the code does, why certain decisions were made, or to temporarily disable parts of the code.
2. Write a single line of Python code that assigns the string 'Python is fun!' to a variable named message.
💡 Suggested Answer: message = 'Python is fun!'
🎯 5. Multiple Choice
1. What will be the output of the following Python code snippet?\n\nprint('Hello' + ' ' + 'World')
2. Which of the following is a valid variable name in Python?
3. What data type would Python assign to the value 3.14?
📝 6. Open-Ended Questions
1. Write a Python program that asks the user for their favorite color and then prints a message incorporating that color. For example, if the user enters 'blue', the program should print 'Your favorite color is blue. That's a great choice!'
💡 Solution Steps:
Here's the Python program:
favorite_color = input("What is your favorite color? ") print("Your favorite color is " + favorite_color + ". That's a great choice!")
<strong>Explanation:</strong> 1. favorite_color = input("What is your favorite color? "): This line uses the input() function to display the prompt "What is your favorite color? " to the user. Whatever the user types and presses Enter is captured as a string and stored in the variable favorite_color. 2. print("Your favorite color is " + favorite_color + ". That's a great choice!"): This line uses the print() function to display the final message. It concatenates (joins) three strings: the literal string "Your favorite color is ", the value stored in the favorite_color variable, and the literal string ". That's a great choice!". The + operator is used for string concatenation.
2. Identify and correct the error in the following Python code snippet so that it prints 'My age is 15'. Explain what the original error was.
age = 15 print("My age is " + age)
💡 Solution Steps:
The original code has a TypeError because you cannot directly concatenate a string with an integer using the + operator in Python. The age variable holds an integer, while "My age is " is a string.
Here are two ways to correct the error:
<strong>Correction 1: Convert the integer to a string using str()</strong> age = 15 print("My age is " + str(age))
<strong>Explanation for Correction 1:</strong> The str() function converts the integer value of age (which is 15) into its string representation ("15"). Then, string concatenation works correctly.
<strong>Correction 2: Use an f-string (formatted string literal)</strong> age = 15 print(f"My age is {age}")
<strong>Explanation for Correction 2:</strong> An f-string allows you to embed expressions directly inside string literals by placing them inside curly braces {}. Python automatically converts the age variable to its string representation within the f-string.
3. Explain the difference between a string and an integer data type in Python, and provide an example for each.
💡 Solution Steps:
<strong>String (str):</strong> * A string is a sequence of characters, such as letters, numbers, symbols, or spaces. * Strings are used to represent text. * They must always be enclosed in single quotes ('...') or double quotes ("..."). * <strong>Example:</strong> name = "Alice" or message = 'Hello world!'
<strong>Integer (int):</strong> * An integer is a whole number (positive, negative, or zero) without any decimal point or fractional part. * Integers are used to represent numerical values that are exact counts or whole quantities. * They are not enclosed in quotes. * <strong>Example:</strong> age = 15 or count = -10
The key difference is their purpose and how they are stored and manipulated. Strings are for text, and integers are for whole numbers.
Name Surname: .................................. Date: .... / .... / 202...
Python Coding Worksheet Worksheet
SCORE
A. True (T) / False (F)
( .... )
In Python, the print() function is used to display output on the console.
( .... )
A variable in Python can only store whole numbers (integers).
( .... )
Comments in Python begin with the // symbol.
( .... )
The input() function in Python is used to get data from the user.
( .... )
String values in Python must be enclosed in single quotes or double quotes.
B. Fill in the Blanks
1)
A .................... is a named storage location in a computer's memory that holds a value.
2)
The .................... function is used to display text or variable values to the user.
3)
Data that consists of a sequence of characters, like 'Hello World', is called a .....................
4)
To convert a user's input to a whole number, you can use the ....................() function.
5)
The ....................() function pauses the program and waits for the user to type something and press Enter.
C. Matching Concepts
( .... )
A name that refers to a value stored in memory.
- Input Function
( .... )
A sequence of characters, such as letters, numbers, or symbols, enclosed in quotes.
- Variable
( .... )
A whole number (positive, negative, or zero) without any decimal part.
- Integer
( .... )
Outputs information to the console for the user to see.
- Print Function
( .... )
Allows a program to receive data from the user during execution.
- String
D. Short Answer Questions
1)
What is the primary purpose of using comments in Python code?
2)
Write a single line of Python code that assigns the string 'Python is fun!' to a variable named message.
E. Multiple Choice Questions
1)
What will be the output of the following Python code snippet?\n\nprint('Hello' + ' ' + 'World')
A) Hello WorldB) HelloWorldC) Hello+ +WorldD) Error
2)
Which of the following is a valid variable name in Python?
A) 1st_numberB) my-variableC) total_amountD) for
3)
What data type would Python assign to the value 3.14?
A) IntegerB) StringC) BooleanD) Float
F. Open-Ended Questions
1)
Write a Python program that asks the user for their favorite color and then prints a message incorporating that color. For example, if the user enters 'blue', the program should print 'Your favorite color is blue. That's a great choice!'
2)
Identify and correct the error in the following Python code snippet so that it prints 'My age is 15'. Explain what the original error was.
age = 15 print("My age is " + age)
3)
Explain the difference between a string and an integer data type in Python, and provide an example for each.