Skip to content

Commit 4b883f4

Browse files
committed
Python Lesson 1.
1 parent cd56231 commit 4b883f4

1 file changed

Lines changed: 115 additions & 2 deletions

File tree

python/lesson1/tutorial.md

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,121 @@ layout: page
33
title: Introduction to Python
44
---
55

6-
#Introduction to Python
6+
# Introduction to Python
77

8-
There is a very good introduction article in [Google Developers Guide](https://developers.google.com/edu/python/introduction)
8+
Welcome to the first Python tutorial. We're going to look at getting Python up and running on your computer, then trying a few simple operations to get to grips with the development environment, the syntax and basic computation.
9+
10+
## Installation
11+
12+
First thing, install Python.
13+
14+
On Mac, you already have Python.
15+
16+
On Windows:
17+
18+
1. Head to [the Python website](https://www.python.org/).
19+
2. Click "Downloads".
20+
3. There will be two options; one for Python 3 and one for Python 2. **Download Python 2.** At the time of writing, the version available is 2.7.10.
21+
4. Run the downloaded installer and click *Next* until you reach the *Customization* page.
22+
5. Scroll down to the bottom and choose to *Add python.exe to PATH*.
23+
6. Continue clicking *Next* until it's installed.
24+
25+
## Opening the REPL
26+
27+
Python is often developed with the aid of a *REPL*, or *Read-Eval-Print-Loop*. The REPL is a way of getting immediate feedback as you work.
28+
29+
Python's REPL is called **IDLE**.
30+
31+
On Windows, you can run it by clicking on the shortcut in your Start menu or Start screen named "IDLE (Python GUI)".
32+
33+
On Mac OS, open the Terminal and type "python" to start the REPL. At the top, you'll see something similar (but probably not identical) to this:
34+
35+
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
36+
37+
If you see it, make sure you're running Python 2, not Python 3, otherwise parts of this exercise will not make sense. Once you've checked that, we're ready to move on to writing Python.
38+
39+
## Hello, World!
40+
41+
In keeping with tradition, we're going start by printing "Hello, World!" to the console. In Python, the command to achieve this is aptly named `print`. Type the following:
42+
43+
print "Hello, World!"
44+
45+
The REPL will simply print the text right back at you.
46+
47+
Now, print your name.
48+
49+
## Maths
50+
51+
Everybody's favourite pastime.
52+
53+
### Simple Arithmetic
54+
55+
Python can do simple arithmetic. To start, let's try addition:
56+
57+
5 + 7
58+
59+
You should now see the result of that calculation in your REPL.
60+
61+
Subtraction, multiplication and division work the same way.
62+
63+
6 - 2
64+
8 * 4
65+
9 / 3
66+
67+
Now try a few more to see what you get.
68+
69+
Let's see what happens when you divide 14 by 4. Notice the remainder is chopped off. We can use the *modulus* operator, `%`, to get the remainder instead. Try `14 % 4` and see.
70+
71+
### Combining Operations
72+
73+
Being able to only perform one operation at a time is pretty limiting, so Python allows us to combine mathematical operations. Try this one:
74+
75+
9 * 4 - 6
76+
77+
Now try a few more. You can combine as many operations as you like.
78+
79+
### Operator Precedence
80+
81+
Did you notice any unexpected results when you started combining operations? If you didn't, try this:
82+
83+
10 - 2 * 4
84+
85+
Python follows the traditional mathematical rules of precedence, which state that multiplication and division are done before addition and subtraction. (You may remember what *BODMAS* means.) This means in our example above, 2 and 4 are multiplied first, and then the result is subtracted from 10.
86+
87+
We can change the order of operations by using parentheses. Anything inside parentheses is executed first.
88+
89+
Now try it like this:
90+
91+
(10 - 2) * 4
92+
93+
You should have a different answer.
94+
95+
Because of precedence rules, complex operations such as our first example can be quite confusing to read. If you find yourself writing more complex expressions, there is no harm in adding parentheses for clarity.
96+
97+
### Decimal Points
98+
99+
Remember dividing 14 by 4? The result probably surprised you. Fortunately, there is a way to convince Python that you really do want the full answer, not just the whole number. We can do this by *starting* with numbers with decimal points:
100+
101+
14.0 / 4.0
102+
103+
Now try a few more and see how it goes. Remember that you can also use non-whole numbers in your calculations.
104+
105+
You may notice that there are still some limitations. For example, try:
106+
107+
10.0 / 3.0
108+
109+
The answer should go on forever, but it doesn't. This is because there is only a finite amount of space to store the number, and it runs out after approximately 15 significant figures.
110+
111+
### Concluding
112+
113+
Now let's combine what we have learnt today. We can tell `print` to print multiple things at once, separated by a space:
114+
115+
print 'The result of 2 + 2 is', 2 + 2
116+
117+
This concludes today's tutorial. In the next tutorial, we'll find out how to combine the results of multiple separate expressions using variables, get input from the user, and make decisions based on that information.
118+
119+
### Further Reading
120+
121+
There is a very good introductory article in [Google Developers Guide](https://developers.google.com/edu/python/introduction)
9122

10123
You can also find resources for beginners on [the Python website](https://www.python.org/about/gettingstarted/) and refer to [the Python documentation](https://docs.python.org/2/tutorial/introduction.html), where the language basics are explained.

0 commit comments

Comments
 (0)