- Code Example 1
- Hello, world!
>>> 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.
Code Example 2 – Maths
>>> 1 + 1 2 >>> 20 + 80 100 >>> 18294 + 449566 467860 (These are additions.) >>> 6 - 5 1 (Subtraction) >>> 2 * 5 10 (Multiply, rabbits!) >>> 5 ** 2 25 (Exponentials; e.g., this one is 5 squared) >>> print "1 + 2 is an addition" 1 + 2 is an addition (The print statement, which writes something onscreen. Notice that 1 + 2 is left unevaluated.) >>> print "One kilobyte is 2^10 bytes, or", 2 ** 10, "bytes." One kilobyte is 2^10 bytes, or 1024 bytes. (You can print sums and variables in a sentence. The commas separating each section are a way of separating clearly different things that you are printing.) >>> 21 / 3 7 >>> 23 / 3 7 (Division; note that Python ignores remainders/decimals.) >>> 23.0 / 3.0 7.666666666666667 (This time, since the numbers are decimals themselves, the answer will be a decimal. >>> 23 % 3 2 >>> 49 % 10 9 (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 to clarify them (because tables look cool, and make you feel smarter ;) ):
Command | Name | Example | Output |
---|---|---|---|
+ | Addition | 4 + 5 | 9 |
- | Subtraction | 8 - 5 | 3 |
* | Multiplication | 4 * 5 | 20 |
/ | Division | 19 / 3 | 6 |
% | Remainder (modulo) | 19 % 3 | 1 |
** | Exponent | 2 ** 4 | 16 |
Remember that thing called order of operations that they taught in maths? Well, it applies in Python, too. Here it is, if you need reminding:
- parentheses ()
- exponents **
- multiplication *, division \, and remainder %
- addition + and subtraction -
Order of Operations [edit]
Here are some examples that you might want to try, if you're rusty on this:
- Code Example 3 – Order of operations
>>> 1 + 2 * 3
7
>>> (1 + 2) * 3
9
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 a 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.
Code Example 4 – Parentheses
>>> 4 - 40 - 3
-39
>>> 4 - (40 - 3)
-33
- Code Example 5 – Comments
>>> #I 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 of it. A hash comments everything after it in the line, and nothing before it. So you could type this:
- Code Example 6 – Comment examples
>>> print "food is very nice" #eat me food is very nice (A normal output, without the smutty comment, thank you very much) >>># print "food is very nice" (Nothing happens, because the code was after a comment) >>> print "food is very nice" eat me (You'll get a fairly harmless error message, because you didn't put your comment after a hash)
WikiMedia CC
No comments:
Post a Comment