Python Forum
[Basic] The ‘Pythonic’ Python Tutorial!
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
Reply
#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
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.
Reply
#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.
Reply
#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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
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'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Basic] Basic Python Tutorial (Playlist) coffeejunkie34 0 783 Sep-28-2023, 12:18 PM
Last Post: coffeejunkie34
  Is there a good tutorial on installing Python correctly on Windows? CynthiaMoore 4 2,037 Aug-31-2022, 12:47 PM
Last Post: snippsat
  Complete Python Tutorial jamessidis 2 5,214 Dec-04-2020, 07:48 PM
Last Post: khalifa
  [Basic] Python Tutorial for Beginners MK_CodingSpace 0 2,168 Dec-03-2020, 09:43 PM
Last Post: MK_CodingSpace
  Python Scrapy Tutorial Knight18 0 3,905 Oct-19-2020, 04:56 PM
Last Post: Knight18
  12 mini games python tutorial series ninedeadeyes 2 2,371 Feb-16-2020, 11:18 PM
Last Post: ninedeadeyes
  [Basic] Python Tutorial for Beginners in One Lesson (98 minutes) artur_m 0 3,989 Jan-23-2020, 09:15 AM
Last Post: artur_m
  Complete Python 3 Tutorial Series KodeBlog 0 5,144 Sep-06-2019, 10:00 AM
Last Post: KodeBlog
  Tutorial for Python, Flask > data into a form Ecniv 1 2,372 May-01-2019, 07:49 PM
Last Post: Larz60+
  Python tutorial on learning & creating your own LRU Cache :) ncorbuk 0 3,442 Jan-26-2019, 08:28 PM
Last Post: ncorbuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020