DEV Community

Santhoshi Mary A
Santhoshi Mary A

Posted on

TASK 2-Constant and Variables

1️. Create a variable name and print it
name = "Santhoshi Mary A"
print(name)

Explanation:

A variable stores data.
Here, the variable name stores a string.
print(name) displays the stored value.
2️. Create a variable age, reassign it, and print the new value
age = 19
print("Original age:", age)

age = 20
print("Updated age:", age)

Explanation:

Variables can be reassigned.
First, age is 19.
Later, we update it to 20.
Python allows changing variable values anytime.
3️. Assign 5, 10, and 15 to variables in a single line
a, b, c = 5, 10, 15
print(a, b, c)

Explanation:

Python allows multiple assignments in one line.
Values are assigned in order.
4️. Swap two variables without using a third variable
x = 10
y = 20

print("Before swapping:")
print("x =", x)
print("y =", y)

x, y = y, x

print("After swapping:")
print("x =", x)
print("y =", y)

Explanation:

Python allows direct swapping using tuple unpacking.
No temporary variable is needed.
5️. Define a constant PI and print it
PI = 3.14159
print("Value of PI:", PI)

Explanation:

Python does not have true constants.
By convention, constants are written in CAPITAL letters.
PI is treated as a constant value.
6️. Calculate area of a circle using constant PI
PI = 3.14159
radius = 7

area = PI * radius * radius
print("Area of the circle:", area)

Explanation:
Formula for area of a circle:

Area = π × r²

We multiply:

PI
radius
radius
7️. Calculate area of a rectangle using constants
LENGTH = 10
WIDTH = 5

area = LENGTH * WIDTH
print("Area of rectangle:", area)

Explanation:
Formula:

Area = Length × Width

Both values are treated as constants.

8️. Calculate circumference of a circle
PI = 3.14159
radius = 7

circumference = 2 * PI * radius
print("Circumference of the circle:", circumference)

Explanation:
Formula:

Circumference = 2 × π × r

We multiply:

2
PI
radius

Top comments (0)