Python Variables – What are the Variables in Python?

Learn Variables in Python

In this tutorial, you'll learn what variables are in Python, how to create and assign values to them.

What are the Variables in Python?

In Python, variables used to store information and to be referenced and manipulated in the computer program. Python is an object-oriented and dynamic-typed programming language. Unlike other programming languages such as Java or C#, you don't need to declare variables and their type before using them.

Creating a Variable

To create a variable, you assign a value and then start using it.

The following is an example of creating a variable named student and assigning John to it:

student = "John"

You assign a variable with a single equal sign (=).

Variable Names

In Python, a variable can have a short name or more descriptive name but can contain only alpha-numeric characters (A-Z, a-z, 0-9) and underscores (_). It must start with either an underscore or a letter. A variable name cannot begin with a number.

Here are some examples:

x = 100 #valid
_x = 100 #valid
0name = 'Jonh' #invalid
name0123 = 'James' #valid
&employee_name = "James" #valid

Variable names are case-sensitive. So, for example, studentSTUDENT, and Student are different variables. And one more restriction on variable names is that you cannot create variables that have the same names as the reserved keywords below.

ifelseelifcontinuebreak
forwhileinandor
exceptwithasyieldfinally
notglobalpassclassfrom
lampdaTrueFalseassertraise
returntry

Here are some examples:

return = 100 #invalid
in = "James" #invalid
as = "Jonh" #invalid

In this tutorial, you've learned what variables are in Python, and how to create and assign values to them. Unlike other static typed programming languages such as C# or Java, you don't need to declare variables and their types before using them. Python creates variables the moment you first assign values to them. Variable names must start with either an underscore (_) or a letter and cannot begin with a number.


See also:
Python return Statement with Examples
Python List Methods and Functions with Examples
Python abs() – Absolute Value in Python
Python Factorial – Find the Factorial of a Number
Python min() – Find the Smallest Value in Python

Leave a Comment