Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Basic] The ‘Pythonic’ Python Tutorial!
#1
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
Quote
#2
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
micseydel likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#3
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.
Quote
#4
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.
Quote
#5
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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  12 mini games python tutorial series ninedeadeyes 2 103 Feb-16-2020, 11:18 PM
Last Post: ninedeadeyes
  [Basic] Python Tutorial for Beginners in One Lesson (98 minutes) artur_m 0 352 Jan-23-2020, 09:15 AM
Last Post: artur_m
  Complete Python 3 Tutorial Series KodeBlog 0 245 Sep-06-2019, 10:00 AM
Last Post: KodeBlog
  Tutorial for Python, Flask > data into a form Ecniv 1 375 May-01-2019, 07:49 PM
Last Post: Larz60+
  Python tutorial on learning & creating your own LRU Cache :) ncorbuk 0 1,250 Jan-26-2019, 08:28 PM
Last Post: ncorbuk
  [split] Python tutorial for beginner's swati10 1 930 Sep-11-2018, 04:24 PM
Last Post: micseydel
  Proposed Tutorial: Introduction to (Pseudo) Random Numbers and Python ljmetzger 1 2,253 Aug-08-2018, 11:05 AM
Last Post: Siddharth1989
  Python tutorial for beginner's kuhak8 3 1,656 May-29-2018, 06:59 PM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)