Hi all this is my second task constant and variables...
1.Create and Print a Variable
Concept: A variable is used to store data that can be used later in a program.
name = "Jaya Sri"
print(name)
Explanation:
We create a variable called name and assign a string value to it. The print() function displays the stored value on the screen.
2.Reassign a Variable
Concept: Variables in Python can be updated with new values.
age = 18
print("Old age:", age)
age = 19
print("New age:", age)
Explanation:
Initially, the variable age stores 18. Later, it is reassigned to 19, showing that variables are flexible and can change during execution.
3.Multiple Assignment in One Line
Concept: Python allows assigning multiple variables in a single line.
a, b, c = 5, 10, 15
print(a, b, c)
Explanation:
The values are assigned in order. This makes the code shorter and more readable.
4.Swap Two Variables Without Third Variable
Concept: Python provides a simple way to swap values.
x = 5
y = 10
print("Before swap:", x, y)
x, y = y, x
print("After swap:", x, y)
Explanation:
Instead of using a temporary variable, Python swaps values directly in one line using tuple unpacking.
5.Define a Constant (PI)
Concept: Constants are values that should not change. Python uses uppercase names by convention.
PI = 3.14159
print(PI)
Explanation:
Although Python does not enforce constants, using uppercase letters indicates that the value should remain unchanged.
6.Area of a Circle
Concept: Use a formula with variables and constants.
Formula: Area = π × r²
PI = 3.14159
radius = 5
area = PI * radius ** 2
print("Area of circle:", area)
Explanation:
The radius is squared using ** 2, and multiplied by PI to calculate the area.
- Area of Rectangle Concept: Use constants for fixed values. Formula: Area = length × width
LENGTH = 10
WIDTH = 5
area = LENGTH * WIDTH
print("Area of rectangle:", area)
Explanation:
Both length and width are constants. The area is calculated by multiplying them.
- Circumference of a Circle Concept: Apply another mathematical formula. Formula: Circumference = 2 × π × r
PI = 3.14159
radius = 5
circumference = 2 * PI * radius
print("Circumference:", circumference)
Explanation:
The formula uses PI and radius to compute the boundary length of the circle.
Top comments (0)