Skip to content

Commit

Permalink
running strip.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Jan 17, 2017
1 parent 375a8f6 commit c8e3a07
Show file tree
Hide file tree
Showing 21 changed files with 337 additions and 337 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The tutorial consists of two sections:

### Basics

This section will get you started with using Python and you'll be able
This section will get you started with using Python and you'll be able
to learn more about whatever you want after studying it.

1. [What is programming?](basics/what-is-programming.md)
Expand All @@ -51,14 +51,14 @@ to learn more about whatever you want after studying it.

### Advanced

If you want to learn more advanced techniques, you can also read this
section. Most of the techniques explained here are great when you're
working on a large project, and your code would be really repetitive
If you want to learn more advanced techniques, you can also read this
section. Most of the techniques explained here are great when you're
working on a large project, and your code would be really repetitive
without these things.

You can experient with these things freely, but please **don't use these
techniques just because you know how to use them.** Prefer the simple
techniques from the Basics part instead when possible. Simple is better
You can experient with these things freely, but please **don't use these
techniques just because you know how to use them.** Prefer the simple
techniques from the Basics part instead when possible. Simple is better
than complex.

1. [Advanced stuff with functions](advanced/functions.md)
Expand Down
86 changes: 43 additions & 43 deletions advanced/iterators.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ So far we have used for loops with many different kinds of things.
```py
>>> for name in ['theelous3', 'RubyPinch', 'go|dfish']:
... print(name)
...
...
theelous3
RubyPinch
go|dfish
>>> for letter in 'abc':
... print(letter)
...
...
a
b
c
>>>
>>>
```

For looping over something is one way to **iterate** over it. Some other
things also iterate, for example, `' '.join(['a', 'b', 'c'])` iterates
over the list `['a', 'b', 'c']`. If we can for loop over something, then
that something is **iterable**. For example, strings and lists are
For looping over something is one way to **iterate** over it. Some other
things also iterate, for example, `' '.join(['a', 'b', 'c'])` iterates
over the list `['a', 'b', 'c']`. If we can for loop over something, then
that something is **iterable**. For example, strings and lists are
iterable, but integers and floats are not.

```py
>>> for thing in 123:
... print(thing)
...
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>>
>>>
```

## Iterators
Expand All @@ -42,36 +42,36 @@ Lists and strings don't change when we iterate over them.
>>> word = 'hi'
>>> for character in word:
... print(character)
...
...
h
i
>>> word
'hello'
>>>
>>>
```

We can also iterate over [files](../basics/files.md), but they change
when we do that. They remember their position, so if we iterate over
We can also iterate over [files](../basics/files.md), but they change
when we do that. They remember their position, so if we iterate over
them twice we get the content once only.

```py
>>> with open('test.txt', 'w') as f:
... print("one", file=f)
... print("two", file=f)
...
...
>>> a = []
>>> b = []
>>> with open('test.txt', 'r') as f:
... for line in f:
... a.append(line)
... for line in f:
... b.append(line)
...
...
>>> a
['one\n', 'two\n']
>>> b
[]
>>>
>>>
```

We have also used [enumerate](../basics/trey-hunner-zip-and-enumerate.md)
Expand All @@ -81,21 +81,21 @@ before, and it actually remembers its position also:
>>> e = enumerate('hello')
>>> for pair in e:
... print(pair)
...
...
(0, 'h')
(1, 'e')
(2, 'l')
(3, 'l')
(4, 'o')
>>> for pair in e:
... print(pair)
...
>>>
...
>>>
```

Iterators are **iterables that remember their position**. For example,
`open('test.txt', 'r')` and `enumerate('hello')` are iterators.
Iterators can only be used once, so we need to create a new iterator if
Iterators are **iterables that remember their position**. For example,
`open('test.txt', 'r')` and `enumerate('hello')` are iterators.
Iterators can only be used once, so we need to create a new iterator if
we want to do another for loop.

Here's a picture that hopefully explains this better:
Expand All @@ -104,8 +104,8 @@ Here's a picture that hopefully explains this better:

## Iterating manually

Iterators have a magic method called `__next__`, and there's a built-in
function called `next()` for calling that. Calling `next()` on an
Iterators have a magic method called `__next__`, and there's a built-in
function called `next()` for calling that. Calling `next()` on an
iterator gets the next value and moves it forward. Like this:

```py
Expand All @@ -116,7 +116,7 @@ iterator gets the next value and moves it forward. Like this:
(1, 'b')
>>> e.__next__()
(2, 'c')
>>>
>>>
```

