Variables in Python

Introduction

In computer programming, variables are used to store information that can be used throughout the program. They act like little boxes that hold data, and you can give them names that reflect what kind of data they store to make your code easier to understand. In this article, we will explore the concept of variables in Python and how they work.

In Python, we can declare a variable and assign a value to it using the ‘=‘ operator. For example, we can create a variable called “age” and assign a value of 25 to it:

age = 25

In this case, “age” is the variable’s name, and 25 is the value stored in the variable.

Variable Naming Rules in Python

In Python, there are some rules that we need to follow while naming variables. These rules include:

  • A variable name must start with a letter or an underscore (_).
  • A variable name can only contain letters, numbers, and underscores.
  • Variable names are case-sensitive, which means “age” and “Age” are two different variables.
  • Variable names should be descriptive and meaningful.

Examples of valid variable names are “age”, “_name”, “my_variable”, “x”, etc.

Here are more examples of valid and invalid variable names:

# Valid variable names
first_name = "John"
last_name = "Doe"
age = 30
is_student = True

# Invalid variable names
123_name = "John" # Variable name cannot start with a number
last-name = "Doe" # Variable name cannot contain hyphens
is_student? = True # Variable name cannot contain special characters

Assigning Values to Variables

As mentioned earlier, we can assign a value to a variable using the ‘=‘ operator. For example, we can assign a value of 10 to a variable called “x” as follows:

x = 10

We can also assign values to multiple variables in a single line of code. For example:

x, y, z = 10, 20, 30

In this case, we have assigned 10, 20, and 30 values to the variables x, y, and z, respectively.

Data Types in Python

Python has several built-in data types, including integers, floating-point numbers, strings, lists, tuples, sets, and dictionaries. When we assign a value to a variable, Python automatically assigns a data type to the variable based on the assigned value.

For example, if we assign an integer value to a variable, Python will assign the integer data type to the variable:

x = 10
print(type(x))

Output: <class ‘int’>

Similarly, if we assign a string value to a variable, Python will assign the string data type to the variable:

name = "Subhankar"
print(type(name))

Output: <class ‘str’>

Variable Scope

In Python, the scope of a variable is determined by where the variable is defined. There are two types of variable scopes in Python:

  • Global scope
  • Local scope

A global variable is a variable that is defined outside of a function, and it can be accessed from any part of the program. A local variable is a variable that is defined inside a function, and it can only be accessed within that function.

For example, let’s say we define a global variable called “count” outside of a function:

count = 0

def increment():
    count = count + 1
    print(count)

In this case, if we call the “increment” function, we will get an error because the variable “count” is not accessible within the function. To access the global variable “count” within the function, we need to use the “global” keyword as follows:

count = 0

def increment():
    global count
    count = count + 1
    print(count)

In this case, the “global” keyword tells Python to use the global variable “count” instead of creating a new local variable.

Exercises

Try some exercises for practicing Python variables.

  1. Create a variable called “name” and assign your name to it. Then, print the value of the variable to the console.
  2. Create two integer variables, “x” and “y”, and assign them the values 5 and 7 respectively. Calculate the sum of x and y and store it in a variable called “z”. Finally, print the value of “z” to the console.
  3. Create a string variable called “sentence” and assign it the value “The quick brown fox jumps over the lazy dog”. Use string manipulation to replace the word “fox” with “cat”, and print the modified sentence to the console.
  4. Create a boolean variable called “is_raining” and assign it the value True.
Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! I’m Subhankar Rakshit, the brains behind PySeek. I’m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles: 194