Python – Variables

Variables:

  • Identity given to memory location
  • Variables store program data.
  • Variable value can be modified.

Note: In Python, no need to specify the data type in variable declaration

  • Variables are created by assigning a value to a name.
    • x = 5
  • Variable names in Python can contain letters, numbers, and underscores, but cannot begin with a number.
    • my_var = 10
    • var_2 = “Hello, world!”
  • Python is case-sensitive, so my_var and My_Var are considered different variables.
  • Variable names should be descriptive and not too long.
    • For example, total_sales is better than ts.
  • Variables can be reassigned to new values:
    • x = 5
    • x = 10
  • Variables can also be used in expressions:
    • x = 5
    • y = x + 3
  • Python supports multiple assignment:
    • x, y, z = 1, 2, 3
  • Underscores can be used to separate large numbers for readability:
    • num = 1_000_000
  • Python has several reserved keywords that cannot be used as variable names, such as if, else, while, and for.
Scroll to Top