Learn Python Variables





Part 1


Variable Names


There are several rules about how we can call a variable


A variable name may not start with a number


A variable name must start with a letter or the underscore


A variable name must only contain alpha-numeric characters and underscores (0-9, A-z, and _ )


Variable names are case sensitive so Jake and jake are not the same variable







Part 2


Creating Variables


Once we assign value to Jake we created a variable.


You dont have to declare variable, once you assign value to it, you can use it and even change its type later.

Code Example:

                   a = 20
                  b = "Jake"
                  print(a)
print(b)
                
                


Code Example:

                   a = 5 
                  # a is now integer
a = "Jake"
 # a is no longer integer ( we changes its type )
print(a)
                
                







Part 3


Assign Value to Multiple Variables


In the next example we learn how to assign values to more than 1 variables at once:


Code Example:

                   a, b, c = "Jake", "David", 10
print(a)
print(b)
print(c)
                
                







Part 4


Global Variables


Global variables are variables that we place outside of a function ( we will learn about function later ), everyone can use them, inside function or block codes or outside


Code Example:

                   a = "Wiki" # the variable a is created outside of the function, but used inside it.
def myfunc():
  print("Free Code " + a)

myfunc()
                
                


However if there are two variables, one inside function and one outside, the variable inside the function will be local variable and can only be used inside the function


Code Example:

                   a = "Outside"

def myfunc():
  a = "Inside"
  print("FreeCode " + a)

myfunc()

print("FreeCode " + a)
                
                







Part 5


Output Variables


In Python we use the statement print to output data.


Code Example:

                  a = "Jake"
print(a)
                
                


You can also use + to be efficient


Code Example:

                  a = "Free"
b = "CodeWiki"
c =  a + b
print(c)

# Either way will work

a = "Free"
b = "Code"
c = "Wiki"
print(a + b + c)
                
                


With numbers however, the + character works as a mathematical operator:

Code Example:

                  a = 10
b = 20
print(a + b)
                
                



tip: You can't combine a number and a string.







Part 6


The global Keyword


If you need a global variable inside a function, use the global keyword ( remember that normally variable inside function will be local unless we use the global keyword.)


Code Example:

                  def myfunc():
  global a
  a = "CodeWiki"
myfunc()
print("Free " + a)
                
                


Let's say you have a variable inside a function, and another global variable with the same name outside the function, but you need to controll the inside variable, you can manipulate the out side global variable by changing the variable inside the function to global:

Code Example:

                  a = "Jake"  # global var outside
def myfunc():
  global a # global var inside manipulating the outside global var.
  a = "Sparrow"
myfunc()
print("Name is " + a)
                

                
                







FreeCodeWiki is the best way to learn python courses, FreeCodeWiki offer free python course, learn python online and free.


Navigation