0% found this document useful (0 votes)
3 views5 pages

Python Basics: IDLE, Math, and Comments

This document serves as a beginner's guide to programming in Python, starting with the installation of IDLE and running simple code examples. It covers basic math operations, the order of operations, and the importance of comments in code. The document concludes with a simple program example demonstrating the use of loops and string manipulation.

Uploaded by

daywart.22
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Python Basics: IDLE, Math, and Comments

This document serves as a beginner's guide to programming in Python, starting with the installation of IDLE and running simple code examples. It covers basic math operations, the order of operations, and the importance of comments in code. The document concludes with a simple program example demonstrating the use of loops and string manipulation.

Uploaded by

daywart.22
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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. Let's give it a go.

Contents

 1Opening IDLE
 2Math in Python
 3Order of Operations
 4Comments, Please

Opening IDLE
Run the program labelled IDLE (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 line and press Enter. Don't type the >>> part, it will already be there.
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. This is useful for
testing things, e.g., defining a few variables, and then testing to see if a certain line will work.
That will come in a later lesson though.

Math in Python
Now try the following examples. I've given explanations in parentheses.
Code Example 2 – Maths

>>> 1 + 1
2

>>> 20 + 80
100

>>> 18294 + 449566


467860
(These are additions.)

>>> 6 - 5
1
(Subtraction)
>>> 2 * 5
10
(Multiplication)

>>> 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.
Table 1 – Python operators
Command Name Example Output
+ Addition 4+5 9
- Subtraction 8-5 3
* Multiplication 4 * 5 20
/ Division 19 / 3 6
Remainder
% 19 % 3 1
(modulo)
** 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:

1. parentheses ()
2. exponents **
3. multiplication *, 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:
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.
Also remember that the math is calculated from left to right, unless you put in parentheses. The
innermost parentheses are calculated first. Watch these examples:
Code Example 4 – Parentheses

>>> 4 - 40 - 3
-39
>>> 4 - (40 - 3)
-33

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.

Comments, Please
The final thing you'll need to know to move on to multi-line programs is the comment. You
should always add comments to code to show others who might be reading your code what
you've done and why. Type the following (and yes, the output is shown):
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


File "<stdin>", line 1
print("food is very nice") eat me
^
SyntaxError: invalid syntax

(You'll 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 out bits of code if you don't want them to compile, but
can't delete them because you might need them later.
Simple program example

>>> a = [1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1]
>>> b = [' ' * 2 * (7 - i) + 'very' * i for i in a]
>>> for line in b:
print(line)

Here multiplication and adding operations have been used. The first line а = [1, 2, 3, 4, 5, 6, 7, 6,
5, 4, 3, 2, 1] reflects values for a parameter i in the second line (for i in a). If we set “1” instead of
“i” for a parameter b we will see that “space” is multiplied for 12 and “very” is multiplied for “1”.
So addition operator “+” unites 12 “spaces” and one word “very” which we can see in the first
printed line. “for line in b: print(line)” is a cycle aimed at displaying required results.

very
veryvery
veryveryvery
veryveryveryvery
veryveryveryveryvery
veryveryveryveryveryvery
veryveryveryveryveryveryvery
veryveryveryveryveryvery
veryveryveryveryvery
veryveryveryvery
veryveryvery
veryvery
very

Common questions

Powered by AI

If comments are not properly implemented in Python, such as missing the hash (#) symbol, the intended comment line may produce syntax errors as Python tries to execute it as code. For instance, an attempt to comment without the hash would lead to a syntax error if there is a print statement immediately after it .

Python evaluates expressions from left to right, which can lead to unexpected results in expressions without parentheses prioritizing operations, particularly with mixed operators of the same precedence. For example, in '4 - 40 - 3', both subtractions are performed left to right, leading to -39 rather than a potentially expected result if calculated in a different sequence .

The print function in Python can dynamically display outputs by incorporating variables and calculations directly into print statements. For example, using commas ',' to separate variables and expressions allows outputs like 'print("One kilobyte is 2^10 bytes, or", 2 ** 10, "bytes.")' to dynamically calculate and display '1024 bytes' as part of the string output. This flexibility is useful for creating informative outputs that reflect real-time calculations .

Python uses a specific order of operations similar to standard mathematics: parentheses () have the highest precedence, followed by exponents **, then multiplication *, division /, and remainder %, and finally addition + and subtraction -. Parentheses can be used to override this natural precedence by enclosing the parts of the expression that should be evaluated first .

The modulo operation in Python, represented by '%', returns the remainder of a division of two numbers. It's beneficial in scenarios like determining if a number is even or odd (n % 2), creating cyclic behaviors in algorithms, or for calendar calculations where cycles are based on a fixed number of units. For instance, '49 % 10' yields 9, the remainder when dividing 49 by 10 .

List comprehensions in Python allow for concise and readable creation and transformation of lists. In the example of generating a pattern of spaces and the word 'very', the comprehension '[ ' ' * 2 * (7 - i) + 'very' * i for i in a]' efficiently creates a series of strings by calculating spaces and repetitions based on list 'a'. This demonstrates how comprehensions can be used to generate complex patterns with minimal code .

Exponentiation in Python, represented by '**', can model a variety of real-world problems requiring power calculations, such as calculating compound interest in finance, growth patterns in biology, or energy consumption scaling in physics. It efficiently handles any expressions where variables or fixed numbers are raised to a power .

One-liner programs in Python serve as effective educational tools by illustrating programming concepts concisely and engagingly. They allow learners to test specific code snippets quickly without the complexity of larger programs. This method helps in understanding functions, arithmetic operations, and syntax through immediate results, enhancing learning retention .

IDLE, which stands for Integrated Development Environment, provides an environment where beginners can write and execute Python code easily. It allows users to run one-liner programs to quickly see outputs, making it suitable for testing and learning. Such immediate feedback is essential for beginners to understand programming and debug effectively .

Comments in Python are crucial for providing explanations or descriptions of code for anyone who may read it, making the code more understandable. This is particularly useful for collaboration, debugging, and future modifications. Comments are implemented by placing a hash (#) in front of the text, which tells Python to ignore the rest of the line .

You might also like