@@ -47,8 +47,8 @@ That worked. How about numbers?
4747
4848There we go, it echoes them back.
4949
50- In some countries, decimal numbers are written with a comma, like ` 3,14 `
51- instead of ` 3.14 ` . Maybe Python knows that?
50+ In some countries, decimal numbers are written like ` 3,14 ` instead of
51+ ` 3.14 ` . Maybe Python knows that?
5252
5353``` python
5454>> > 3 ,14
@@ -60,65 +60,70 @@ We didn't get an error... but `(3, 14)` is not at all what we expected!
6060So from now on, let's use a dot with decimal numbers, because ` 3.14 `
6161worked just fine. Later we'll learn what ` (3, 14) ` is.
6262
63+ Python also supports simple math:
64+
65+ ``` python
66+ >> > 3 + 2
67+ 5
68+ >> > 3 - 2
69+ 1
70+ >> > 3 * 2
71+ 6
72+ >> > 3 / 2
73+ 1.5
74+ >> >
75+ ```
76+
6377## Comments
6478
65- ** Comments are text that does nothing.** They can be created by typing a
66- ` # ` and then some text after it, and they are useful when our code would
67- be hard to understand without them.
79+ ** Comments are text that does nothing.** Everything after a ` # ` is ignored.
80+ Like this:
6881
6982``` python
7083>> > 1 + 2 # can you guess what the result is?
71843
7285>> >
7386```
7487
75- Again, I put a space after the ` # ` and multiple spaces before it just to
76- make things easier to read.
88+ Comments are useful for explaining something to people reading the code.
7789
78- If we write a comment on a line with no code on it, the prompt changes
79- from ` >>> ` to ` ... ` . To be honest, I have no idea why it does that and I
80- think it would be better if it would just stay as ` >>> ` . The prompt goes
81- back to ` >>> ` when we press Enter again.
90+ The interactive prompt does this if you enter a comment on a line with
91+ nothing else on it:
8292
8393``` python
8494>> > # hello there
8595...
8696>> >
8797```
8898
99+ I have no idea why this happens and I think it would be better if the
100+ prompt would just stay as ` >>> ` . Press Enter twice to get the prompt
101+ back to ` >>> ` .
102+
89103## Strings
90104
91105Strings are small pieces of text that we can use in our programs. We can
92- create strings by simply writing some text in quotes.
106+ create strings like this:
93107
94108``` python
95109>> > ' hello'
96110' hello'
97111>> > ' this is a test'
98112' this is a test'
99- >> >
100- ```
101-
102- Strings can also be written with "double quotes" instead of 'single
103- quotes'. This is useful when we need to put quotes inside the string.
104-
105- ``` python
106- >> > " hello there"
107- ' hello there'
108- >> > " it's sunny"
109- " it's sunny"
110- >> >
113+ >> > ' string with a # in it'
114+ ' string with a # in it'
115+ >> > ' test'
116+ ' test'
117+ >> > " test"
118+ ' test'
111119```
112120
113- It's also possible to add single quotes and double quotes into the same
114- string, but most of the time we don't need to do that so I'm not going
115- to talk about it now.
116-
117- It doesn't matter which quotes you use when the string doesn't need to
118- contain any quotes. If you think that one of the quote types looks nicer
119- than the other or you find it faster to type, go ahead and use that.
121+ It doesn't matter if you use 'single quotes' or "double quotes". Python
122+ prefers single quotes when displaying strings to us, but it understands
123+ both quote styles. Double quotes are useful for strings that contain
124+ single quotes, like ` "it's weird" ` .
120125
121- Strings can be joined together easily with ` + ` or repeated with ` * ` :
126+ You can also do this :
122127
123128``` python
124129>> > " hello" + " world"
@@ -128,75 +133,6 @@ Strings can be joined together easily with `+` or repeated with `*`:
128133>> >
129134```
130135
131- Note that a ` # ` inside a string doesn't create a comment.
132-
133- ``` python
134- >> > " strings can contain # characters"
135- ' strings can contain # characters'
136- >> >
137- ```
138-
139- ## Using Python as a calculator
140-
141- ``` diff
142- - --------- WARNING: This part contains boring math. Proceed with caution. ----------
143- ```
144-
145- Let's type some math stuff into Python and see what it does.
146-
147- ``` python
148- >> > 17 + 3
149- 20
150- >> > 17 - 3
151- 14
152- >> > 17 * 3
153- 51
154- >> > 17 / 3
155- 5.666666666666667
156- >> >
157- ```
158-
159- It's working, Python just calculates the result and echoes it back.
160-
161- I added a space on both sides of ` + ` , ` - ` , ` * ` and ` / ` . Everything would
162- work without those spaces too:
163-
164- ``` python
165- >> > 4 + 2 + 1
166- 7
167- >> > 4 + 2 + 1
168- 7
169- >> >
170- ```
171-
172- However, I recommend always adding the spaces because they make the code
173- easier to read.
174-
175- Things are calculated in the same order as in math. The parentheses ` ( `
176- and ` ) ` also work the same way.
177-
178- ``` python
179- >> > 1 + 2 * 3 # 2 * 3 is calculated first
180- 7
181- >> > (1 + 2 ) * 3 # 1 + 2 is calculated first
182- 9
183- >> >
184- ```
185-
186- You can also leave out spaces to show what's calculated first. Python
187- ignores it, but our code will be easier to read for people.
188-
189- ``` python
190- >> > 1 + 2 * 3 # now it looks like 2*3 is calculated first
191- 7
192- >> >
193- ```
194-
195- Python also supports many other kinds of calculations, but most of the
196- time you don't need them. Actually you don't need even these
197- calculations most of the time, but these calculations are probably
198- enough when you need to calculate something.
199-
200136## Summary
201137
202138[ comment ] : # ( the first line in this summary is exactly same as in )
0 commit comments