Python Forum

Full Version: [Basic] The ‘Pythonic’ Python Tutorial!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I know there are plenty of freely available Python tutorials out there, but I thought I'd create one that's more 'Pythonic', i.e. succinct and easy to follow. Any constructive feedback is appreciated:

The Pythonic Python Tutorial
https://github.com/the-machine-preacher/...n-Tutorial
Succinct and easy to follow is not enough. Information provided must be correct as well. Therefore my feedback not very positive. It's good to put effort into writing tutorial but result is not ready for distribution.

I randomly opened one chapter (Lists) and read:

Quote:A list is an ordered set of objects in Python.

Hmm.... 'ordered set of objects'? As set is itself datatype of Python ('A set object is an unordered collection of distinct hashable objects.') meaning of this definition is: ordered unordered /blablabla/.

No good. Stick to the definition provided in documentation: "Lists are mutable sequences"

So I went to check next definition (Tuples) and read:

Quote:A tuple is a collection which is ordered and unchangeable. Tuples are similar to lists except that their elements are immutable. Once a tuple is created, the elements, the order of the elements & number of elements can't be changed.

Tuple definition in documentation: 'Tuples are immutable sequences'

Immutable and unchangeable are not the same:

>>> t = ([1, 2, 3], [3, 5, 6])          # tuple with two mutable elements (contrary to definition in tutorial)
>>> t[1].append(7)                      # change second element  (contrary to definition in tutorial)
>>> t                                   # both element in tuple itself has changed (contrary to definition in tutorial)
([1, 2, 3], [3, 5, 6, 7])
From documentation:

Quote:The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable. (The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.)

I didn't look any further, but please correct these definitions at least. I recommend to read following sections of Python documentation:

Set types - set, frozenset
Lists
Tuples
Datamodel - Objects, values and types
Mistakes happen especially on the first release of things. I bet it'll get better with some support from people who can pinpoint errors and mistakes. Apparently lots of efforts went into it.

I appreciate the effort to share free information. Keep it up.
in addition to @perfringo's comment - even if set was not distinct type, set in math is collection of distinct objects, i.e. no repetitions. Using term set for list definition suggests there could be no repeating elements, which is not the case.
https://github.com/the-machine-preacher/...iles.ipynb

In Cell 8:

with open('Examples/how_many_lines.txt') as lines_doc:
    for line in lines_doc.readlines():
        print(line)
It should be:

with open('Examples/how_many_lines.txt') as lines_doc:
    for line in lines_doc:
        print(line)
The example from Cell 8 opens the file,
then it iterates over all lines with readlines().
The list object is created in memory and holds the complete content of the file.

The correction of it, don't use readlines().
Iterating over a file, yields line by line without loading the whole content into memory.
The first function could not process a file, which is bigger as your RAM + Swap.


Another cool trick is the use of itertools.islice.
If you want to print the first 10 lines:

from itertools import islice


def head(file, lines):
    with open(file) as fd:
        for line in islice(fd, 0, lines):
            print(line.strip())


head(r'C:\Windows\system.ini', 10)
Tail have to be implemented different.
from collections import deque                
                                             
                                             
def tail(file, lines):                       
    result = deque(maxlen=lines)             
    with open(file) as fd:                   
        for line in fd:                      
            result.append(line.strip())      
    for line in result:                      
        print(line)                          

                                    
tail(r'C:\Windows\system.ini', 10)           
This is not optimal, because it has to read the whole file content.
As a beginner, it was quite interesting for me to study this material; I have not yet looked through everything.
I hope you take into account the comments given here and improve your 'Pythonic'.