srzara018
Lesson 2 - Very simple ‘programs!
Sthurlow.com
Civilization IV Python
tutorial
The Python Tutorial
The original sthurlow.com python
tutorial
Installing Python
Very Simple Programs
Variables, Scripts
Loops, Conditionals
Functions
‘Tuples. Lists, Dictionaries,
for Loop
Classes
Importing Modules,
File vO
Error Handling
The Civ4 Programming
Tutorial
All new Civ4 programming tutorial
Lesson + (not released)
htp:iwwwsthurlow-comipythonosson02/
Very simple 'programs'
Introduct
(OKI We have python installed, now what? Well, we program!
‘And itis that simple (at east for now), Python makes it easy to run single lines
of code - one-liner programs. Lets give it a go.
Opening IDLE
Go to the start menu, find Python, and run the program labelled IDLE’ (Stands
for Integrated Development Environment.
Now you are in the IDLE environment. This is the place you will be spending
‘most time in, Here you can open a new window to write a program, or you can
simply mess around with single lines of code, which is what we are going to do.
‘Type the following and press enter: (don't type >>> as it should already be
there)
>>> print "Hello, World!
What happened? You just created a program, that prints the words ‘Hello,
World’, The IDLE environment that you are in immediately compiles whatover
you have typed in. This is useful for testing things, e.g. define a few variables,
and then test to see ifa certain line will work. That will come in a later lesson,
though.
Math in Python
Now try typing the stuff in bold. You should get the output shown in blue. I've
given explainations in brackets.
pode
2
>>> 20480
180
>>> 182984449566
467860
(These are additions)
>>> 6-5
(Subtraction)
>>> 295
18
(multiply, rabbits!)
>>> 52
25
(Exponentials e.g. this one is 5 squared)
>>> print "1 +2 is an addition"
142 fs an addition
(the print statement, which writes something onscreen)
>>> print “one kilobyte is 2°10 bytes, or", 2**10, “oytes
one kilobyte is 2°20 bytes, or 1024 bytes
(you can print sums and variables in a sentence.
“4srzara018
htp:iwwwsthurlow-comipythonosson02/
Lesson 2 - Very simple ‘programs!
The conmas seperating each section are a way of
seperating clearly different things that you are
>>> 21/3
7
>>> 23/3
7
>>> 23.0/3.0
7.6666,
(division, 2nd time ignoring renainder/decimals,
3rd time including decimals)
>>>. 2353
2
>>> 49%10
9
(the remainder from a division)
— »
AAs you see, there is the code, then the result of that code. | then explain them
in brackets. These are the basic commands of python, and what they do, Here
isa table to clarify them (because tables look cool, and make you feel smarter
de
command name example output
+ Addition 445 9
- Subtraction 8-5 3
. Multiplication 4° 20
I Division 19/3 6
% Remainder 19%3 5
” Exponent 24 16
Remember that thing called order of operation that they taught in maths? Well,
it applies in python, too. Here it's, if you need reminding:
41. parentheses ()
2. exponents *
3. multipication *, division \, and remainder %
4, addition + and subtraction -
Order of Operations
Here are some examples that you might want to try, if you're rusty on this:
poo e2e3
7
poo (142) 43
9
In the first example, the computer calculates 2 * 3 first, then adds 1 to it, This is
because mutiplication has the higher priority (at 3) and addition is below that
{at lowly 4).
Inthe second example, the computer calculates 1 + 2 first, then multiplies it by
3, This is because parentheses (brackets, le the ones that are surrounding
this interluding text :) ) have the higher priority (at 1) and addition comes in
later than that,
‘Also remember that the math is calculated from left to right, UNLESS you put
28srzara018
htp:iwwwsthurlow-comipythonosson02/
Lesson 2 - Very simple ‘programs!
in parentheses. The innermost parentheses are calculated frst. Watch these
examples:
>>> 4 = 40-3
-39
>>> & = (40 - 3)
°33
In the first example, 4 -40 is calculated,then - 3is done.
In the second example, 40 - 3 Is calculated, then itis subtracted from 4.
Comments, Please
‘The final thing youll need to knew to move on to multiline programs is the
‘comment. Type the following (and yes, the output is shown):
>>> HT am a comment. Fear my wrath!
‘A comment is a piece of code that is not run. In python, you make something a
comment by putting a hash in front ofit. A hash comments everything afer it in
the line, and nothing before it. So you could type this:
>>> pri
it "food is very nice” Heat me
food is very nice
(2 normal output, without the smutty comment,
thankyou very much)
>oott print “Food is very nice
(nothing happens, because the code was after 2 conment)
>>> print "food is very nice” eat me
(you'11 get a fairly harmless error message,
because you didn't put your comment after a hash)
Comments are important for adding necessary information for another
programmer to read, but not the computer. For example, an explanation of a
section of code, saying what it does, or what is wrong with it. You can also
‘comment bits of code by puting a # in front of it-if you don’t want it to compile,
but cant delete it because you might need it later.
Conclusion
There you go! Lesson 2 Completed, That was even shorter than lesson 1!
Next lesson, we make programs with many lines of code, and save them, so
we can actually send them to people. That's right, you don't have to retype
every program you run! What an amazing innovation!
Thanks to all,
sthurlow.com
34srzara018 Lesson 2 - Very simple ‘programs!
htp:iwwwsthurlow-comipythonosson02/ 48