@@ -47,8 +47,8 @@ That worked. How about numbers?
47
47
48
48
There we go, it echoes them back.
49
49
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?
52
52
53
53
``` python
54
54
>> > 3 ,14
@@ -60,65 +60,70 @@ We didn't get an error... but `(3, 14)` is not at all what we expected!
60
60
So from now on, let's use a dot with decimal numbers, because ` 3.14 `
61
61
worked just fine. Later we'll learn what ` (3, 14) ` is.
62
62
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
+
63
77
## Comments
64
78
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:
68
81
69
82
``` python
70
83
>> > 1 + 2 # can you guess what the result is?
71
84
3
72
85
>> >
73
86
```
74
87
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.
77
89
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:
82
92
83
93
``` python
84
94
>> > # hello there
85
95
...
86
96
>> >
87
97
```
88
98
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
+
89
103
## Strings
90
104
91
105
Strings 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:
93
107
94
108
``` python
95
109
>> > ' hello'
96
110
' hello'
97
111
>> > ' this is a test'
98
112
' 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'
111
119
```
112
120
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" ` .
120
125
121
- Strings can be joined together easily with ` + ` or repeated with ` * ` :
126
+ You can also do this :
122
127
123
128
``` python
124
129
>> > " hello" + " world"
@@ -128,75 +133,6 @@ Strings can be joined together easily with `+` or repeated with `*`:
128
133
>> >
129
134
```
130
135
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
-
200
136
## Summary
201
137
202
138
[ comment ] : # ( the first line in this summary is exactly same as in )
0 commit comments