Python Variable

Python Variable:

Think of any number is a variable in Python. Let’s store it for later. When you think of that number, you are holding that value in your head.




It means that capable of being varied or changed.

A variable is a memory location where a programmer can store a value.

Example: Emp no, Emp name etc.

Value is either a string or numeric etc

Example: “Vijay”, 2456

Variables are created when first assigned.

The interpreter allocates memory on the basis of the data type of variable.

The type(string, int, float etc) of the variable is determined by Python.

Simple Rules for Python Variables:

1. Must begin with a letter (a-z, A-Z) or (_)

Examples:

>>>@@@EmpNumber =10134

SyntaxError: invalid syntax

>>>_EmpNumber =10134

>>>print(_EmpNumber)

10134

2.Must not contain any special characters like ! , @,#,$ etc.

Examples:

>>>Vijay@=48

SyntaxError: invalid syntax

>>>@@@=0

SyntaxError: invalid syntax

3.Case sensitive

Examples:

>>> product_name = “Phone”

>>>print(Product_name)

Traceback(most recent call last):

File “<pyshell#26>” line 1 in <module>

print(Product_name)

NameError: name ‘Product_name’ is not defined

>>>print(PRODUCT_NAME)

Traceback(most recent call last):

File “<pyshell#27>” line 1 in <module>

print(PRODUCT_NAME)

NameError: name ‘PRODUCT_NAME’ is not defined

4. There are some reserved keywords which you cannot use as a variable name because Python uses them for other things.

Examples:

>>>for = 100

SyntaxError: invalid syntax

Good Variable Name :

I) Choose a meaningful name instead of short names.

II) Maintain the length of a variable name

III) Begin a variable name with an underscore(_) character for a special case.

Multi Assignment:

a = 100

print (a)

Name = ‘Vijay’

Age = 27

a=b=c=1

print(a)

print(b)

print(c)

Swaping variable

Syntax:

var 1, var 2= var 2, var 1

>>>x=100

>>>y=200

>>>print(x)

100

>>>x,y=y,x

>>>print(x)

20

>>>print(y)

30

Input Function:

Examples:

>>>a=input()

100

>>>print(a)

100

>>>age = input(“Enter your age\n”)

Enter your age

27

>>>print(age)

27





Summary: Python Variables are very useful for Python learners and simple to learn for developers. In all programming languages, variables are almost the same the major difference is syntax only. Python is simple to learn for developers also. It is the easiest programming language.