by Dinesh Thakur

Variable is a reserved memory storage location that has an associated symbolic name (called "identifier"), which contains some value (can be literal or other data) that can change. You do not need to declare variables before you use them, or declare its type. An identifier is a name used to identify a variable, function, class, module or other object. Literal is a notation for constant values of some built-in type. Literal can be string, plain integer, long integer, floating point number, imaginary number. All variables in Python are an object. So i create a variable age and name and set that equal to any thing i want. For example

age = 12
name = 'kamal'
 
Here we are creates variables who name is age and name are identifiers and who value is 12 and kamal are integer and string are literals, respectively. Now we ever in the python program I call one age, I communicate the value has 12. The main thing about the variable is you can always change the value. Remember variables are not permanent. The variables are changeable.

Variable Name in Python

There are some rules that need to be followed for valid identifier naming:

  1. The first character of the identifier must be a letter of the alphabet (uppercase or lowercase) or an underscore('_').
  2. The rest of the identifier name can consist of letters (uppercase or lowercase character), underscores('_') or digits (0-9).
  3. Identifier names are case-sensitive. For example, myname and myName are not the same. Identifiers can be of unlimited length.

A few examples of valid and invalid names in python in the table.

 

                             Example of valid and invalid names for variables

 

Name

Valid

Comment

A1

yes

Although it containsa number,the nameA1startswithletter

speed

yes

Name consisting of letters.

Speed90

yes

Name consisting of letters and numbers, but initiated by letters.

First_Name

yes

The symbols underscore (_) and allowed and facilitates reading of big names.

First Name

no

Variable names can not contain blank spaces.

_b

yes

The underscore (_) is accepted in variable names, even at the beginning.

1a

no

Variable names can not begin with numbers.

 

Version 3 of Python allows the use of accents in variable names because, by default, programs are interpreted using a character set called UTF-8, capable of representing almost all the letters of the known alphabets.

Multiple Assignment

In Multiple Assignment you can declare multiple values to multiple variables in one line. The way work is

p1,p2,p3="aman","kamal","sachin"

In some situation you want to assign same value to multiple variable then you can assign it as

p1= p2 = p3 = "Apple"

Data Type in python

Variables have other properties in addition to name and content. One, known as type and defines the nature of the data that the variable stores. Python has several data types, but the most common are integers, floating point numbers and strings. Besides being able to store numbers and letters, the variables in Python also store values as true or false.We say that these variables are the logical type.

Python numerical variables

We say that a variable is numeric when stores integer or floating-point numbers.

The integers are numbers without decimal part as 1.0, -5.550, -47.30000 or decimal floating-point numbers are numbers with decimal part as 1.0,5.478, 10.478,30000.4. 1.0 Note that even with zero in the decimal part is a floating-point number.

Python and in most programming languages use the point and not the comma as the separator between the whole and fractional part of a number. This is also a legacy of English. Also note that we do not use anything as thousands separator Example: 1.000,000 (one million) is written 1000000.

Python supports two types of numbers: integers and floating point. (Also supports complex numbers).

Full: int (% d)

Assigning an integer value to an integer variable (% d):

myInt = 12
# Printing variable
print ("My Whole Number is")
# Printing variable with personalized message
print ("whole variable is:% d"% myInt)

Where the operator: % d is replaced by the value that myInt variable is stored.

Python Float variables

Assigning a decimal value to a floating point variable of type (% f):

MyFloatVal = 1.0
#Printing variable
print (MyFloatVal)
# Printing variable with personalized message
print ("Float Value is % f" % MyFloatVal)

In the previous example, as you can see. Where is the mask %f operator is replaced by the variable content that myFloatVal, it has stored.

You can also assign integer content on a variable floating point NOT the reverse happens.

MyFloatVal = 7.5 # Assigning a value to decimal
MyFloatVal = float (7) #Making parse the integer value for the type float

Python Logic Variables

Often want to store a simple content: true or false in a variable. In this case, we will use a type of variable called logical or Boolean type. In Python, we will write True or False and true or false. Note that the T and F are written with capital letters.

Example of Logic Variables

Result = true
Approval = false