Example HTML page

Variables

Variables

The concept of variable is similar to as in other programming languages. A variable is a name which can be used as a reference to a value. A variable can be treated as some memory location where it can store some data or any value assigned to it. When a value is assigned to a variable it stores in some memory location.

Let’s have a look on variables in brief:

Declaration of variables in different formats

>>> x=16                                                  # integer variable

>>> y=13.26                                             #float variable

>>> z=”Flipkart”                                       # string variable

To print the above examples use print( )

>>> print(x)

16

>>> print(y)

16.26

>>> print(z)

Flipkart

You can also check the type of the variable as shown in below.

>>> type(x)

<class ‘int’>

>>> type(z)

<class ‘str’>

We can also re-declare the variable even after once it is already declared. It is shown in below:

>>> x=4

>>> print(x)

4

>>> x=”my_variable”

>>> print(x)

my_variable

Note: After re-declaring a variable, it will print the recently assigned value.

You can also delete a variable by using ‘del’ command.

>>> var=123

>>> var

123

>>> del var

>>> var

Traceback (most recent call last):

File “<pyshell#10>”, line 1, in <module>

var

NameError: name ‘var’ is not defined

We can increase the value of specific variable and it shown in below

>>> a=1

>>> b=a+2

>>> b

3

 

>>> print(b)

3

Note: The variable type changes every time during execution. It is shown in the following example.

>>> k=26                                                    #int variable

>>> k=26+1.4                                             #addition of int and float but resultant is float

>>> k

27.4

>>> k=”Sixty”                                            #and now it is string

>>> k

‘Sixty’

 If you want to integrate both the text and variable, just look at the below example

>>> var=”interesting”

>>> print(“Python is”+var)

Python is interesting

If you want to integrate a variable with the other variable then:

>>> s=”Hello”                                               #in case of strings

>>> t=”Sudhakar”

>>> u=s+t

>>> print(u)

HelloSudhakar

 

>>> print(s+t)

HelloSudhakar

>>> i=3                                                          #in case of integers

>>> j=5

>>> print(i+j)

8

id( )

As we discussed earlier the variable has some memory location. So to find the location of specific variable we use identity function (id( )).

Ex:

>>> a=13

>>> id(a)

1492447408                                      #Memory location of variable ‘a’

>>> a=b

>>> id(a),id(b)

(1492447088, 1492447088)

>>> >>> b=14

>>> id(a),id(b)

(1492447088, 1492447440)

Multiple assignment of variables

We can assign a value to the multi variables at a time.

Ex:

p=q=r=6

a,b,c=2,1.11,”One”

Naming Conventions:

Do’s and Don’ts of variables

Do’s

Don’ts

1.Must start with a letter or an underscore (‘_’).

Ex: _python or python_.

2.It must include characters from (a-z/A-Z/0-9) and ‘_’.

Ex: abc12, 5A2, pr_ogra_mmin_g.

3.Variables are case-sensitive.

Ex: Snow,snow,SNOW are considered as three different variables.

4. An identifier consists of letters and digits.

5. In case of identifiers special ‘_'(underscore) can be used.

6. Examples of valid declaration of identifiers in Python are : BeHappy,mychoice,Twoapples, The_Movie

 

 

 

 

1. Do not start with number.

Ex: 6=a.

2. Avoid the usage of both lowercase and uppercase of letter ‘i’ and uppercase letter ‘o’. because ‘i’ and ‘o’ look like 1 and 0.

3. A variable name cannot match with the existing reserve words/keywords.

Ex:as=9.

4. An identifier cannot begin with ‘_’ and a digit.

5.  Spaces and quotes are not considered to be part of an identifier.

6. Examples of invalid declaration of identifiers in Python:

a.’BeHappy’ — quotes are ignored

b. my choice — spaces are ignored

c.  2apples — we cannot start with a digit.

d. _TheMovie — we cannot start with ‘_’.

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest