Loading...

Python tutorial

Variables In Python 

Variables are containers for storing data values.

Unlike other programming languages, Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

Rules for variable names

  • names can not start with a number
  • names can not contain spaces, use _ intead
  • names can not contain any of these symbols:

    :'",<>/?|\[email protected]#%^&*~-+
  • it's considered best practice (PEP8) that names are lowercase with underscores

  • avoid using Python built-in keywords like list and str
  • avoid using the single characters l (lowercase letter el), O (uppercase letter oh) and I (uppercase letter eye) as they can be confused with 1 and 0

Assigning Variables

Variable assignment follows name = object, where a single equals sign = is an assignment operator

A variable is created the moment you first assign a value to it.

create a file demo_variables1.py add the below code in this file.

x = 5
y = "John"
print(x)
print(y)

Execute the code in following below manner.

C:\Users\My Name>python demo_variables1.py
5
John

Variables do not need to be declared with any particular type and can even change type after they have been set.

x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
C:\Users\My Name>python demo_variables2.py
Sally

String variables can be declared either by using single or double quotes:

x = "John"
# is the same as
x = 'John'
C:\Users\My Name>python demo_variables7.py
John
John

Assign Value to Multiple Variables

x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
C:\Users\My Name>python demo_variables8.py
Orange
Banana
Cherry

And you can assign the same value to multiple variables in one line:

x = y = z = "Orange"
print(x)
print(y)
print(z)
C:\Users\My Name>python demo_variables6.py
Orange
Orange
Orange

Reassigning Variables

a=5
print a
a = a + 10
print a
C:\Users\My Name>python demo_var.py
5
15

Output Variables

The Python print statement is often used to output variables.

To combine both text and a variable, Python uses the + character:

x = "awesome"
print("Python is " + x)
C:\Users\My Name>python demo_variables3.py
Python is awesome

You can also use the + character to add a variable to another variable:

x = "Python is "
y = "awesome"
z =  x + y
print(z)
C:\Users\My Name>python demo_variables4.py
Python is awesome

For numbers, the + character works as a mathematical operator:

x = 5
y = 5
print(x + y)
C:\Users\My Name>python demo_variables5.py
10

If you try to combine a string and a number, Python will give you an error:

x = 5
y = "John"
print(x + y)
C:\Users\My Name>python demo_variables_test.py
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Global Variables

Variables that are created outside of a function (as in all of the examples above) are known as global variables.

Global variables can be used by everyone, both inside of functions and outside.

Create a variable outside of a function, and use it inside the function

x = "awesome"

def myfunc():
  print("Python is " + x)

myfunc()
C:\Users\My Name>python demo_variables_global.py
Python is awesome

If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.

Create a variable inside a function, with the same name as the global variable

x = "awesome"

def myfunc():
  x = "fantastic"
  print("Python is " + x)

myfunc()

print("Python is " + x)
C:\Users\My Name>python demo_variables_global2.py
Python is fantastic
Python is awesome

 Global Keyword

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.

To create a global variable inside a function, you can use the global keyword.

If you use the global keyword, the variable belongs to the global scope:

def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)
C:\Users\My Name>python demo_variables_global3.py
Python is fantastic

Also, use the global keyword if you want to change a global variable inside a function.

To change the value of a global variable inside a function, refer to the variable by using the global keyword:

x = "awesome"

def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)
C:\Users\My Name>python demo_variables_global4.py
Python is fantastic