Loading, please wait...

Python - Variable

A variable must have a short name (like a and b) or a more descriptive name (age, carname, total_volume).

 

Rules for Python variables:

·         The variable name only start with a letter or the underscore character.

·         The variable name cannot start with a number.

·         The variable name must only contain alpha-numeric characters and underscores.

·         Variable names are case-sensitive (name, Name and NAME are three different variables).

 

Variables are case sensitive.

Unlike other programming languages Python has no command for declaring a variable.

Creating Variables

A variable is created when you first assign a value to it.

Example:

x = 5 # x is the type of int
x =
"Tripti" # x is the type of str
print
(x)

Output Variables

The Python print statement is use to output variables.

You can also use the + character to add a variable to another variable and to combine both text and a variable.

Example:

x = "My name is "
y =
"sana"
z = a + b
print(z)