Python Variables, Identifiers, declaring variables and assigning values

Python Variables, Identifiers, declaring variables and assigning values

Variable is a memory location in every programming language that is represented by a name. Variables are identifiers that are used to hold values.

Every variable have there own type which is called data type like (int, string, float, etc) but In Python programming language, there is no need to specify the type of variable because Python programming language is smart enough to get variable type by itself.

Every variable must have a name that contains both letters and digits or only letters, but the variable name must begin with a letter or an underscore.

Lowercase letters recommended using for a variable name. Student and student both are two different variables with the different variable names. The Student is started with capital and the student is started with small letter s.

Identifier Naming

Variables are the example of identifiers. An Identifier is used to identify the literals used in the program. Rules are given below to name an identifier.

o The identifier name must start with a letter or underscore ( _ ).

o All the characters except the first character may be an alphabet of lower-case(a-z), upper-case (A-Z), underscore or digit (0-9).

o White-spaces and special characters are not allowed in an identifier name, special characters are (!, @, #, %, ^, &, *).

o The identifier name must not be similar to any keyword defined in the language.

o Names of identifiers are case sensitive i.e myname, and MyName is not the same identifiers.

o Valid identifiers Examples are : name123, _name, name_9, etc.

o Invalid identifiers Examples are: 1name, name%4, name 9, etc.

Declaring Variable and Assigning Values

Declaring a variable in python before using it is not mandatory in any application. Python allows to create and declare variables at the time of use.

We don't need to declare explicitly variable in Python. Assignment of value to any variable will declare that variable at that time of data type of the assigned value.

Example: a = 3 (Here a have integer data type)

  b = "HI" (b have string data type)

The equal (=) operator is used to assign value to a variable.

Multiple Assignment

Multiple assignments allows in python which enables developers to assign values to multiple variables in one statement so developers can do this job in just one line code.

We can assign values to multiple variables in two ways. One is assigned a single value to all variables example is

1. x=y=z=50  

2. print x  

3. print y  

4. print z  

Output:

1. >>>   

2. 50  

3. 50  

4. 50  

5. >>>  

2.Assigning multiple values to multiple variables:

Eg:

1. a,b,c=5,10,15  

2. print a  

3. print b  

4. print c  

Output:

1. >>>   

2. 5  

3. 10  

4. 15  

5. >>>  

Note : Order must be followed when assigning values to the variables.

 

Enjoyed this article? Stay informed by joining our newsletter!

Comments

You must be logged in to post a comment.

Related Articles
About Author