variables
A variable in Python is a name that refers to a memory location where data is stored. Variables allow you to store, modify, and access data during the execution of a program. Unlike some other programming languages, Python variables are dynamically typed, meaning you do not need to explicitly declare their data type.
Syntax:
variable_name = value
- variable_name: A valid identifier that follows Python's naming conventions.
- value: The data assigned to the variable.
Example:
name = "Nihar"
age = 25
height = 6.0
is_student = True
In this example:
nameis assigned a string ("Nihar").ageis assigned an integer (25).heightis assigned a float (6.0).is_studentis assigned a boolean value (True).
Variable Naming Rules
- Case-sensitive: Variable names are case-sensitive.
ageandAgeare two different variables. - Must start with a letter or underscore: Variables can start with a letter (a-z, A-Z) or an underscore (
_), but not a number. - Cannot use reserved keywords: Python keywords like
class,if,while, etc., cannot be used as variable names. - No spaces allowed: Use underscores to separate words (
my_variable).
Example:
first_name = "Nihar" # Valid
1st_name = "Nihar" # Invalid (cannot start with a number)