r/learnpython • • Feb 20 '21

What are you having difficulty learning or are currently struggling to learn?

I'm curious what the community here has difficulty with when it comes to learning Python. Partly because I want to see if the more experienced community here (or myself) can help point you into the right direction. . . but also because I'm trying to come up with video tutorial ideas for future content and would love to hear from Python learners what would be most useful to dedicate our time on.

I'm looking forward to hearing what you guys have to say!

417 Upvotes

324 comments sorted by

View all comments

4

u/barryhakker Feb 21 '21

I’d love to see real life examples of concepts (mostly looking at you, OOP). This came up in a thread earlier but for some topics there are great practical tutorials that show you real life applications (e.g. a Flask tutorial where you build an actual website with tons of functions) whereas when it comes to OOP the examples come down to “YOU CAN MAKE CLASS DOG, DOG GO WOOF!” or if you dig a bit more you always get to the RPG example. So what if you don’t care about making an RPG? No other examples? There are of course and I remember seeing a video that used something like a map app as an example but those are rare. More examples of people using it in real life would be great.

2

u/Ne0_1 Feb 21 '21

I have been doing the MIT OCW courses myself with great success. The problem sets are challenging and fun to build. I recommend starting with the 6.00SC course. Specifically, for OOP psets 6, 7, and 8 are great and you learn how to incorporate OOP. I have never had success just watching tutorials. I need to write my own code to learn and incorporate the ideas I learned. They also go over Python implementations as well.

0

u/[deleted] Feb 21 '21

[deleted]

2

u/MaraudingAvenger Feb 21 '21 edited Feb 21 '21

For recursion -

I've got an awful API that gives me nested dictionaries of arbitrary depth. I need to get at the value in:

d = { key : { key : { key : { key : value }}}}

the next one I get might be:

d = { key : { key : { key : { key : { key : value }}}}}

if I just call dict.get recursively until there's no .get method, then return that thing, that grabs the value:

def get_val(d):
    if not hasattr(d, "get"): # if it ain't a dict
        return d # gimme that
    for k in d: # otherwise
        return get_val(d.get(k)) # run this func on the thing inside
    
get_val(d)

There are nested things, and there are even recursive data structures (check out Trie structure implementations, they're kind of neat) -- in some ways, doing things like a recursive get is a bit easier than trying to figure out how I'd possibly get a value at an arbitrary depth from a dict.

There are other places like that, that you'll run into potentially with specific data structures. The reason everyone uses Towers of Hanoi, factorials, or making the Fibonacci sequence is that they're easy examples of where it's way easier, or you have to use recursion to solve. Many other uses don't really require you to recurse.

u/MyUsernameIs3pic, u/Steak_Cold, u/_OSCP, u/ELAKSAVKD, u/goofyboarder64, u/shoguneye2

edited to add @ mentions and a link to RealPython's recursion tutorial, that doesn't just use the awful factorial/fibonacci/math examples.

ALL recursive functions will be structured like this (tbh try to prove me wrong):

def recursive_func( a_thing ) # it WILL take an argument
    if stop_condition: # will always start with a final condition
        return something # return the thing I want

    #otherwise we're not stopped
    new_thing = do_something_to_what_we_got_instead() # maybe

    return recursive_func(new_thing) # run the func on the new thing

Check out that tutorial with that structure in mind and see if it helps!

1

u/Ne0_1 Feb 22 '21

I can't preach MIT OCW enough. For oop they have us simulate a working robot in a room and another problem they have us model virus growth in a patient modeling the effects with and without drugs. For a recursion problem we did a multiple-shift caesar shift decryption tool. Lots of cool, useful examples to build software and get as close to real-world experience as you can get. These problem sets are fun and challenging and I have learned so much about not only how to write software in Python but the underlying data structures and algorithms as well as Python specific implementations.

1

u/MaraudingAvenger Feb 21 '21

In another comment I mentioned API wrappers. They're some seriously useful things when you have to interact with some API. It's the kind of thing that you work out and create once, and then never have to create/work out again, so you can brain dump at least some of it.

Here's an implementation of the contrived Wiktionary example I used in this comment. It uses requests and beautifulsoup4 if you want to run it yourself:

from urllib.parse import quote, urlencode
import requests
from bs4 import BeautifulSoup

class Wiktionary():
    def __init__(self):
        self._base_url = "http://en.wiktionary.org/w/api.php"
        
    def _query(self, term):
        with requests.Session() as sess:
            url = self._base_url + '?' + urlencode(
                {
                    'action' : "query", 
                    "list": "search", 
                    "srsearch" : quote(term),
                    "format" : 'json'
                }
            )
           
            d = sess.get(url).json()
        if d: 
            return d['query']['search'][0]['pageid']
        return None
    
    def lookup(self, word):
        
        pageid = self._query(word)
        if not pageid:
            return []
        
        with requests.Session() as sess:
            url = self._base_url + '?' + urlencode(
                {
                    "action" : "parse",
                    "format" : "json",
                    "pageid" : pageid,
                    "disabletoc" : 1,
                    "contentmodel": "text"
                }
            )

            d = sess.get(url).json()
        if d:
            return BeautifulSoup(d["parse"]['text']['*']).select('ol')[0].text.splitlines()
        return []

You just save that in a file (maybe "wiki.py"), and import it to use it. Never look at it again.

from wiki import Wiktionary
w = Wiktionary()
w.lookup("dude")

The Wiktionary object could almost certainly have been done better, and I'm probably butchering that API, but I don't care1, 2. It works!

1 I do care

2 I don't want to break Wiktionary accidentally, for sure, and this was made in like 10 minutes using their API documentation. By all means improve/make a better one and share!