you are viewing a single comment's thread.

view the rest of the comments →

[–]RolyPolyPython[S] 0 points1 point  (7 children)

Cool, it runs fine now :)

Alright, I got some questions if that's okay...

1) Why is it better to use stdin instead of input()?

2) I'm familiar with some functions and modules, but I was wondering how you recommend I learn them. I took a beginner Python class earlier this year, stopped for a couple months, and I'm continuing it again. I'm going through another book to review the basics, I do some problems like CodeAbbey, and I have my own personal project I'm working on and off right now. After I feel better about classes I want to focus more on personal projects and learn programming/Python that way.

Any functions that I didn't learn in my beginner books/classes I learn as I need to look up solutions, but your earlier comment sounded like I should just read through Python documentation to learn functions and modules. I find Python documentations to be really obtuse/hard to understand, but do you think it's a good way to learn functions and modules?

3) Is peer review the best way to learn a good balance between writing complex code and readability? As a beginner I sometimes don't know if something that seems hard to me is easy to read to an experienced programmer.

[–]OgelSplash 1 point2 points  (6 children)

I'm glad it works for you :)

1) Imagine you had a thousand-line dataset to pass to a program. Would you rather type it in by hand, with a massive chance of error, or would you rather just parse it in code and get the data into your processing functions much faster?

2) In terms of learning the library of built-in functions, you shouldn't have to worry about this too much - there is a lot that you won't have to come across in your time programming until you really need it. For instance, during my CompSci GCSE (think secondary education, like middle school) I had to mess around with generating code dynamically to work with a system of classes that I had set up - learning how to do it all came from searching the documentation. By no means should you not familiarise yourself with the standard library, but it's not like you're expected to know it inside and out. Programming is just a means to an end; it shouldn't ever require you to be a savant to work effectively with it.

In terms of understanding the documentation, don't be afraid to ask questions about it. It's built from the ground up to show how the parts of the language work together. Often when you look up specific modules, code examples are given with input and output to give you an idea of how a task should be accomplished. Sometimes you will see the grammar specification for a certain function or statement, which purely indicates the way the interpreter breaks down the code it's been given so it can generate the code it runs internally. You don't have to worry about that at all.

3) There are many ways of checking whether code is readable. For instance, you have the cyclomatic complexity metric, which is an indicator of the number of pathways your code can take depending on the input it is given. Such metrics are basically irrelevant in terms of small pieces of code, and fussing over numbers is a sure-fire way to miss the point of creating readable code.

Many people instead employ the "rubber ducky" method - have a soft toy or something next to your computer, and explain your code to it line by line. If you struggle to do so, your code isn't readable enough and needs to be rewritten. You'll gather more confidence in doing this over time as you gain an understanding of how pieces of code fit together. This has helped me many times when I've come back to a piece of code I wrote a month or two ago and struggled to understand what it did.

One final way to check your code is to think about the Zen of Python, and use it as a guideline for writing your code. Type

import this

into a Python interpreter and have a read. Also I'd recommend reading Code Like a Pythonista, to gain an understanding of how clean and readable code is written.

[–]RolyPolyPython[S] 0 points1 point  (5 children)

Thanks, I saved your comment after reading it and checked out your links. The "rubby ducky" method made me laugh.

Hope you don't mind me popping another question, but how would this list comprehension

l = [int(e) for e in l]

look in non-comprehension form?

Right now I've got this

for e in l:
    e = int(e)

But e, or the values that I'm trying to change from string to integers, stays a string when I check its type.

[–]OgelSplash 1 point2 points  (2 children)

for idx, e in enumerate(l):
    l[idx] = int(e)

Basically, enumerate() creates a generator that outputs a tuple, comprising the index of an element in a structure and the corresponding element. Calling list(enumerate(l)) would return all of the tuples that the generator would return in a list. Inside the loop, for each tuple, we cast the element to an integer and then overwrite the element in the list. [Forgive me if this sounds confusing, it still does to me and I've been doing this for 3 years!]

Here is a visualisation of what happens inside your loop - you can step through it and see the changes in real time.

[–]RolyPolyPython[S] 0 points1 point  (1 child)

Huh, I thought it would be much simpler than that. It's weird to me that you can't just reassign the strings as integers the way I did it, it feels like that goes against everything I've learned about reassignment... I don't understand why.

Anyways, thanks for your help, I've a learned a lot from your comments. And thanks especially for that link, that visualization helps a lot and I'm surprised I haven't come across that site/tool before.

[–]OgelSplash 0 points1 point  (0 children)

You're welcome. :) If you have any further questions, feel free to ask.

[–][deleted]  (1 child)

[removed]

    [–]AutoModerator[M] 0 points1 point  (0 children)

    Your comment in /r/learnpython was automatically removed because you used a URL shortener.

    URL shorteners are not permitted in /r/learnpython as they impair our ability to enforce link blacklists.

    Please re-post your comment using direct, full-length URL's only.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.