0% found this document useful (0 votes)
7 views2 pages

Q4) Explain Variables in Python With Examples.

Uploaded by

vasukommaraju369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Q4) Explain Variables in Python With Examples.

Uploaded by

vasukommaraju369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

PYTHON

Q4) Explain Variables in Python with examples.

1. Definition
 A variable in Python is a name that refers to a memory location where data is stored.
 Variables are used to store, update, and retrieve values during program execution.
 In Python, variables are created automatically when a value is assigned (no need to declare
type explicitly).
Example:
x = 10
 Here, x is the variable (the box).
 10 is the value stored inside.
2. Features of Variables in Python
1. Python is dynamically typed – type is decided at runtime.
2. No need to declare variables before using them.
3. Variables can change type during execution.
4. Case-sensitive (age, Age, and AGE are different variables).
3. Rules for Naming Variables

 Can contain letters, digits, and underscores (_).


 Must start with a letter or underscore, not a digit.
 Cannot use Python keywords (if, while, class, etc.).
 Should have meaningful names for readability.
Valid Examples:

name = "Teja"
_age = 20
roll_no123 = 45

Invalid Examples:

123name (starts with digit)


my-name (contains special character -)
class (keyword in Python)

4. Data Types with Variables


Variables can store different types of values:
x = 10 # int (integer)
y = 10.5 # float (decimal number)
name = "Teja" # str (string)
is_student = True # bool (boolean)

5. Multiple Variables Assignment


• Python allows us to assign a value to multiple variables and multiple values to multiple
variables in a single statement which is also known as multiple assignments.

1. Assign single value to multiple variables:

x = y = z = 100
print(x, y, z) # Output: 100 100 100

2. Assign multiple values to multiple variables:

a, b, c = 1, 2, 3
print(a, b, c) # Output: 1 2 3

TEJA KOTAMRAJU
Page 1
PYTHON
6. Changing Value of Variables
Variables can change value anytime.
x=5
print(x) # 5

x = "Hello"
print(x) # Hello
👉 Python allows the same variable to hold different types of data (because it’s dynamically typed).

7. Type Checking
Use type() function to check variable type:
a = 10
print(type(a)) # <class 'int'>

b = 3.14
print(type(b)) # <class 'float'>

c = "Teja"
print(type(c)) # <class 'str'>

8. Constants (Fixed Values)


Python doesn’t have real constants, but by convention we use UPPERCASE names:
PI = 3.14159
print(PI)
GRAVITY = 9.8
print(gravity)
(Though Python still allows changing them, programmers agree not to.)

9. Deleting Variables
You can delete a variable using del:
x = 100
print(x) # 100

del x
print(x) # ❌ Error: x is not defined

10. Memory Concept


When you write x = 10, Python:
1. Creates a memory location for value 10.
2. x becomes a reference (name pointing to that value).
If you assign another variable:
y=x
Then y will also point to the same value.

TEJA KOTAMRAJU
Page 2

You might also like