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.
Thursday, 4 June 2015
Thursday, 14 May 2015
My First Attempt
The reason that I have started to look at coding is to help me to do something in my spare time that is constructive and to gain a better understanding of computers as well. I was looking for a language to get started on and came across Python as one of best coding languages to get started with. So with that I also bought a Raspberry Pi to keep it separate from my normal computer and due to the fact that I can plug it into the TV which means that I have a bigger screen.
So having plugged my raspberry pi in and getting it up and running I find that I have Python 3 installed on it and that the shell (See picture below) for its use is called IDLE. It looks very scary to start with having never used anything like this before and they seems like a lot of options on the menu bar to use. However I have myself armed with a beginners guide to helping get a grip to understanding it and its a book called Sams Teach Yourself Python Programming for Raspberry Pi in 24 hours written by Richard Blum and Christine Bresnahan. Having this a starting point is extremely helpful as without this I think that I would be lost and I strongly recommend to anyone that is looking to start python to get yourself a book or some on-line support to help get a basic understanding.
Having a little type in the shell brings up an error code of the following :
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
hello
NameError: name 'hello' is not defined
However in order to write within the shell and get a response you have to write in functions in other words you have to tell it what you want it to do but in a technical way for example to say hello you have to put the following in:
Print 'Hello'
This then kicks out the response of Hello from the system upon looking further into this the quotation marks so that the system knows that this is what it is going to produce an output if you miss parts of the quotation mark out it brings it back as an error message so you need to make sure that what you are putting in has a end quotation mark on it. As you might have noticed whatever you enter after them will be repeated out on the line below. You can use either the single or double quotation marks for it to produce an output but you need to make sure that you use one and stick to it as if you start mixing them up then it can be hard to go back and read your script later on when you produce long ones.
This is the end of the first basic steps the next time I'm going to try out variables.
So having plugged my raspberry pi in and getting it up and running I find that I have Python 3 installed on it and that the shell (See picture below) for its use is called IDLE. It looks very scary to start with having never used anything like this before and they seems like a lot of options on the menu bar to use. However I have myself armed with a beginners guide to helping get a grip to understanding it and its a book called Sams Teach Yourself Python Programming for Raspberry Pi in 24 hours written by Richard Blum and Christine Bresnahan. Having this a starting point is extremely helpful as without this I think that I would be lost and I strongly recommend to anyone that is looking to start python to get yourself a book or some on-line support to help get a basic understanding.
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
hello
NameError: name 'hello' is not defined
However in order to write within the shell and get a response you have to write in functions in other words you have to tell it what you want it to do but in a technical way for example to say hello you have to put the following in:
Print 'Hello'
This then kicks out the response of Hello from the system upon looking further into this the quotation marks so that the system knows that this is what it is going to produce an output if you miss parts of the quotation mark out it brings it back as an error message so you need to make sure that what you are putting in has a end quotation mark on it. As you might have noticed whatever you enter after them will be repeated out on the line below. You can use either the single or double quotation marks for it to produce an output but you need to make sure that you use one and stick to it as if you start mixing them up then it can be hard to go back and read your script later on when you produce long ones.
This is the end of the first basic steps the next time I'm going to try out variables.
Subscribe to:
Posts (Atom)