Thursday, 4 June 2015

Variables

A variable is a name that stores a value to be used later within the script. You can use anything as a variable so long as it isn't a Python Keyword and the best way to find out which keywords you can't use within python is to go into the shell and put the following in:

>>> import keyword
>>> print (keyword.kwlist)

This will then bring up all the keywords within python and show you the words that you cannot use for a variable. You also cannot use a variable if it begins with a number or has a space within it.

An example of what isn't aloud is as follows:

1home_sweet_home      -  This is because it has a number in front of it.

home sweet home          -  This is due to fact that there our spaces. 

In order to get round the spaces you need to use the underscore button _ this will then create a space when it is printed from the script.

When assigning a value to a variable you need to set the equal sign in it this gives it the value for example:

variable = value

An example of assigning a variable:

>>> home_now= 'home' 
>>> print (home_now)
home

The print function is used in order to perform the output of the value of the variable without having to use quotation marks.

You can also use variables with strings as well in the print statement:

String = 'I want to go'
Variable = home_now

an example

>>> home_now = 'home' 
>>> print ('I want to go', home_now)
>>> I want to go home

Remember you cannot use a variable if it hasn't got a value added to it as seen below this is what it will return in the python shell.



The next section that I shall have a look at attempting is the different types of data from python.

No comments:

Post a Comment