(toc)

What is a Variable?

In Python, a variable is a symbolic name for a value. You can think of it as a label that points to some data stored in memory. When you create a variable, you assign a value to it, and you can change that value during the program’s execution.

Declaring a Variable in Python

Python is known for its simplicity, and defining a variable is no different. In most programming languages, you have to specify the data type of a variable (like int, string, etc.), but in Python, this is not necessary. Python automatically determines the type based on the value assigned to the variable.


Example:


# Declaring variables name = "Alice" age = 25 height = 5.5 print(name) # Output: Alice print(age) # Output: 25 print(height) # Output: 5.5

In this example:

  • The variable name holds a string ("Alice").
  • The variable age holds an integer (25).
  • The variable height holds a floating-point number (5.5).

Notice that we didn’t specify the data types for the variables—Python automatically knows what type each value is.

Variable Naming Rules

When naming variables in Python, there are a few rules to follow:

  1. Variable names can only contain letters (a-z, A-Z), numbers (0-9), and underscores (_).
  2. They cannot start with a number (e.g., 1name is not allowed).
  3. Variable names are case-sensitive. For example, age and Age are different variables.
  4. You cannot use Python keywords (like if, while, def) as variable names.


Example of Invalid Variable Names:


# Invalid variable names 1name = "John" # Starts with a number if = "test" # Cannot use a Python keyword

Assigning Values to Variables

In Python, you can assign values to variables using the assignment operator (=). The value on the right side of the operator is assigned to the variable on the left side.


Example:


x = 10 y = 5 # Adding variables sum = x + y print(sum) # Output: 15

In this case, the values 10 and 5 are assigned to the variables x and y, and then the sum of x and y is stored in the variable sum.

Changing the Value of a Variable

Python allows you to change the value of a variable at any time. When you assign a new value to an existing variable, the old value is replaced.


Example:


score = 50 # Initial value print(score) # Output: 50 score = 75 # Changing the value print(score) # Output: 75

Here, the variable score was initially set to 50, but we later changed it to 75.

Variable Types in Python

As mentioned earlier, Python automatically determines the type of a variable based on the value you assign. Some common types include:

  1. Integers (int): Whole numbers, both positive and negative.
    • Example: x = 5
  2. Floating-point numbers (float): Numbers with decimals.
    • Example: height = 5.9
  3. Strings (str): Text data, enclosed in quotation marks.
    • Example: name = "Bob"
  4. Booleans (bool): True or False values.
    • Example: is_active = True

You can check the type of a variable using the type() function:


Example:


x = 10 y = 3.14 z = "Hello, World!" print(type(x)) # Output: <class 'int'> print(type(y)) # Output: <class 'float'> print(type(z)) # Output: <class 'str'>

Multiple Assignments

Python allows you to assign values to multiple variables in a single line:


Example:


a, b, c = 10, 20, 30 print(a) # Output: 10 print(b) # Output: 20 print(c) # Output: 30

In this example, the values 10, 20, and 30 are assigned to a, b, and c, respectively.

Constants in Python

Python doesn’t have built-in support for constants (variables whose values shouldn’t change), but by convention, we use uppercase names to indicate that a variable should remain constant.


Example:


PI = 3.14159 # Convention for constant

Although you can still change the value of PI in Python, the uppercase naming convention signals to other developers that this value should remain unchanged.

Key Points to Remember:

  • Variable names are case-sensitive.
  • You can reassign values to a variable.
  • Python is dynamically typed, so you don’t need to declare the data type explicitly.

Leave a Reply