A variable is a name that refers to a value stored in the computer's memory. Think of it as a label or a box holding data.
Use the assignment operator (=) to assign a value to a variable.
variable_name = value
variable_name: The name you choose for your variable. Variable names must start with a letter or an underscore (_), followed by letters, numbers, or underscores. They are case-sensitive (my_var is different from My_Var).=: The assignment operator.value: The data you want to store in the variable (e.g., a number, a string, a list).Examples:
age = 30 # Assigning an integer
name = "Alice" # Assigning a string
is_student = True # Assigning a boolean
prices = [10, 20, 30] # Assigning a list
Python is dynamically typed, meaning you don't need to declare the variable type explicitly. The type is determined by the value assigned.
You can check the type of a variable using the type() function:
my_variable = 123
print(type(my_variable)) # Output: <class 'int'>
another_variable = "hello"
print(type(another_variable)) # Output: <class 'str'>
You can change the value a variable refers to at any time.
count = 5
print(count) # Output: 5
count = 10 # Reassigning the variable
print(count) # Output: 10