A variable in Python is a name given to a memory location that holds a value. These variables act as identifiers in the code, allowing programmers to reference and manipulate data.
• Type Inference: Python is an interpreted language that automatically determines the type of a variable when it's assigned. Therefore, you do not need to specify the variable type explicitly.
• Naming Conventions: Variables must start with a letter or an underscore (_). They can consist of letters, digits, and underscores. Variable names are case-sensitive, meaning myVar and myvar would be considered different variables.
Identifiers, like variables, are used to recognize and reference the various elements in a program, such as variables, functions, classes, etc.
1. Start with a Letter or Underscore: The first character of an identifier must be an alphabetic character or an underscore (_).
2. Use Letters, Digits, or Underscores: The remaining characters can include lowercase (a-z), uppercase (A-Z), digits (0-9), or underscores.
3. No Special Characters: Identifiers cannot contain special characters like !, @, #, %, etc.
4. No Whitespace: Identifiers cannot contain spaces.
5. Reserved Keywords: Identifiers should not be named after Python's reserved keywords.
6. Case Sensitivity: Python identifiers are case-sensitive. For example, myVar, MyVar, and MYVAR are three distinct identifiers.
• Valid: a123, _n, n_9
• Invalid: 1a, n%4, n 9
In Python, variables do not need to be declared before use. They are created and assigned a value using the = operator.
x = 5
y = "Hello"
z = 10.5
Python handles variables differently compared to many other programming languages. Variables in Python are references or pointers to objects stored in memory.
a = 50
b = a
Here, both a and b refer to the same integer object 50. If we reassign b to a new value, it points to a different object.
Every object in Python has a unique identifier, which can be retrieved using the id() function. This identifier is unique and constant for the object during its lifetime.
a = 50
b = a
print(id(a)) # Prints the unique id of object referred by a
print(id(b)) # Same as id(a)
a = 500
print(id(a)) # Different id since a now refers to a new object
Variable names can be of any length and can include uppercase and lowercase letters, digits, and underscores. However, the variable name should be descriptive to improve code readability.
• Camel Case: myVariableName
• Pascal Case: MyVariableName
• Snake Case: my_variable_name
Python allows assigning a single value to multiple variables or multiple values to multiple variables in a single statement.
x = y = z = 50
a, b, c = 5, 10, 15
Python supports two types of variables: Local and Global.
These are declared within a function and can only be accessed within that function.
def add():
a = 12 # Local variable
b = 21 # Local variable
print(a + b)
add()
33
These are declared outside of any function and can be accessed throughout the program.
x = 101 # Global variable
def my_function():
global x
print(x)
x = "Hello, Python!"
my_function()
print(x)
101
Hello, Python!
Variables can be deleted using the del keyword, which removes the reference to the object.
x = 6
del x
print(x) # Raises NameError: name 'x' is not defined
Unlike other programming languages, Python does not impose limits on the size of integers. Python uses the int type for all integer values, and the maximum size is constrained only by the system's memory.
a = 50000000000000000000000000000000000000000000
print(a)
Variables can be printed using the print() function.
# Printing a single variable
a = 5
print(a) # Outputs: 5
# Printing multiple variables
x, y = 10, 20
print(x, y) # Outputs: 10 20