What is a variable?

def myFunc():
    x = 2
    return None

<script type="text/javascript">
// Say hello world until the user starts questioning
// the meaningfulness of their existence.
function helloWorld(world) {
  for (var i = 42; --i >= 0;) {
    alert('Hello ' + String(world));
  }
}
</script>
<style>
p { color: pink }
b { color: blue }
u { color: "umber" }
</style>


x = 2
There are many online resource
y = 1 + x

Back Story

There are many online resources out there to get you thinking like a programmer, so I'll go over a few simple concepts that may help you along the way. The number one thing i tell people that they need when learning python is 'application'. Without 'application' a lot of concepts will become a lot harder to learn since you may not see their relivance. If you are just starting in python i recommend trying to come up with a simple idea for a script and then learn the necassary steps to achieve your goal.

The code

So, what is a variable?
Remember those Algebra lessons you never thought you'd use?
y = 1 + x
'x' and 'y' are variables.  In general terms, variables are symbols we put in place of some value. If we make 'x' equal a number it is called assigning a variable.
x = 2
Now, if we run this in a python interpreter we can print the result
>>> x = 2
>>> y = 1 + x
>>> print y
3

Types of variables

Python is an object-oriented language, so variables hold objects.  Below is a general overview of some common object types that are built into python.

Basic Types


x = 1 
y = -3 
z = 2435444

int

Int is short for integer and can be any whole number.


x = 1.0 
y = -5.5 
z = 2435.34534544

float

Float is short for floating point and can be any number with a decimal.

str

Str is short for string and refers to a sequence or string of characters enclosed by in single or double quotes.
x = 'I am a string'
y = "1.0"
z = "^^ that is also a string"

Container Types

Containers are objects that are designed to hold more than one object (or element) within them.
Each element of a container type should be separated by a coma.

list()

A list is a mutable ordered group of objects. To create a list enclose your objects in brackets.
x = [1, -3, 2435444]
To get an object from a list use the list's variable name object's number, starting with 0, enclosed in brackets.
>>> print x[0]
1
>>> print x[2]
2435444
Setting an object in a list uses the same convention.
>>> x[2] = 14
>>> print x
[1, -3, 14]

tuple()

A tuple is a list that is immutable.  To create a tuple enclose your objects in parentheses .
x = (1, -3, 2435444)
You can get an object from a tuple the same way as a list.
>>> print x[0] 
1
Since tuples are immutable and cannot be edited you mush create a new tuple if you would like to set an object.
>>> x[2] = 14
TypeError: 'tuple' object does not support item assignment
Instead do this:
>>> y = (x[0], x[1], 14)
>>> print y
(1, -3, 14)

dict()

dictionaries are probably my favorite python variable.  I use them everywhere.  Unlike lists and tuples where the you access a the value of an element by giving it an index, dictionaries access values by giving them a key.  keys can be any immutable variable, and because keys can't garantee to have a natural order to them like the indexes, dictionaries don't have an order either.  They have absolutely no concept of which element is first or last.  If you want to make a dictionary use curly brackets and seperate each key and value with a colon.  Once again, seperate the each element with a comma.
x = {'a':1, 'b':-3, 'c':2435444}
Dictionaries are muteable and can be access using brakets like lists and tuples.
>>> print x['b']
-3
They can be set in the same manor.
>>> x['c'] = 14
>>> print x
{'a':1, 'b':-3, 'c':14}
[code language="python"]
y = x + 1
[/code]

Mutable Vs Immutable

KEEP THIS MORE GENERAL.. DONT MENTION THE TYPES... JUST THE CAUSE AND EFFECT MUTABLE - CANNOT CHANGE.  IMMUTABLE - CAN CHANGE These can feel like odd terms but they basically refer to whether your variable type can be edit after you create it or not.  The three types above (int, float, str) are all immutable types.  The way you get around the limitation of immutable objects is by create a new object. by looking back at our original equation you can see that y is newly created object that has the 1 + the value of x (2). [code language="python" light="true"] x = 2 y = 1 + x >>>> 3 [/code] This also works on strings [code language="python" light="true"] x = 'I am a string' y = x + ' of characters.' >>>> 'I am a string of characters.' [/code]

Containers

These are objects that are designed to hold more than one object (or element) within them.
Separate each element of a container type with a coma.
A few of the most common container types are:

list()

This is a group of variables all in a row. You create a list by enclosing your variables in brackets and seperating them with commas.
Define
x = [1, -3, 2435444]
To get an element of a list use the list variable name with the element's number starting with 0 enclosed in brackets.
Get
>>> print x[0]
1
Setting an element of a list uses the same element number enclosed in brackets.
Set
>>> x[2] = 14
>>> print x
[1, -3, 14]

tuple()

This is a group or variables all in a row (much like a list) but you can't edit it.  This is what is referred to as immutable.  In order to change the contents of a tuple you have to create a new one.  You create a typle by enclosing your variables in perenthises and seperating them with commas.
x = (1, -3, 2435444)
you can get values the same way as a list, but when setting you will have to create a new tuple
>>> x[2] = 14
TypeError: 'tuple' object does not support item assignment
instead do this
>>> y = (x[0], x[1], 14)
>>> print y
(1, -3, 14)

dict()

dictionaries are probably my favorite python variable.  I use them everywhere.  Unlike lists and tuples where the you access a the value of an element by giving it an index, dictionaries access values by giving them a key.  keys can be any immutable variable, and because keys can't garantee to have a natural order to them like the indexes, dictionaries don't have an order either.  They have absolutely no concept of which element is first or last.  If you want to make a dictionary use curly brackets and seperate each key and value with a colon.  Once again, seperate the each element with a comma.
x = {'a':1, 'b':-3, 'c':2435444}
Dictionaries are muteable and can be access using brakets like lists and tuples.
>>> print x['b']
-3
They can be set in the same manor.
>>> x['c'] = 14
>>> print x
{'a':1, 'b':-3, 'c':14}

Comments

Anonymous said…
FIRST ONE!!!
Anonymous said…
REPLYING!!

Contact Me Directly

Name

Email *

Message *