Python Variables

Python Variables

In this tutorial, you will learn about variables in Python language.

Variables are used to store and manage data. Variables store values that can be used, manipulated, and referenced throughout a program.

Creating and Assigning Variables

Python is a dynamically typed programming language, which means, there is no need to specify the datatype when you are creating a variable. Also, there is no special keyword to create or define a variable.

Just assign the value to the variable.

The syntax of creating and assigning a variable with a value is given below.

variable_name = value

Now, let us go through some examples, where we create and assign variables with different types of values.

message = "Hello World"
quantity = 20
pi = 3.14

In the above code,

  • message is the variable name, and this variable is assigned with a string value of "Hello World".
  • quantity is the variable name, and this variable is assigned with an integer value of 20.
  • pi is the variable name, and this variable is assigned with a floating point value of 3.14.

Reassigning Variables

Once a variable is initialized with a value, you can reassign the variable with a new value of the same type or another type.

For example, consider the following statement, where we have initialized a variable x with a string value of "Hello World".

x = "Hello World"

We can reassign a new string value to this variable, as shown in the following code.

x = "Hello World"
x = "Good Morning"

Now, variable x holds the value of "Good Morning".

We can also assign a value of another type to this variable x. Let us assign a boolean value.

x = "Hello World"
x = "Good Morning"
x = True

If you print the value in x using a print statement, after reassigning the variable x, the updated value in the variable x is printed to the output.

Python Program

x = "Hello World"
x = "Good Morning"
x = True
print(f"x is {x}")
Run Code Copy

Output

x is True

Using Variables in Expressions

We can use variables in an expression.

For example, consider the following program, where we take integer values in two variables, and find their sum. To find their sum, we use the variables with Addition operator.

Python Program

a = 10
b = 20
c = a + b
print(f"c is {c}")
Run Code Copy

Output

c is 30

a and b are variables assigned with integer values. Then we created a variable c and assigned it with an expression a + b. We have used the variables a and b in an expression.

Rules for naming a variable

You have to follow the below rules while naming a variable.

1. Valid characters

Variable names can consist of letters (both uppercase and lowercase), numbers, and underscores (_). They must start with a letter or an underscore.

2. No spaces or special characters

Variable names cannot contain spaces or special characters like !, @, #, $, %, etc.

3. Case sensitivity

Python is case-sensitive, so variable names like “my_variable” and “My_variable” are considered different.

4. Avoid reserved words

Do not use Python reserved words (also known as keywords) as variable names. Examples of reserved words include if, else, while, for, def, class, etc.

5. Descriptive and readable

Use descriptive variable names that convey the purpose of the variable. Avoid single-letter variable names (except for counters in loops). Make your variable names readable and meaningful.

6. Snake case

For multi-word variable names, use snake_case, which involves lowercase letters separated by underscores. For example, user_name, total_amount.

7. Leading underscore (Conventions)

A variable name starting with a single underscore (_variable) is a convention to indicate that the variable is meant to be private or should not be accessed directly from outside the class/module.

8. Double underscore (Name Mangling)

A variable name starting with two underscores (__variable) undergoes name mangling. This is used to make a variable more unique in the context of class inheritance.

Examples for valid variable names.

  • name
  • age
  • _name
  • first_name
  • quantity

Examples for invalid variable names:

  • @name variable name cannot contain special characters
  • 4as variable name cannot start with numeric values
  • first name variable name cannot contain spaces

Printing value in variable

We have already seen in the previous examples, that we used print() builtin function to print the value in given variable.

Now, let us take a value in a variable, and print the value in variable using print() statement.

Python Program

a = "Hello World"
print(a)
Run Code Copy

Output

Hello World

Converting value in variable

We can convert the value in a variable to desired datatype using builtin functions like str(), int(), float(), etc.

For example, consider that we have take a float value of 236.52142 in variable x. We can convert the value in variable x to an integer value using int() function.

Python Program

x = 236.52142
x = int(x)
print(x)
Run Code Copy

Output

236

Get type of value in variable

We can get the type of value in a variable using type() builtin function. Pass the variable as argument to the type() builtin function, and the function returns a string representing the type of the value in given variable.

For example, consider that we have take a float value of 236.52142 in variable x. Let us get the type of value in variable x.

Python Program

x = 236.52142
print(type(x))
Run Code Copy

Output

<class 'float'>

Summary

Python variables are used to store and manage data values. They play a crucial role in programming by allowing data to be referenced, manipulated, and reused throughout a program. Understanding how to create, assign, and reassign variables is fundamental to writing effective Python code.

Related Tutorials

Code copied to clipboard successfully 👍