A variable is a named memory location that efficiently stores data temporarily. which can be used and modified during a program’s execution.
Table of Contents
Characteristics of Variables in Python
- Dynamic Typing: Variables can store different types of data, and the type can change at runtime.
- No Declaration Required: No need to declare the type of a variable before assigning a value to it.
- Case-Sensitive: Variable names are case-sensitive (age & Age are different variables).
- Naming Rules:
- Must start with a letter or an underscore (_).
- Can contain letters, numbers, and underscores (e.g., my_variable_23).
- Cannot start with a number.
- Cannot use Python keywords as variable names (e.g., if, while).
Assigning Values to Variables
You can assign values to variables using the “ = “ operator.
# Assigning an integer
age = 25
# Assigning a floating-point number
height = 6.1
# Assigning a string
name = "Ravi"
# Assigning a boolean
is_student = True
Multiple Assignments
Python allows assigning a single value to multiple variables or multiple values to multiple variables in a line.
# Assigning the same value to multiple variables
x = y = z = 20
# Assigning multiple values to multiple variables
a, b, c = 10, 20, 30
Variable Reinitialization
Variables in Python can be reinitialized with a new value or even a new type at any point in the code. This is made possible due to Python’s dynamic typing.
# Initial value
count = 10
print(count) # Output: 10
# Reinitializing with a new value
count = 20
print(count) # Output: 20
# Reinitializing with a different type
count = "Twenty"
print(count) # Output: "Twenty"
Variable Types and Dynamic Typing
In Python, the type of a variable differs from the value assigned to it and allows the variables to change data type dynamically.
# Integer
num = 1
print(type(num)) # Output: <class 'int'>
# Changing the type to a string
num = "One"
print(type(num)) # Output: <class 'str'>
Basic Variable Operations
Variables can be used in various operations, including arithmetic and string concatenation.
# Arithmetic operations
x = 5
y = 10
sum = x + y # 15
# String concatenation (str+str)
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name # "John Doe"
Constants in Python
Python doesn’t have a built-in constant data type. However, by convention, variables intended to be constants are named using all uppercase letters.
PI = 3.14159
GRAVITY = 9.81
While these values can technically be changed, using all uppercase letters indicates that they should not be modified (as in real-time).
Conclusion
Variables are names for memory locations where data is stored, allowing us to change and manipulate this data in our program. In Python, variables are versatile and easy to use due to the language’s dynamic typing and simple syntax. Understanding how to use and reinitialize variables effectively is fundamental to writing efficient and readable Python code.
Knowledge Check
Related Article No.4
Check out our Trending Courses Demo Playlist
Data Analytics with Power Bi and Fabric |
Could Data Engineer |
Data Analytics With Power Bi Fabic |
AWS Data Engineering with Snowflake |
Azure Data Engineering |
Azure & Fabric for Power bi |
Full Stack Power Bi |
Kick Start Your Career With Our Data Job
Social Media channels
► KSR Datavizon Website :- https://www.datavizon.com
► KSR Datavizon LinkedIn :- https://www.linkedin.com/company/datavizon/
► KSR Datavizon You tube :- https://www.youtube.com/c/KSRDatavizon
► KSR Datavizon Twitter :- https://twitter.com/ksrdatavizon
► KSR Datavizon Instagram :- https://www.instagram.com/ksr_datavision
► KSR Datavizon Face book :- https://www.facebook.com/KSRConsultingServices
► KSR Datavizon Playstore :- https://play.google.com/store/apps/details?id=com.datavizon.courses&hl=en-IN
► KSR Datavizon Appstore :- https://apps.apple.com/in/app/ksr-datavizon/id1611034268
Most Commented