In Python, Variables are used to store data values. variables can be referenced and manipulated throughout a program.
A declared variable is the name specified to a memory location.
A value-holding declared variable is also known as an identifier.
Python doesn't require explicit type declarations of variables and can hold various data types like integers, floats, strings, and booleans.
Python is dynamically typed, meaning we don't need to declare the variable type explicitly. Instead, The type of a variable is determined dynamically based on the value assigned to it.
Here are some key points about variables in Python:
Variable names can consist of letters (both lowercase and uppercase), digits, and underscores. However, they cannot start with a digit.
Variable names must start with a letter or an underscore.
Variables can not be declared with special characters (@, %, `, !, #, $, ^, & etc.) or whitespace is not allowed in the variable name.
Python is case-sensitive, so "yourVariableName", "YourVariableName", and "yourvariablename" would be considered different variables.
Avoid using reserved keywords as variable names (e.g, for, True, False, if, else, while, None, etc.).
Variables names can be constructed using following patterns:
Camel Case - Each word in the middle will start with a capital letter. Whitespace is not allowed while variable declaration. For example - userInfo, employeeDetails, etc.
Pascal Case - It is the same as the Camel Case, but here the first word is also capital. for ex - EmployeeDetails.
Snake Case - In the snake case, Words are separated by the underscore. for ex - employee_details, Employee_Details etc.
Variables are assigned values using the assignment operator `=`.
xyz = 123 username = "Alice Collin" isStudent = True
Python variables can hold values of different types, such as integers, floats, strings, lists, tuples, dictionaries, etc.
Python assigns the type dynamically based on the value assigned to it.
age = 35 # integer height = 6.2 # float username = "Alice Collin" # string isStudent = True # boolean
xy, yz, zx = 10, 20, 30
We can assign multiple variables in a single line using multiple assignments or unpacking.
xyz = "Alice" xyz = 123
We can reassign a variable to a different value of any type at any time.
PI = 3.14
Python doesn't have built-in constants as other programming languages have.
age = 33 print(age) # Output: 33 del age print(age) # Error: NameError: name 'age' is not defined
Variables named in all capital letters (e.g., PI = 3.14) are conventionally treated as constants and should not be reassigned.
def get_age(): age = 35 print(age) get_age() # Output: 35
We can delete a variable using the "del" keyword.
age = 35 def get_age(): print(age) get_age() # Output: 35
Variables in Python have function scope, meaning their scope is limited to the block of code where they are defined.
Local Variables: Defined within a function and can only be accessed within that function.
However, variables declared outside of any function have global scope and can be accessed from anywhere in the code.
Global Variables: Defined outside of any function and can be accessed throughout the program.
Python variables provide flexibility and ease of use, allowing developers to work with different types of data efficiently.
With Python's dynamic typing and flexible variable naming conventions, we can easily modify, manage, and create variables.