Q.Write the corresponding Python assignment statements:
This question requires writing Python assignment statements to store various types of data—integers, lists of strings, and individual strings—into variables, and then performing an arithmetic operation and string concatenation.
In Python, an assignment statement is used to bind a value to a variable name. The basic syntax is variable_name = value. The value can be a literal (like a number or a string), a result of an expression (like an arithmetic calculation), or a data structure (like a list).
Let's break down each part of the question.
a) Assign 10 to variable length and 20 to variable breadth.
This involves simple direct assignment of integer literals to two distinct variables.
length = 10
breadth = 20
length = 10: The integer value10is assigned to the variable namedlength.breadth = 20: The integer value20is assigned to the variable namedbreadth.
b) Assign the average of values of variables length and breadth to a variable sum.
First, we need to calculate the sum of length and breadth, then divide by 2 to find the average. The result of this expression is then assigned to the variable sum. Note that the question asks to assign the average to a variable named sum, which might be slightly confusing as sum usually implies the total. However, we must follow the variable name specified.
# Assuming 'length' and 'breadth' are already defined from part (a)
average_value = (length + breadth) / 2
sum = average_value # Assigning the calculated average to 'sum' as per the question
(length + breadth): This calculates the sum of the values currently stored inlengthandbreadth./ 2: This divides the sum by 2 to compute the average. Python's/operator performs float division, which is appropriate for calculating averages.sum = average_value: The calculated average is assigned to the variablesum.
Be careful with variable names. While sum is often used for a total, here the question explicitly asks to assign the average to sum. Always follow the exact instructions for variable names.
c) Assign a list containing strings 'Paper', 'Gel Pen', and 'Eraser' to a variable stationery.
A list in Python is an ordered, mutable collection of items. It is defined by enclosing comma-separated items within square brackets []. Each item in this case is a string literal.
stationery = ['Paper', 'Gel Pen', 'Eraser']
['Paper', 'Gel Pen', 'Eraser']: This creates a list containing three string elements.stationery = ...: This list is then assigned to the variablestationery.
d) Assign the strings 'Mohandas', 'Karamchand', and 'Gandhi' to variables first, middle and last.
Python allows for multiple assignments in a single line, which is very convenient for assigning several values to several variables simultaneously. The number of variables on the left side must match the number of values on the right side.
first, middle, last = 'Mohandas', 'Karamchand', 'Gandhi'
'Mohandas', 'Karamchand', 'Gandhi': These are three string literals.first, middle, last = ...: The first string is assigned tofirst, the second tomiddle, and the third tolast.
e) Assign the concatenated value of string variables first, middle, last to variable fullname. Make sure to incorporate blank spaces appropriately between different parts of names.
String concatenation in Python can be done using the + operator. To include spaces between the names, we concatenate the variables with string literals containing a single space ' '.
# Assuming 'first', 'middle', and 'last' are already defined from part (d)
fullname = first + ' ' + middle + ' ' + last
first + ' ' + middle + ' ' + last: This expression concatenates the string infirst, a space, the string inmiddle, another space, and finally the string inlast.fullname = ...: The resulting concatenated string is assigned to the variablefullname.
For more readable and often more efficient string formatting, especially when combining variables and literals, f-strings (formatted string literals) are a modern Python feature introduced in Python 3.6.
fullname_fstring = f"{first} {middle} {last}"
This achieves the same result as concatenation but can be clearer for complex strings.
The complete Python assignment statements are:
# Part (a)
length = 10
breadth = 20
# Part (b)
average_value = (length + breadth) / 2
sum = average_value
# Part (c)
stationery = ['Paper', 'Gel Pen', 'Eraser']
# Part (d)
first, middle, last = 'Mohandas', 'Karamchand', 'Gandhi'
# Part (e)
fullname = first + ' ' + middle + ' ' + last
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.