Python Lessons
hi, i am planning to make a small python introduction, then move to Tkinter
(very cool stuff to make boxes and buttons) and then some PYgame(building games in python)
Table of contents
1. Installing python/introduction
2. Simple programs
3. variables
4. Loops
5. Input - Interaction with humans part 1
i. Strings
6. Tkinter-The basics
7. Tkinter(interaction with humans part 2)
1. Installing python / introduction
Download python and read the "read me", which will help you set things up.
Open IDLE(python GUI) thats where you will be typing your program later.By pressing F5 you run it. But for now, press F5 and we will be working with that window(python shell)
introduction
Python is a programing language like for example C++, C, Matlab , Java and many more. Python has it's advantages. Using Python you can do
>>> 2+2
4
>>>print "how are you?"
how are you?
or you can write in files, save them and python will run them like a normal program would.
2. Simple Programs.
write the following and press Enter.(the ">>>" should already be there)
>>> print "hello! hello! hello!"
hello! hello! hello!
you just wrote a tiny program that prints "hello! hello! hello!
You can also add, subtruct and do many other things--
Try those
>>>1+1
2
>>>3+8
11
>>>12-11
1
>>>print "2+2=4"
2+2=4
>>>4*5
20
>>>100/20
5
>>>2**3
8
("**" means to the power)
(% means remainder)
>>>13%2
1
Python doesnt know how to find the square root of a number, But we can import this knolwdge into it. Here's how:
>>>from math import sqrt
>>> sqrt(25)
5.0
so, so far we know this-
input | name | example | output |
+ | addition | 12+7 | 19 |
- | substruction | 10-5 | 5 |
* | multiplication | 12*12 | 144 |
/ | division | 7/7 | 1 |
** | power | 2**6 | 64 |
% | remainder | 16%7 | 2 |
sqrt | square root | sqrt(144) | 12 |
ofcorse rules of math (order of operations) do apply, for example
>>> 4+6*6
40
If you write "#" before anything it makes it invisible to the program.
>>>print "hello" #this text is invisible
hello
more to come to this section very very very soon
3. Variables
variables are letters a,b,c,.. that are used for convinience
so for example here is a simple program.
>>>a=70
>>>a+1
71
>>>a=30
>>>b=34
>>>f=12
>>>a*b+f
1032
Now we will make things a little bit more complicated.
Here is how you write in files.
You can either write a program in "notebook" and save it a "something.py"
the "py"is what makes it a python file.
Next you go to IDLE open file and run the program by clicking F5.
Or you simply write the program in IDLE (not python shell or command line like before)and then press F5 to run it.
Lets first start with fairly easy programs.
in your file write down the following
print "hi" #this is line one
print "how" # this is line two
print "are" # this is line three
print "you" # this is line four
print "today" # this is line five
when you run it it will look like this
hi
how
are
you
today
we can use variables in sentences too shown in the program below.
a = "left"
b = "right"
print "here is how you should go to school every day"
print "go", a, "then ", b, "then", a, "then", b, "and", b, "again"
when you run it it will look like this
here is how you should go to school every day
go left then right then left then right then again right
page 2