Python (civ4 scripting language) tutorial thread

Gingerbread Man,
not to put pressure on you but when will the next lesson begin? Again I'm not pressuring you to hurry up your doing a great job to teach us but I'm just wondering?

Thanks for answering my question if you can.
 
The lesson is about 1/2 complete.

I was going to have a driving lesson, but things went awry, so the being written.

EDIT: It's now 3/4 written, I just have to play in a band for a few hours. Not long, my fellows!
 
LESSON 2:
One-liners - very simple 'programs'

OK! We have python installed, now what? Well, we program!

And it is that simple (at least for now). Python makes it easy to run single lines of code - one-liner programs. Lets give it a go.

Go to the start menu, find Python, and run the program labelled 'IDLE' (Stands for Integrated Development Environment, it isn't literally idle... think that's dumb? it took me a day to figure that out ;) ) If that doesn't work, go to the command line and type:
Code:
python
That starts the IDLE in a command line. You in now? Good.

Now you are in the IDLE environment. Type the following and press enter: (don't type >>> as it should already be there)
Code:
>>> 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 whatever you have typed in. This is useful for testing things, e.g. define a few variables, then test an equation. That will come in a later lesson, though.

Now try typing the stuff in bold. You should get the output shown in blue. I've given explainations in brackets.
Code:
>>> [b]1 + 1[/b]
[color=blue]2[/color]
>>> [b]20+80[/b]
[color=blue]100[/color]
>>> [b]18294+449566[/b]
[color=blue]467860[/color]
        (These are additions)
>>> [b]6-5[/b]
[color=blue]1[/color]
        (Subtraction)
>>> [b]2*5[/b]
[color=blue]10[/color]
        (Multiply, rabbits!)
>>> [b]5**2[/b]
[color=blue]25[/color]
        (Exponentials e.g. this one is 5 squared)
>>> [b]print "1 + 2 is an addition"[/b]
[color=blue]1 + 2 is an addition[/color]
        (the print statement, which writes something onscreen)
>>> [b]print "one kilobyte is 2^10 bytes, or", 2**10, "bytes"[/b]
[color=blue]one kilobyte is 2^10 bytes, or 1024 bytes[/color]
        (you can print sums and variables in a sentence. The commas seperating each section stop a few problems from occuring, which will be discussed later)
>>> [b]21/3[/b]
[color=blue]7[/color]
>>> [b]23/3[/b]
[color=blue]7[/color]
>>> [b]23.0/3.0[/b]
[color=blue]7.6666...[/color]
        (division, 2nd ignoring remainder/decimals, 3rd including decimals)
>>> [b]23%3[/b]
[color=blue]2[/color]
>>> [b]49%10[/b]
[color=blue]9[/color]
        (the remainder from a division)
As you see, there is the code, then the result of that code. I then explain them in brackets. These are the basic commands of python, and what they do. Here is a table (because tables look cool, and make you feel smarter ;) )
Code:
Command   Name            Example   Output
   +      Addition        4+5         9
   -      Subtraction     8-5         3
   *      Multiplication  4*5         20
   /      Division        8/2         4
   %      Remainder       15/4        3
   **     Exponential     2**3        8

Remember that thing called order of operation that they taught in maths? Well, it applies in python, too. Here it is, if you need reminding:
1) parentheses ()
2) exponents **
3) multiplication *, division \, and remainder %
4) addition + and subtraction -

