Python Variable

What is Variable?

In general sense variable means something whose value can be changed or varies, and of-course it has a name. Like a container can contain three marbles, again the container is big enough to hold 4 marbles or more than that but it has a limit. You can call the container as marble-container.

In programming variable means something which contains a value in a memory location in our computer’s memory. We give a name to indicate it. Like:

X = 10 or y =3.5

Again, its value can be changed. Like:

X = 20 or y = 3.9

Assignment of variable:

We do not need to define variable type explicitly in Python because Python totally Object Oriented. Variable are also Object is Python.  

we can just print a value like :

print(5)

Output: 5

But if we need to reuse this value then definitely we have to provide a name so that we can find out it next time:

Ex1:

a = 5

print(“a =”, a)

output:

a = 5

Ex2:

b = a+10

print (b)

output:

15

Variable Naming Conventions:

In mathematics or in any other programming language, we have seen that a variable can be a short name such as a, b, A, or C etc. Again it can a detailed name like: amount, ratio, userName etc.

Python has several and convention for Python variables’ naming:

  1. Variable name must start with an alphabet or the underscore character

Ex: a = 10, age = 20; _name = “Dada”

  1. Variable name cannot start with a number

Ex: 5bc = 10    —> not allowed

  1. Variable name can only contain alphanumeric characters and underscores

Ex: user5 = “Michael Anderson” , userName = “Xyz” , last_name = “James Bindas” etc.

  1. Python is a Case Sensitive programming language.So, Variable names are case-sensitive

Ex: name = “Alicia Jones” , Name = “Alicia Jones” refers to two different variables.

Same variable can be used to assign different types of values. Like in this example we have integer and then String to the same variable. Existing python data types are:

  1. Number
  2. String
  3. List
  4. Tuple
  5. Dictionary

This data types will be explained elaborately in the letter tutorials.

Variable’s value assignment:

Ex: when a is number then its value changed to a string

a = 5

print(“a =”, a)

a = “High five”

print(“a =”, a)

output:

a = 5

a = High five

Multiple or Parallel assignment: we can declare multiple variables at a time and assign them values

a, b, c = 9, 3.8, “Hello World”

print (“a: “, a)

print (“c: “, b)

print (“c: “, c)

output:

a: 9

b: 3.8

c: Hello World

a = 10

b = a

k = 10

then their values will be same