1. Home
  2. Docs
  3. Python 101 – Python Programming for Beginners
  4. Variables

Variables

What is a variable ?

 
Variable is an identifier which can hold a value that may change.

 

Variables in Python.

In python a variable is a reference to a computer memory, where the value is stored.

Unlike in other languages, in Python variable is just a label or reference.

Variable in Python, doesn’t hold any value.

age = 17

print(age)

here is age is a variable ,

which points a memory location where 17 is stored in memory.

Its value may change, during the course of time.

age = 21

print(age)

 

In Python a variable can hold different types of values.

A variable can hold a string, a number, or various objects like a function or a class.

 

Some points to know about variables in Python.

#1. One variable can have different values during course of program.

#2. Each value that a variable is holding has a associated datatype.

Like number, list, tuple, list, string, set etc. See data types section for more details.

#3. Python is completely object oriented, and everything in python is an object

That means variables, datatypes  are also objects.

#4. Python is dynamically typed and strongly typed programming language.

Unlike in C , C++ or Java, there is no need to declare the data type for variable.

#5. Python dynamically determines the type of data assigned to the variable.

 

In below example we assign values to a variable by using a symbol “=”,

which is assignment operator, more operators will be covered in later section.

Let’s look at some examples

Try assigning different values to same variable “price”

price = 10             #price is a variable and it holds an integer value

price = 10.50        # price a variable and it holds a float value.

price = “Alex”       # price a variable and it hods a string value

price = 10

print(price)

output:
    
10

price = 10.5

print(price)

output:
    
10.5



price = "Alex"

print(Alex)

output:
    
Alex

 

The above example shows that Python is dynamically typed.

Variable price can hold different types values, number or strings.

Python internally keeps track of datatype of the variable.

Remember you did your first program as Hello, World!,

Now let’s  extended that program little further.

We make use of variables and greet people.

 

user = "Alex" # user a variable holds a string value "Alex'

print("Hello ", user)

 

How variables are named ?

Naming rules for variables.

1.Variables names must start with a letter or an underscore

a, x, _a, _x  are valid variable names

2. From second letter onward may contain letters, numbers or underscores

a, x, _a, _x , __a, __x , name, _name, __name, password, userName, user_name, password9

are all valid variable names.

3. Names are case sensitive

name , Name, nAme, naMe, namE are all different variables.

upper_case, UPPER_CASE, Upper_Case are different variables.

 

Examples of variable names.

#1
>> name = "Alex"

>>> name

'Alex'

#2
>>> _name = "Alex"

>>> _name

'Alex'

#3
>>> __name = "Mark"

>>> __name

'Mark'

#4
>>> name_ = "Julie"

>>> name_

'Julie'

#5
>>> name__ = "Anne"

>>> name__

'Anne'

#6
>>> pass_word = "as8#99A"

>>> pass_word

'as8#99A'

#7
>>> name9

'John'

#8

>>> last_name = "Shepherd"

>>> last_name

'Shepherd'

>>> __name__ = "Alex"

>>> __name__ #avoid these kinds of variables

'Alex'

>>>

Invalid variable names.

8user_name  # variable name starting with number

username#     #variable name ending with special character '#'

username)      # variable name ending with special character ')'

$username     # variable name starting with special character '$'

%username     # variable name starting with special character '%'

 

 

Are there any naming conventions ?

Yes, apart form naming rules, some best practices are fallowed.

Below are some naming conventions to start with.

Naming conventions.

1.Python is known for readability. the variable names to be self descriptive.

2. You can use descriptive nouns, verbs with UnderScore, or camelCase style.

Exmples:

UnderScore style.

user_name, pass_word, database_name  or db_name,

camelCase style.

userName, passWord, dataBase, dbName etc..

more popular is the underscore style.

3. Avoid using the lowercase letter ‘l’, uppercase ‘O’, and uppercase ‘I’.  As “l” and “I” looks like digit 1 and “O” looks like digit zero.

4. Avoid using double underscore variable such as __name__ these type variables are internally used by Python.

Coding guidelines are detailed in PEP 8 — Style Guide for Python Code 

There are tools or modules that format your code to conform with pep 8, like autopep8

PEP stands for Python Enhancement Proposal.

Here you can find list of PEPs

reading documentation is a vital part of python learning.

You can start with PEP 8, 20, 257, 3099.

 

What is flexible variable assignment ?

Flexible Variable assignment.

Python allows to assign multiple variable assignment in a single line.

Examples:

Assigning same value to variables

a = b = c = 20                            # a,b,c are variables assigned value 20

This is called chain loader assignment, where all variables refer to the same object.

Try this on Python shell

>>> a = b = c = 20

>>> a

20

>>> b

20

>>> c

20

>>>

Assigning different values to variables

x,y = 45, 67

>>> x,y = 45, 67

>>> x

45

>>> y

67

>>>

This is called parallel assignment.

Assigning different values to variables

t1 = 10,11,12

a, b, c = t1                     # a,b,c are variables a is 10, b is 11, c is 12

This is called tuple unpacking assignment. More tuples in later section.

>>> a, b, c = t1

>>> a

10

>>> b

11

>>> c

12
>>>

Assigning different datatypes to variables

name, age ,salary = “Jennifer”, 26, 99000  #name, age, salary are variables.

>>> name, age ,salary = "Jennifer", 26, 99000

>>> name

'Jennifer'

>>> age

26

>>> salary

99000

>>>

A variable can hold more than one value

>>> a = 10, 20, 50  #comma separated values are Tuples in python

>>> a

(10, 20, 50)

>>>

This is called tuple packing assignment.