You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: try_python/README.md
+94-7Lines changed: 94 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -104,7 +104,7 @@ If you want to get a number of letters in your name, there is a method for that
104
104
>>> len("Ola")
105
105
3
106
106
107
-
Wonder why sometimes you call methods by adding `.` at the end of the string (like `"Ola".upper()`) and sometimes you first call a method and add string in brackets? Well, in some cases, methods belong to objects, like `upper` that can only be performed on Strings. But sometimes, methods don't belong to anything specific and can be used on different types of objects, just like `len`. That's why we're giving `"Ola"` as a parameter to `len` method.
107
+
Wonder why sometimes you call methods by adding `.` at the end of the string (like `"Ola".upper()`) and sometimes you first call a method and add string in parentheses? Well, in some cases, methods belong to objects, like `upper` that can only be performed on Strings. But sometimes, methods don't belong to anything specific and can be used on different types of objects, just like `len`. That's why we're giving `"Ola"` as a parameter to `len` method.
108
108
109
109
### Summary
110
110
@@ -137,27 +137,114 @@ It worked! We used `str` method inside of `len` method. `str` is converting ever
137
137
138
138
As long as we can convert numbers into text, we can't convert text into numbers.
139
139
140
+
## Variables
141
+
142
+
There is a concept in programming: variable. This is nothing more than a name for something so you can use the name rather than the something as you code. Programmers use these variable names to make their code more readable and so they won't forget what something is.
143
+
144
+
Let's say we want to create a new variable called `name`:
145
+
146
+
>>> name = "Ola"
147
+
148
+
You see? It's easy! Simply: name equals Ola.
149
+
150
+
As you've noticed, your program didn't return anything as it did before. How do we now that the variable actually is there? Simply enter `name` and hit Enter:
151
+
152
+
>>> name
153
+
'Ola'
154
+
155
+
Yikes! Your first variable :) You can always change what it means:
156
+
157
+
>>> name = "Sonja"
158
+
>>> name
159
+
'Sonja'
160
+
161
+
You can use it in methods too:
162
+
163
+
>>> len(name)
164
+
5
165
+
166
+
Awesome, right? Of course, variables can be anything, numbers too! Try this:
167
+
168
+
>>> a = 4
169
+
>>> b = 6
170
+
>>> a * b
171
+
24
172
+
173
+
Play with this for a while and see what you can do!
174
+
140
175
## Lists
141
176
142
-
empty list
143
-
list with elements
144
-
methods on lists
177
+
Beside strings and integers, Python has all sorts of different types of objects. Now we're going to introduce another one called __lists__. List is the exact thing you think about now: object that is a list of objects :)
145
178
146
-
## Variables
179
+
Go ahead and create a list:
180
+
181
+
>>> []
182
+
[]
183
+
184
+
Yes, it's empty. Not very useful, right? Let's create a list of lottery numbers. We don't want to repear ourselves all the time, so we will put it in a variable, too:
185
+
186
+
>>> lottery = [3, 42, 12, 19, 30, 59]
187
+
188
+
All right, we have a list! What can we do with it? Let's see how many lottery numbers there is in a list. Do you have an idea which method you should use for that? You've know this already!
189
+
190
+
>>> len(lottery)
191
+
6
192
+
193
+
Yes! `len` can give you a number of objects in a list. Handy, right? Let's sort this list now.
194
+
195
+
>>> lottery.sort()
196
+
197
+
This doesn't return anything... because we didn't print it! Let's try this:
198
+
199
+
>>> print(lottery)
200
+
[3, 12, 19, 30, 42, 59]
201
+
202
+
As you can see, numbers are now sorted from lowest to highest in your list. Congrats!
203
+
204
+
Maybe we want to reverse the order? Let's do that!
205
+
206
+
>>> lottery.reverse()
207
+
>>> print(lottery)
208
+
[59, 42, 30, 19, 12, 3]
209
+
210
+
It worked! If you want to add something to the list, you can do this by typing this command:
211
+
212
+
>>> lottery.append(199)
213
+
>>> print(lottery)
214
+
[59, 42, 30, 19, 12, 3, 199]
215
+
216
+
If you want to only read the first number, you can do this by using index. The first objects of list is an object number 0, next one is 1, and so on. Try this:
217
+
218
+
>>> print(lottery[0])
219
+
59
220
+
>>> print(lottery[1])
221
+
42
222
+
223
+
As you can see, you can access different objects in your list by using its name and index number inside of brackets.
224
+
225
+
You can find a list of all available list methods here in Python documentation: https://docs.python.org/2/tutorial/datastructures.html
147
226
148
227
### Summary
149
228
229
+
Awesome! You know a lot about programming now. In the last part you learned about:
230
+
231
+
-__errors__ - you now know how to read and understand errors that show up if Python doesn't understand your command
232
+
-__variables__ - names for objects that allow to code more easily and make your code more readable
233
+
-__lists__ - list of objects stored in particular order.
0 commit comments