Python — Variables
Variables are one of the most fundamental concepts in Python and in any programming language. In Python, variables are used to store data values, which can be retrieved and manipulated later in the code.
Unlike other programming languages, Python doesn’t require declaring the variable type explicitly, making it dynamically typed.
This means the type of the variable is determined automatically based on the value assigned to it.
What is Variable?
A variable is essentially a name that refers to a location in memory where a value is stored.
Get Bollavaram Harshavardhan Reddy’s stories in your inbox
Join Medium for free to get updates from this writer.
You can think of a variable as a container that holds data, and that data can be of different types, such as integers, floats, strings, lists, etc.
x = 10 #'x' is a variable that holds the value 10(Integer)Since python is dynamically typed programming language changing the value of variable also changes the data type
x=10 #Here it is Integer
x-'Hello' #Here it is StringNaming Conventions:
Variable names must start with a letter or an underscore (_).
Subsequent characters can include letters, numbers, or underscores.
Variable names are case-sensitive, meaning
Varandvarare two different variables.Reserved keywords cannot be used as variable names. For example, words like
class,def,if,elseare reserved by Python for specific functions in the language.
In Python we can assign multiple variables with values in one line
a,b,c = 10,20,30Scope of Variables
Variables in Python have different scopes. Variables declared inside a function are local, while variables declared outside any function are global.
x = "global" # Global variable
def my_func():
x = "local" # Local variable
print(x)
my_func() # Output: local
print(x) # Output: globalThank you for reading , Like my content for more stories





