Scientific Linux

  Reply to this topicStart new topicStart Poll

> Python Lesson 1
 Posted: Jun 28 2011, 08:37 AM
Quote Post


Group: Members
Posts: 47
Member No.: 180
Joined: 15-May 11


I have been programming for a few years now in python and I have come to share my knowledge in python by writing a few "Lessons" for a beginner to python and programming its self. These assume NO knowledge of programming or python. This is designed to be read from start to end, if you already know the basic start stick with it, don't skip ahead. Without further ado:

Python is a relatively new programming language but with that it brings some great things its learnt from the mistakes of others. This language has had popular uptake by big companies such as Google as well as being increasingly taught in education. Python can run on Linux, Mac OS X, windows, bsd, haiku and others.

Python is an interpreted language, this means it takes each line of code from the file and executes it. It also gives you what will be one of you're greatest assets, the interpreter, this you can feed a file and it will run however whats more interesting if you can start it without a file and it will give you a prompt allowing you to write lines of code in that it will run. Think of this as an interpreter in real life someone says something in a foreign language, after the sentence the has finished the interpreter will read it back to you in your language, it's the same principal with this.

You can invoke the interpreter by going to terminal (Applications -> System Tools -> Terminal) and python "python"


# python
Python 2.6.6 (r266:84292, May 20 2011, 16:42:11)
[GCC 4.4.5 20110214 (Red Hat 4.4.5-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

the ">>>" is where you type the code, its called the prompt.

First and foremost its important for you to know about maths as you tend to do a lot of number work in programming as you will find out as you get into programming more. Most maths you will do will most likely simple but its maths non the less so you best know how to do it.

As you do maths in a calculator or written down it seems pretty normal, do these along with me with your python interpreter:


>>> 2 + 5
>>> 2 - 5
>>> 2 * 5
>>> 2 / 5

Now all seems normal? What about the division? its 0, surely its not 0. We will come onto that in the next section (data types) and I shall explain exactly why it is 0.

You can also do more complicated things like powers so 2^5 but this in python is written 2**5


>>> 2**5

Python also respects the rules of maths so if you can think back to when you did maths at school and they most likely thought you PEMDAS (maybe you learnt it by "Please Excuse My Dear Aunt Sally") This basically means parenthesis (so the brackets which this is in now) then the exponent (the power so like 2**5) then multiplication and division are the same so its whichever comes first so:

4 * 5 / 8

would do multiplication first where as:

4 / 5 * 8

would do division first

Equally with addition and subtraction they are equal so it does addition and subtraction in the order it comes so:

5 + 2 - 6

would do the addition and then the subtraction where as:

5 - 2 + 6

would do the subtraciton then the addition.

Now we have that basic maths out of the way you will understand a mathematical equation such as


>>> (2 + 5) * 4 - 17

Data types
These are very important when programming, this took me a while at first to understand and i got very frustrated but bare with it. We first need to look at data its self. Data is just stuff right? okay well stuff can be "Hello" or it can be 72 but i can't add 4 to "Hello" yet i can add 4 to 72 to these seem very different? Computers see that too they need to know what type of data these things are and so do you as a programmer.

Okay so we have just been doing maths, these are numbers aren't they? well numbers can be split into two main types whole numbers
72, 4, 9, 10, etc...
and decimals

This is quite important to tell python if you mean a whole number or a decimal number this is to do with how it stores it in the computers RAM (we will get onto that in a later section) but also how maths is done, now earlier we looked at this


>>> 2/5

Now that's quite strange but we need to look why it did that. We have 1 whole number 2 and we are trying to divide it by 5 which is another whole number. Now python isn't smart, we as humans can look at that and realize that actually that won't be a whole number that will be a decimal but python doesn't, it simply knows what you tell it and you haven't told it you want a decimal infact in this line of code it forgets about decimals.

So you might ask okay so i want to tell python this is going to be a decimal so i want it to give you the answer as one. The way you do that is give python a decimal to start with and then it thinks "O okay well they gave me a decimal i better give them the answer as a decimal". So hold on you but 2 is a whole number how do i make it a decimal. Well 2 as a decimal is 2.0 you put the decimal part (the bit after the point) as a 0.

So lets try it:


>>> 2.0 / 5.0

Okay so lets look we have 0.4 that looks right but tons of 0's and then a 2, whats going on there! Well as we did initially with 2/5 we will get to this in the next section (Binary & Storing).

Okay so we have got whole number and decimal numbers, now the correct mathematical term for whole numbers is integer, so often shorted to int. So an int is

2, 7, 72, 10, etc..

int is an integer and an integer is a whole number

Now a decimal is called a float there are reasons why that we will come to later however for now just accept its called a float. So an float is:

9.2, 4.85, 9.827394, 172.0003, etc...

a float is a decimal number.

okay so we have this under control but we still have "Hello" from the start, that isn't a int its also not a float. What is it?

Well that is a good question, its a string. I'm sure you've heard the word string in the English language just to mean a load of stuff i guess and that's all it is its a load of stuff you can store words as a string like Hello and you can also a sentence like

"The blue hamster liked climbing tree's"

notice how i put "" around strings, in python you can use '' or "" that's the single quote or the double quote now i tend to use "" as like with the sentence i showed you i more often than not have ' in them. Now clearly we can't do maths

"Shoe" + 9

is clearly not okay and python will also "flip out" if you do such a thing:


>>> "shoe" + 9
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

Now okay you might be thinking now help whats it telling me i have no idea. That's okay i will write an entire section on how to interpret them. And how to use these to help fix programs which mistakes (bugs). However for now just accept if it tells you traceback and a load of stuff, you have told python something it didn't like or understand.

Also you might have noticed we tend to call things different names in python such as integer goes to int, we do this with string too. string goes to str so:

string goes to str such as "Hello World!"

One cool thing you might note is you can have numbers but as strings so "72" it doesn't see it as a number, its just a string and you must stop seeing "72" as a number, the number you are thinking of is 72. I can do 72 + 1 and get 73 however you can't do "72" + 1 you get a traceback. You might be saying but why and struggling to grasp this and that's fine. The reason you can't add "72" and 1 is because 1 is an int (integer -> whole number) and "72" is a str (string). and you cant mix these!

Lastly I want to say about booleans, these are 2 states. as in on or off, black or white, 0 or 1. It is something exists in 1 of 2 states. In python they are simply True of False (the capital T and F are important!)


>>> True
>>> False

Okay that seems a little pointless right? Sure right now they seem a bit pointless but they really become helpful for understand python and using some of the other parts of the language later on so make sure you understand them. As with strings and integers python shortens booleans to bool so:

Boolean becomes bool

True, False

Now you know about:

- str (string)
- int (integer)
- float
- bool (boolean)

a cool thing is this its called type() you type type with the thing you want the type of inside the brackets and it gives you something that looks like
<type 'int'>

this means its an int you can do it for others


>>> type(72)
<type 'int'>
>>> type("Hello")
<type 'str'>
>>> type(9.4)
<type 'float'>
>>> type(False)
<type 'bool'>

Okay that's a lot to take in, if you don't understand something please ask or re-read what i wrote, there are things i will come back to in later lessons (in other posts) keep trying these until you understand them there uses will become apparent later. Below are some exercises, you can try and do these, to test your self making sure you know the material covered.

Right so these are a few questions you can ask your self, you can make your own, basically I will give you a line of python it might be some maths or a data type and you have to decide which it is, then you can test it by if its maths entering the maths and checking the answer is what you thought it was or if its a type using the type() thing and checking its the type you think it is:

1) What type is "Hello"?
2) What type is 2.0?
3) What type is 9?
4) What type is True?
5) What type is 6.0?
6) When you do 9 / 2 what do you get?
7) When you do 5 ** 5 what do you get?
8) When do you do (5 + 4) * 8 what do you get?
9) When you do 3 / 2 what do you get?
10) Is "99" - 1 okay to do?
PMUsers Website
 Posted: Jun 28 2011, 08:56 AM
Quote Post

SLF Advocate

Group: Members
Posts: 472
Member No.: 32
Joined: 11-April 11

Well presented, Jessica, thanks.
I am now ready to advance to Lesson #2 (i.e. the int. 2) biggrin.gif

 Posted: Jun 28 2011, 09:22 AM
Quote Post


Group: Members
Posts: 47
Member No.: 180
Joined: 15-May 11

Thanks I wasn't sure if it would be any good, I wanted to take my time explaining things as tutorials and books for new programmers tend to leave out a lot and go quite fast in my opinion
PMUsers Website
 Posted: Jun 28 2011, 10:39 AM
Quote Post

Retired SLF Administrator

Group: Admins
Posts: 1276
Member No.: 2
Joined: 8-April 11

Great work Jessica

"Sometimes the best helping hand you can give is a good, firm push."
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:

Topic Options Reply to this topicStart new topicStart Poll