Learning Python For Beginners Lesson 1
(learn to program the hard way the easy way)
Hi, guys. Welcome to my tutorial on Learning Python For Beginners! In the tutorial, I will attempt to teach any non-programmers the basics of python and programming. Just to make this clear, I will be teaching python 3.x.x, not 2.x.x.
Zzzzzz..... I'm not going to write a boring, long preface. For me, it just puts me off the whole book/tutorial.
- A computer
- A brain
- Common sense
- A pair of hands
- Python 3 installed on your computer
- Basic knowledge on computers and how they work (if you don't know what deleting System32 does, this tutorial is not for you)
- A Basic understanding on algorithms (I will explain further on how to get a better understanding on algorithms)
If you don't know what an algorithm is or how they work, it is basically step-by-step instructions to do something (e.g: an algorithm to speak is: open your mouth, make a noise through your mouth). If you still don't understand or want to learn more, try using programs like 'Scratch' and 'Stencyl' which utilise basic box dragging and dropping mechanisms to make simple algorithms and behaviours in 2D games. Eh.
Go to python.org
and download any version of python 3.x.x, try downloading the latest version but any version will do. Don't download any ide, it is harder to learn using those. Just use the default python IDLE.
Ubuntu (idk about other distros):
Ubuntu has python 3 already installed.
To install the IDLE, go to the Ubuntu Software Center and search 'python 3 idle'. Download the latest idle version.
The Python IDLE:
This is the python IDLE shell. It will act as the console screen for running your python code. To create a new file, do CTRL-N. To open a '*.py' file, do CTRL-O.
To run an opened python file, press F5.
Create a new file and paste the code:
Run the code.
That's it! That is the hello world for python! Nothing else! Just print and 'Hello World' in speech marks and brackets! The print function
is used to display text on the console - not like that takes much thinking.
Looking At Variables:
Have a look at the following code:
text = "I like cake"
What do you think it does? If you thought it would print 'I like cake', then you are right!
A variable is when you assign a name to something. It is a bit like saying 'do your chores' instead of saying 'wash the dishes, clean your bedroom, mow the lawn...'.
What the code means is that in line 1 it is saying in line 1 that "by when i say 'text', I mean 'I like cake'.
There are three types of variables I would like to talk about, strings, ints (integers) and floats.
A string is any sequence of characters (any utf-8 character) in speech marks (double or single). E.g:
"Example String 2 $)£!%^~"
An integer is any whole number. It can be negative too. It doesn't have any speech marks. E.g:
A float is basically an integer but with a decimal point. E.g:
The '+' sign:
For strings, the '+' sign is used to join two strings together into one.
string1 = "Hello "
string2 = "User"
joined = string1+string2
For integers and floats, the '+' sign is used to 'add' numbers together. '-' means minus, '/' means divide, '*' means multiply, '**' means multiply to the 'power'.
int = 2+4 # The answer is 6
int1 = 5
int2 = 9
final_int = int1*int2 # The answer is 45
float = 2.5+4.2 # The answer is 6.7
float1 = 2.5
float2 = 2.0
final_float = float1+float2 # The answer is 5.0
Here is are some situations:
number = 10+2.5 # You are expecting 'number' to be 12.5
text = "My Age: "+5 # You are expecting 'text' to be 'My Age: 5'
math = "5"+2.1 # You are expecting 'math' to be 7
Situation 1 will work however it is better to take my advise that I will tell later.
Situation 2 and 3 will not
work. It will result in a 'TypeError' because you cannot add a string and int/float together. The python interpreter does not know if you want to add the variables together or to join them.
To fix this, you will have to convert the variables. You can convert variables to a string by doing 'str("Example String
")', you can convert to an int by doing 'int(100
)' and you can convert to a float by doing 'float(100.0
text = "5"+2
number = int("5")+2
text = "My Age: "+5
text = "My Age"+str(5)
Thanks for reading. Soon I will update my tutorial and add more
. Tell me what you think.