Skip to content

Commit 47718ce

Browse files
committed
first commit
0 parents  commit 47718ce

File tree

9 files changed

+1025
-0
lines changed

9 files changed

+1025
-0
lines changed

1.md

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
# 1. Installing Python and using it as a calculator
2+
3+
### Installing Python
4+
5+
Make sure you have read my [introduction to this tutorial](README.md).
6+
7+
The first thing you need to do with Python is to launch it. In this tutorial we're going to use IDLE to get started. More experienced Python users will go on and on about how IDLE sucks, but Python comes with it so we might as well use it.
8+
9+
On Windows, download the latest version of Python 3 from [python.org](https://www.python.org/downloads/). On GNU/Linux, **do not download Python**. You likely have Python 3 installed already and all you need is IDLE. If you're on a Debian based Linux distrubution (such as Ubuntu or Linux Mint) you can use aptitude to install IDLE. Simply open a terminal from a menu, type this command to it and press Enter:
10+
11+
sudo aptitude install idle3
12+
13+
You can also use apt-get if you don't have aptitude:
14+
15+
sudo apt-get install idle3
16+
17+
The terminal will ask for your password. You won't see anything happening as you enter it, that's normal. Just type it and press Enter again. If you mistype it, you'll simply be asked to type it again.
18+
19+
When IDLE is installed, open it from a menu, such as the Start Menu on Windows. It should look like this:
20+
21+
![IDLE](idle.png)
22+
23+
From now on, I'll instead show everything like this, so I don't have to take more screenshots:
24+
25+
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
26+
[GCC 4.8.4] on linux
27+
Type "copyright", "credits" or "license()" for more information.
28+
>>>
29+
30+
The exact content of your Python's welcome message is probably different than mine, it's ok.
31+
32+
The `>>>` in the beginning of the line means that IDLE is ready and you can enter a Python command there. But before you enter anything, just see what happens if you press Enter. It should simply go to the next line.
33+
34+
```py
35+
>>>
36+
>>>
37+
>>>
38+
>>>
39+
```
40+
41+
### Using Python as a calculator
42+
43+
In the interactive `>>>` mode, IDLE will simply echo back everything you type into it. One of the simpliest things you can type are numbers:
44+
45+
```py
46+
>>> 1
47+
1
48+
>>> 2
49+
2
50+
>>> 42
51+
42
52+
>>> -174
53+
-174
54+
>>>
55+
```
56+
57+
Another thing you can do is to type a `#` and then anything you want. Python will not do anything to whatever you write after the `#` character. This is known as commenting.
58+
59+
```py
60+
>>> # This is a comment.
61+
>>> 42 # Do you know where this number comes from?
62+
42
63+
>>>
64+
```
65+
66+
Floating point numbers, also known as floats, are basically decimal numbers. In some countries, decimal numbers are written like 3,14. Separating with a `,` like that doesn't work for making floats in Python, so use a `.` instead. The `,` makes a tuple, we'll talk more about them later in this tutorial.
67+
68+
```py
69+
>>> 3,14
70+
(3, 14)
71+
>>> 3.14
72+
3.14
73+
>>>
74+
```
75+
76+
You can also type mathematical statements. Python will compute the result and echo it back.
77+
78+
```py
79+
>>> 17 + 3
80+
20
81+
>>> 17 - 3
82+
14
83+
>>> 17 * 3
84+
51
85+
>>> 17 / 3
86+
5.666666666666667
87+
>>>
88+
```
89+
90+
Also note that the spaces between numbers and operators are not important. They just make the code easier to read when they are used correctly.
91+
92+
```py
93+
>>> 14 + 2 + 1
94+
17
95+
>>> 14 +2+ 1
96+
17
97+
>>>
98+
```
99+
100+
Calculations are done in the same order as in math. The parenthesis `(` and `)` also work the same way.
101+
102+
```py
103+
>>> 1 + 2 * 3 # 2 * 3 is calculated first
104+
7
105+
>>> (1 + 2) * 3 # 1 + 2 is calculated first
106+
9
107+
>>>
108+
```
109+
Square brackets `[]` and curly brackets `{}` cannot be used instead of parenthesis. Square brackets are for making lists and curly brackets for making sets and dictionaries. Again, all that will be covered later.
110+
111+
```py
112+
>>> [1 + 2] * 3
113+
[3, 3, 3]
114+
>>> {1 + 2} * 3
115+
Traceback (most recent call last):
116+
File "<stdin>", line 1, in <module>
117+
TypeError: unsupported operand type(s) for *: 'set' and 'int'
118+
>>>
119+
```
120+
121+
When you get an error message like this one, don't get scared. Read it instead. It tells you what's wrong and where. In this case it says that we are using `*` with a set and an integer, not two integers. We'll learn more about sets later in this tutorial.
122+
123+
### More advanced math
124+
125+
I decided to include this in my tutorial because some people might be interested in this. **Skip this section if you're not interested.**
126+
127+
The `//` operator will divide and round down to an integer. For example, `17 / 3` is `5.666666666666667`, and so `17 // 3` is `5`.
128+
129+
```py
130+
>>> 17 // 3
131+
5
132+
>>>
133+
```
134+
135+
The `%` operator gets the division remainder.
136+
137+
```py
138+
>>> 17 % 3
139+
2
140+
>>>
141+
```
142+
143+
For example, if there were 17 apples that should be given evenly to 3 people, everyone would get 5 apples and there would be 2 apples left over.
144+
145+
```py
146+
>>> 17 // 3
147+
5
148+
>>> 17 % 3
149+
2
150+
>>>
151+
```
152+
153+
This is also useful for converting time from minutes to seconds. 500 seconds is 8 minutes and 20 seconds.
154+
155+
```py
156+
>>> 500 // 60
157+
8
158+
>>> 500 % 60
159+
20
160+
>>>
161+
```
162+
163+
`**` can be used to raise to a power, so 3² in math is `3**2` in Python. Powers are calculated before `*` and `/`, but after `()`.
164+
165+
```py
166+
>>> 3 ** 2
167+
9
168+
>>> 2 * 3 ** 4 # 3 ** 4 is calculated first
169+
162
170+
>>> (2 * 3) ** 4 # 2 * 3 is calculated first
171+
1296
172+
>>>
173+
```
174+
175+
The `math` module contains more useful things.
176+
177+
```py
178+
>>> from math import pi, sqrt
179+
>>> pi
180+
3.141592653589793
181+
>>> sqrt(2)
182+
1.4142135623730951
183+
>>>
184+
```
185+
186+
Python uses radians instead of degrees, so degrees need to be converted to radians first. Of course, sin 30° is supposed to be ½ instead of 0.49999999999999994. These issues are known as floating point errors, but most of the time they don't matter any.
187+
188+
```py
189+
>>> from math import sin, radians
190+
>>> sin(radians(30))
191+
0.49999999999999994
192+
>>>
193+
```
194+
195+
See the [offical documentation of the math module](https://docs.python.org/3/library/math.html) for more info about it.
196+
197+
### Exercises
198+
199+
1. A company needs to buy new computers and monitors. With taxes, a new monitor costs 49,95 € and a new computer costs 200 €. How much would 100 computers cost without taxes, when the taxes are 24 % of the price?
200+
2. The volume of a sphere with radius r is 4/3 π r³. What is the volume of a sphere with radius 5? (taken from the end of [this](http://www.greenteapress.com/thinkpython/html/thinkpython003.html) page)
201+
202+
The answers are [here](answers.md).
203+
204+
[Next](2.md) | [Home](README.md)

0 commit comments

Comments
 (0)