NCERT Solutions: Getting Started with Python — CBSE Class 11 Computer Science
This page provides complete NCERT exercise solutions for CBSE Class 11 Computer Science Chapter 5 — Getting Started with Python. Every question is answered with a clear explanation and working Python code.
How to Use These Solutions
1. Attempt the question yourself first — write the code on paper or in IDLE before reading the solution. 2. Compare your answer — check the logic, variable names, and output format against the solution. 3. Understand every line — do not copy blindly. If a line is unclear, revisit the [chapter guide](/class/11/computer-science/cbse-getting-started-python) for the concept. 4. Run the code — type each program in IDLE or any Python editor and verify the output. 5. Fix your mistakes — note where you went wrong in a mistake notebook before moving to the next question.
Key Concepts Quick Review
Before solving the exercises, make sure you recall these fundamentals:
| Concept | Key Point |
|---|---|
input() | Always returns a string; use int() or float() for numbers |
print() | Use sep and end parameters; use f-strings for formatting |
type() | Returns the data type of a value or variable |
| Type conversion | int(), float(), str(), bool() convert between types |
| Arithmetic | / = float division, // = floor division, % = remainder, ** = power |
| Comparison | == != < > <= >= return True or False |
| Logical | and (both True), or (at least one True), not (reverse) |
| Assignment | += -= *= /= //= shorthand for updating variables |
| Precedence | * > unary > / // % > + - > comparison > logical |
| Dynamic typing | Variable type changes based on the value assigned |
Exercise Solutions
Q1. Write a program to add two numbers and display the result.
# Program to add two numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
result = num1 + num2
print(f"The sum of {num1} and {num2} is {result}")
Output:
Q2. Write a program to find the area of a rectangle. Take length and breadth from the user.
# Program to find area of a rectangle
length = float(input("Enter length of rectangle: "))
breadth = float(input("Enter breadth of rectangle: "))
area = length * breadth
print(f"Area of rectangle = {area:.2f}")
Output:
Q3. Write a program to convert temperature from Celsius to Fahrenheit.
The formula is: F = (C x 9/5) + 32
# Program to convert Celsius to Fahrenheit
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9 / 5) + 32
print(f"{celsius}°C = {fahrenheit:.2f}°F")
Output:
Q4. Write a program to swap two variables without using a third variable.
# Program to swap two variables without a temporary variable
a = int(input("Enter first number (a): "))
b = int(input("Enter second number (b): "))
print(f"Before swapping: a = {a}, b = {b}")
# Swapping using arithmetic
a = a + b
b = a - b
a = a - b
print(f"After swapping: a = {a}, b = {b}")
Output:
Alternative method using Python's multiple assignment:
a = int(input("Enter a: "))
b = int(input("Enter b: "))
a, b = b, a
print(f"After swap: a = {a}, b = {b}")
Q5. Write a program to find the data type of a value entered by the user.
# Program to find the data type of user input
value = input("Enter a value: ")
print(f"Input as string: {value}, type = {type(value)}")
# Try converting to int
try:
int_value = int(value)
print(f"As integer: {int_value}, type = {type(int_value)}")
except ValueError:
print("Cannot convert to integer")
# Try converting to float
try:
float_value = float(value)
print(f"As float: {float_value}, type = {type(float_value)}")
except ValueError:
print("Cannot convert to float")
Output (when user enters 42):
Q6. Write a program to demonstrate the use of arithmetic operators on two integers.
# Program to demonstrate arithmetic operators
a = int(input("Enter first integer: "))
b = int(input("Enter second integer: "))
print(f"{a} + {b} = {a + b}")
print(f"{a} - {b} = {a - b}")
print(f"{a} * {b} = {a * b}")
print(f"{a} / {b} = {a / b}")
print(f"{a} // {b} = {a // b}")
print(f"{a} % {b} = {a % b}")
print(f"{a} ** {b} = {a ** b}")
Output:
Q7. Write a program that accepts the user's name and prints a personalised greeting.
# Program to accept name and print greeting
name = input("Enter your name: ")
print(f"Hello, {name}! Welcome to Python programming.")
Output:
Q8. Write a program to calculate simple interest.
The formula is: SI = (P x R x T) / 100
# Program to calculate simple interest
principal = float(input("Enter principal amount: "))
rate = float(input("Enter rate of interest (%): "))
time = float(input("Enter time (years): "))
simple_interest = (principal * rate * time) / 100
total_amount = principal + simple_interest
print(f"Simple Interest = {simple_interest:.2f}")
print(f"Total Amount = {total_amount:.2f}")
Output:
Q9. Write a program to calculate the area and circumference of a circle.
# Program to calculate area and circumference of a circle
radius = float(input("Enter the radius of the circle: "))
pi = 3.14159
area = pi * radius ** 2
circumference = 2 * pi * radius
print(f"Area = {area:.2f}")
print(f"Circumference = {circumference:.2f}")
Output:
Q10. Write a program to accept two numbers and demonstrate relational operators.
# Program to demonstrate relational operators
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(f"{a} == {b} : {a == b}")
print(f"{a} != {b} : {a != b}")
print(f"{a} < {b} : {a < b}")
print(f"{a} > {b} : {a > b}")
print(f"{a} <= {b} : {a <= b}")
print(f"{a} >= {b} : {a >= b}")
Output:
Q11. Write a program to demonstrate logical operators.
# Program to demonstrate logical operators
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(f"({a} > 0) and ({b} > 0) : {(a > 0) and (b > 0)}")
print(f"({a} > 0) or ({b} > 0) : {(a > 0) or (b > 0)}")
print(f"not({a} > 0) : {not(a > 0)}")
Output:
Q12. Write a program to evaluate an arithmetic expression and show operator precedence.
# Program to demonstrate operator precedence
a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))
result1 = a + b * c # multiplication first
result2 = (a + b) * c # parentheses override
result3 = a ** b + c # exponentiation first
result4 = a + b // c # floor division first
print(f"a + b * c = {a} + {b} * {c} = {result1}")
print(f"(a + b) * c = ({a} + {b}) * {c} = {result2}")
print(f"a ** b + c = {a} ** {b} + {c} = {result3}")
print(f"a + b // c = {a} + {b} // {c} = {result4}")
Output:
Related Pages
- [Getting Started with Python — Chapter Guide](/class/11/computer-science/cbse-getting-started-python)
- [Flow of Control — Chapter Guide](/class/11/computer-science/cbse-flow-of-control)
- [Flow of Control — Exercise Solutions](/class/11/computer-science/solutions/cbse-flow-of-control-exercise-solutions)