There's also a built-in `next()` function that does the same thing:
Expand All @@ -129,26 +129,26 @@ There's also a built-in `next()` function that does the same thing:
(1, 'b')
>>> next(e)
(2, 'c')
>>>
>>>
```

Here `e` remembers its position, and every time we call `next(e)` it
gives us the next element and moves forward. When it has no more values
Here `e` remembers its position, and every time we call `next(e)` it
gives us the next element and moves forward. When it has no more values
to give us, calling `next(e)` raises a StopIteration:

```py
>>> next(e)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>
>>>
```

There is usually not a good way to check if the iterator is at the end,
and it's best to just try to get an value from it and catch
There is usually not a good way to check if the iterator is at the end,
and it's best to just try to get an value from it and catch
StopIteration.

This is actually what for looping over an iterator does. For example,
This is actually what for looping over an iterator does. For example,
this code...

```py
Expand All @@ -170,24 +170,24 @@ while True:
print(pair)
```

The for loop version is much simpler to write and I wrote the while loop
The for loop version is much simpler to write and I wrote the while loop
version just to explain what the for loop does.

## Converting to iterators

Now we know what iterating over an iterator does. But how about
iterating over a list or a string? They are not iterators, so we can't
Now we know what iterating over an iterator does. But how about
iterating over a list or a string? They are not iterators, so we can't
call `next()` on them:

```py
>>> next('abc')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not an iterator
>>>
>>>
```

There's a built-in function called `iter()` that converts anything
There's a built-in function called `iter()` that converts anything
iterable to an iterator.

```py
Expand All @@ -204,7 +204,7 @@ iterable to an iterator.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>
>>>
```

Calling `iter()` on anything non-iterable gives us an error.
Expand All @@ -214,17 +214,17 @@ Calling `iter()` on anything non-iterable gives us an error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>>
>>>
```

If we try to convert an iterator to an iterator using `iter()` we just
If we try to convert an iterator to an iterator using `iter()` we just
get back the same iterator.

```py
>>> e = enumerate('abc')
>>> iter(e) is e
True
>>>
>>>
```

So code like this...
Expand All @@ -248,8 +248,8 @@ while True:

## Custom iterables

Implementing a custom iterator is easy. All we need to do is to define a
`__next__` method that gets the next element, and an `__iter__` method
Implementing a custom iterator is easy. All we need to do is to define a
`__next__` method that gets the next element, and an `__iter__` method
that returns the iterator itself. For example, here's an iterator that
behaves like `iter([1, 2, 3])`:

Expand Down
2 changes: 1 addition & 1 deletion advanced/magicmethods.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ follow one of these styles:
## Other magic methods

There are many more magic methods, and I don't see any reason to list
them all here. [The official
them all here. [The official
documentation](https://docs.python.org/3/reference/datamodel.html) has
more information about magic methods if you need it. We'll go through
using the most important magic methods in the rest of this tutorial, so
Expand Down
16 changes: 8 additions & 8 deletions basics/answers.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ isn't exactly like mine but it works just fine it's ok, and you can
2. If we have a look at `help(str.upper)` it looks like this:

S.upper() -> str

Return a copy of S converted to uppercase.

We have two problems. First of all, the broken code uses
Expand Down Expand Up @@ -324,11 +324,11 @@ isn't exactly like mine but it works just fine it's ok, and you can
```py
>>> for pair in zip('ABC', 'abc'):
... print(pair)
...
...
('A', 'a')
('B', 'b')
('C', 'c')
>>>
>>>
```

Great, it works just like it works with lists. Now let's create
Expand Down Expand Up @@ -389,7 +389,7 @@ isn't exactly like mine but it works just fine it's ok, and you can
def ask_name():
name = input("Enter your name: ")
return name

print("Your name is", ask_name())
```

Expand All @@ -403,7 +403,7 @@ isn't exactly like mine but it works just fine it's ok, and you can
```py
def get_greeting():
return "Hello World!"

print(get_greeting())
```

Expand All @@ -414,7 +414,7 @@ isn't exactly like mine but it works just fine it's ok, and you can
```py
>>> greet("World")
Hello World
>>>
>>>
```

But it also returns None because we don't tell it to return anything else.
Expand All @@ -424,7 +424,7 @@ isn't exactly like mine but it works just fine it's ok, and you can
Hello World
>>> print(return_value)
None
>>>
>>>
```

This code from the exercise does the same thing as the code above
Expand All @@ -434,7 +434,7 @@ isn't exactly like mine but it works just fine it's ok, and you can
>>> print(greet("World"))
Hello World
None
>>>
>>>
```

***
Expand Down
Loading

0 comments on commit c8e3a07

Please sign in to comment.