Here are some examples that you might want to try, if you're rusty on this:
Code:
>>> [b]1 + 2 * 3[/b]
[color=blue]7[/color]
>>> [b](1 + 2) * 3[/b]
[color=blue]9[/color]
In the first example, the computer calculates 2 * 3 first, then adds 1 to it. This is because multiplication has the higher priority (at 3) and addition is below that (at lowly 4)
In the second example, the computer calculates 1 + 2 first, then multiplies it by 3. This is because parentheses (brackets, like 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 in brackets. Watch these examples:
Code:
>>> [b]4 - 40 - 3[/b]
[color=blue]-39[/color]
>>> [b]4 - (40 - 3)[/b]
[color=blue]-33[/color]
In the first example, 4 -40 is calculated,then - 3 is done.
In the second example, 40 - 3 is calculated, then it is subtracted from 4.

The final thing you'll need to know to move on to multi-line programs is the comment. Type the following (and yes, the output is shown):
Code:
>>> [b]#I am a comment. Fear my wrath![/b]
>>>
A comment is a piece of code that is not run. In python, you make something a comment by putting a hash in front of it. A hash comments everything after it in the line, and nothing before it. So you could type this:
Code:
>>> [b]print "food is very nice" #eat me[/b]
[color=blue]food is very nice[/color]
        (a normal output, without the smutty comment, thankyou very much)
>>> [b]print "food is very nice" eat me[/b]
        (you'll get a fairly harmless error message, which is a computer trying to tell you it doesn't know what 'eat me' means)
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 putting a # in front of it - if you don't want it to compile, but cant delete it because you might need it later.

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, Gingerbread Man
 
great lesson Gingerbread Man. Thank you for taking time to write the lesson.

I finished the lesson.

Thank you again for the great lesson.
 
please remove me from the program
 
I sure am worried about the graphical aspects of modifying the gamebry engine... allthough the python looks interesting indeed.
 
I am done with lesson 2
 
Late joiner here!

Been ghosting this site for the past 6-8 months or so, picking up tips on how to improve my game...Never really felt the need to register. Figured I might as well become a part of this community.

Programming experience = basic Turing knowledge...It's the programming language that is taught in grades 9/10 in Ontario...Sort of an intro to programming. Very limitted knowledge of html and perl. And I can count in binary :)

Completed lessons 1 and 2.
 
well Done Blackbird and Coorae, Welcome aboard, Trump, and sorry to see you leaving, ybbor.
Ybbor - Why the dropping out? If you got stuck somewhere, we're here to help. If you're short on time, well, maybe on day we'll see you catch up again. Good on you for having a go, anyway.

Trump - never mind the late joining, you can join at any time. And welcome to Civfanatics! Hope to see you around, in this forum, and maybe others too!
 
Gingerbread Man said:
Ybbor - Why the dropping out? If you got stuck somewhere, we're here to help. If you're short on time, well, maybe on day we'll see you catch up again. Good on you for having a go, anyway.

i just don't have the time right now, and i dought i will with school strating in a week :twitch: . i'll try to go through the whole thing over winter break though
 
Please PM me when new lessons come out or I'll forget. Hopefully I'll have time to get on later this school year during keyboarding. Working on lesson 2. My my, is it just me or does this look a lot like basic?
 
Done, thank you Ginger.
 
Welcome MSTK!

I wont maintain the list showing all of our skill levels, until we actually need to use it. We seem to be chugging along fine here.

Hopefully I'll have the next lesson written before long. But I'll definitely wait for the others to catch up.
 
I want to subscribe.
I have nearly no programming experience. Tried some things on C64 BASIC 15 years ago.

Edit: when I tried to do "lesson 1" all I got was:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name ´python´ is not defined.


:confused: :confused: :confused:

I am running WIN XP Pro.
 
What does it take for me to get to status "Complete"?

Oh, yeah, and I've went over the lessons. They were surprisingly simple. Even on my own I was able to enter and manipulate variables...which you will likely cover in the next lesson, right?
I hope you get to functions (or something like them with a different name) soon. Arrays would be nice.
 
Taé Shala said:
I want to subscribe.
I have nearly no programming experience. Tried some things on C64 BASIC 15 years ago.

Edit: when I tried to do "lesson 1" all I got was:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name ´python´ is not defined.


:confused: :confused: :confused:

I am running WIN XP Pro.

I had the same problem but here is how you solve it:

open Command Prompt
type "cd\"
type "cd python23"
type "python -V"

This will work if you installed Python in C:\Python23\ directory.

Hope that helps.......
This instructions are from Coorae so thank him.
 
also just a minor mistake but Gingerbread Man you have me under status for the 2nd lesson as "Waiting". I finished the second lesson. Just want to clear that up.
 
Blackbird_SR-71 said:
I had the same problem but here is how you solve it:

open Command Prompt
type "cd\"
type "cd python23"
type "python -V"

This will work if you installed Python in C:\Python23\ directory.

Hope that helps.......
This instructions are from Coorae so thank him.

Tried this
cd\ ==> creates a new line with ...
cd python23 ==> gives an error:
file "<stdin>", line 2
cd python23
^
SyntaxError: invalid syntax


PS: tried lesson 2 ==> it works!
but 23%3 is 2 not 1 :hmm: :lol:
 
Top Bottom