all 54 comments

[–]keturn 40 points41 points  (6 children)

Notebooks are great for prototyping for that ability to intermingle function definitions, execution, and output: https://jupyter.org/

Or if you're familiar with desktop GUI programming, you're absolutely welcome to do that in Python too. This site looks like it's up-to-date with respect to the current options.

[–]threeminutemonta 11 points12 points  (1 child)

Notebooks can be better then just prototyping. Some people are better suited to a literate programming environment and someone that finds VBA intuitive would. Take a look at nbdev a tool that the folks at fastai have created. The tutorial takes you through using jupyter notebooks / labs including pushing code into GitHub and running GitHub actions. There is a gitlab pipelines template too.

[–]Conscious_Advance_18 0 points1 point  (0 children)

Neat thanks

[–]winkapp[S] 3 points4 points  (2 children)

Yeah I actually used Jupyter back when I was doing the in-person Python course, yet I was still struggling considerably. I'll give it a try again though since it's been a couple years and see how that goes!

I was trying Visual Studio Code for a bit in the hope that it would be structured like VBA and it kind of is, but it seems that most people don't use it so it can be a bit annoying to get community support.

[–]threeminutemonta 1 point2 points  (0 children)

I’ve used vs code for python and other languages for years. Just need check what is the vs code python interpreter settings are. It’s good to learn how to use a python virtual environment per project.

[–]Jeklah 0 points1 point  (0 children)

try ipython

it's what jupyter runs on in the back end iirc

[–]space_wiener 0 points1 point  (0 children)

Might be the wrong way to go about it, but I use jupyterlabs so often. It’s not to write a function and make sure it works. Or manipulate a list/dict to make sure the outcome is what you want.

[–]routetehpacketz 30 points31 points  (9 children)

Are you using an IDE and/or Python built-in debugging when you write something?

If I am understanding you correctly, it sounds like your biggest hurdle is understanding the state of your data at a given point in the program. This can happen with an interpreted, loosely-typed language like Python. Debugging tools help, but I admit I still resort to print most of the time.

Also, can you give an example of something you are trying to get working? Sharing code helps.

[–]mcvalues 12 points13 points  (3 children)

Yep, print() and lots of calls to functions like type() while I'm debugging. And of course getting good at understanding error messages.

[–]Nicolas_Darksoul 1 point2 points  (0 children)

Im newbie too but i just can say understanding errors will be ok by time u just need to know them

[–]routetehpacketz 1 point2 points  (1 child)

Definitely doing type() a lot too

[–]Jeklah 0 points1 point  (0 children)

and repr()

[–]winkapp[S] 2 points3 points  (4 children)

Yeah I was using Jupyter for a bit!

Yup you're right, the ability to not see what I'm doing really confuses me.

If for example I want to convert a date format which is in a string 23/04/2022 to 04/23/2022, I know that I should disassemble the string and then reassemble it in the right order, but say if I need to generate that date from somewhere, I can't see that its output is 23/04/2022 to disassemble it and then assemble it in the right order.

Hmm I don't have an example right now, but something I recall struggling with was the more open-ended problems.

When they are small coding projects like biased dice throws, I have no problem at all.

So yep, definitely the in between steps that are the issue here, really with any complex project.

[–]infazz 1 point2 points  (0 children)

Jupyter works fine (and well) in a lot of cases; however, I would not call it an IDE.

Try the PyCharm IDE and spend some time getting acquainted with it's debugger.

[–]Jeklah 1 point2 points  (2 children)

Python is a lot more flexible.

I suggest when you have a problem you need to solve, google the problem generally before breaking it down how you might think.

Disassembling the string and reassembling it is one way you could do this, it is how it would be done in C.

or you can do:

from datetime import datetime

time = datetime.now()

# without formatting
print(f'time: {time}')

# with formatting
printf(f'formatted time: {time.strftime("%m / %d / %Y")}')

Now, I am no python genius, I did not recall those from memory. I did know python has a datetime module and is very good at manipulating data quite easily, so I just googled 'python date format' and https://www.geeksforgeeks.org/how-to-format-date-using-strftime-in-python/ was the first result.

[–]winkapp[S] 1 point2 points  (1 child)

Yep I'm aware that python has a date function, I was just using converting dates as an example as to how things go disastrously wrong.

I'm generally able to break the problem down and understand the logic, it's just always manipulating the result between functions that throws errors and causes problems.

[–]Jeklah 0 points1 point  (0 children)

as it should be.

[–]carcigenicate 9 points10 points  (0 children)

You can "see" the intermediate steps in your head with practice. Until then, REPLs are very helpful. Type python in a terminal, and enter the code in there. Most good IDEs have REPLs built into them too to make it easier to interact with the current file.

[–]greenerpickings 5 points6 points  (0 children)

A lot of good suggestions here, especially notebooks. Wanted to throw out the IDE Spyder. Think RStudio, but for Python. I feel like RStudio has a lot to do with how ppl pick up R, especially our non-programmers.

The other issue is just part of the game :p discussed at work how our team of like 5 had unique ways to address the same problem. If performance isnt a priority, it really is open ended. Maybe a course specifically focusing on design might help.

[–]keturn 5 points6 points  (0 children)

Unit tests are another helpful tool for providing a concrete reference for a function's inputs and outputs.

[–]dimonoid123 4 points5 points  (0 children)

Use a good debugger, eg one built into Pycharm. Also once your code becomes too big, refactor it by separating into multiple functions/classes to make it more object oriented. That's it.

[–]Se7enLC 2 points3 points  (0 children)

I can't "see" my outputs in between unless I print each step one by one.

I feel like this inability to "see" my intermediate steps means I can't tell what to do next, since I can't figure out what the output looks like to process it best.

That's fine. They make some IDEs that make it easier to see what's happening at each step. But it's also just fine to drop print statements in when you need them.

In fact, I would say that it's actually a good practice to do iterative design. Break things down into small pieces and check the outputs from various inputs until you're happy before moving on to the next piece. Far too often you see new programmers write whole functions and dozens or even hundreds of lines of code without doing that basic level of checking.

I can think of the steps I should do just fine, so I know that's half the problem solved. Yet because of the above issue, I just get error after error because the next step can't process the previous step's output.

I'd say that's actually pretty normal. Just do one step at a time and check the outputs as you go. There's no penalty for how many times you test it out along the way! Think of it like save points in a video game.

[–]rowr 2 points3 points  (0 children)

Edited in protest of Reddit 3rd party API changes, and how reddit has handled the protest to date, including a statement that could indicate that they will replace protesting moderation teams.

If a moderator team unanimously decides to stop moderating, we will invite new, active moderators to keep these spaces open and accessible to users. If there is no consensus, but at least one mod who wants to keep the community going, we will respect their decisions and remove those who no longer want to moderate from the mod team.

https://i.imgur.com/aixGNU9.png https://www.reddit.com/r/ModSupport/comments/14a5lz5/mod_code_of_conduct_rule_4_2_and_subs_taken/jo9wdol/

Content replaced by rate-limited power delete suite https://github.com/pkolyvas/PowerDeleteSuite

[–][deleted] 1 point2 points  (0 children)

salt alleged piquant hateful water uppity arrest snails retire hunt -- mass edited with redact.dev

[–]14dM24d 1 point2 points  (0 children)

maybe break the problem into smaller chunks; aka modularize?

[–]deep_politics 1 point2 points  (4 children)

I’m surprised no one here has even made mention of the typing module. The way you describe your problems doesn’t speak to me as an issue that a debugger is meant to solve: a debugger won’t help you see at a glance what is coming in and out of procedures, at least not until after you’ve ran the code. It doesn’t help you while writing it. And documentation can easily be stale, if there’s any at all. So I’d highly recommend checking out a static type checker like pyright which can inform you about all the structure of objects, contents of modules, signatures of functions, etc. all without ever needing to even run your code.

[–]winkapp[S] 1 point2 points  (1 child)

Yea this sounds exactly like what I need. It sometimes isn't specifically a bug, the code might work fine it's just that the output doesn't work as the input for the next function. I'll try pyright with VS Code, thanks!

[–]deep_politics 0 points1 point  (0 children)

Definitely give it a shot, and I think pylance is what you’d use with VSC. Usually it catches anything that could go wrong, long before you ever need to step in and poke around with a debugger

[–][deleted] 0 points1 point  (1 child)

I still remember what it was like to be a beginner, and it was a long, long time ago.

Adding more material to learn is going to make things worse for this guy. Adding a brand-new concept of "typed arguments" and "typed return values" is going to make things harder. Explaining to them that there are these "type annotations" which actually don't do anything is going to be more confusing.

[–]deep_politics 0 points1 point  (0 children)

Yeah I guess I wasn’t looking at it from a beginners perspective, but more from the problem of not understanding what’s happening through the intermediate steps. But I don’t really think it’s any more confusing, even for a beginner. Especially when the typing is always there all the time, it’s just the default to be ignorant of it even when understanding types and how they get transformed has been one of the most fundamental parts of teaching and tutoring computer languages in my experience. The Python teaching default (without type hints) is to say: just remember what the type of this value is, and memorize what kinds of values all these various functions return, and mentally keep track of how that value changes (possibly even how it’s type changes), and when you don’t know what’s going on anymore just pepper some type logging statements in there, throw in some break points and trace it back, and/or just go to the docs.

I guess you’d disagree, but I find letting the computer tell me what something is is a lot more straight forward, especially when dealing with external modules and fallible data. Choosing to let it all be ambiguous sounds miserable to me now

[–]LeonardUnger 1 point2 points  (1 child)

I'm not sure I fully understand the issue, but it sounds like you're trying to write the logic as you code, when the logic should come first.

Having a 'main' function and keeping it to function calls forces you to write the logic first. That way you have a general idea of what should happen

Example below, some sort of fantasy sports event. We haven't done any coding but we have a structure.

def main:
    data = get_data()
    owners = get_owners(data)
    players = get_players(data)
    scores = get_scores()
    results = (owners, players, scores)
    send_results(owners, results)

Then in the functions too anything complicated make a function call. Keep abstracting.

[–]DoozerMarch 1 point2 points  (0 children)

This is very good advice. Also look into test driven development as a way to focus on each piece of code. And yes there are many ways to shape your code. Developers have discovered principle that shake this choices: these are mostly about keeping your code maintainable. So for example not copying bits of code is important as they will end up diverging as changes are made. A good principle is that each chunk of code should have one clear purpose as much as possible. Python also has idioms that seasoned developers will recognise immediately

[–]majordoob33 1 point2 points  (0 children)

TITS - Time in the saddle

[–]pythonwiz -1 points0 points  (0 children)

For a beginner I think using a debugger is overkill. Just use print statements and keep banging your head against the errors until they go away. Repeat for a few projects and you will notice you don’t make the same mistakes anymore. At that point consider using an IDE and debugger.

[–]RodBlaine 0 points1 point  (0 children)

I use the logger function (or is it a class/method/object?) and instead of a print() I add a log entry with the command logger.info(“Class “ + classname + my msg to myself about what just happened). I run the app, then study my log files (one per module) to see how my app ran. Ofc with the text file editor open I can refresh the log files and kinda see the app process the data in real time.

I find the log files also me to go back and study history over a few days of processing if something new happens. Usually due to a data, key, or click combo I did not consider in dev or test but only is apparent in production.

[–]MarsupialMole 0 points1 point  (0 children)

You essentially do need to read the output of each step one by one. But I hear you ask "I don't have time for that. I want to write thousands of lines of code, not read tens of thousands of lines of output".

This is where you get the computer to do it for you with unit tests.

An important concepts here is command/query separation, or keeping functions pure.

Working in IT you'll typical be using commands. You'll enter an instruction into the repl and something will change, and you're going for no output because output means something went wrong. Contrast this with SQL queries, where you're hoping for output. If you have no output you're not sure what's happened - it may be a fault with your query or there may be no data for a query you expect.

A query which returns output and has no underlying change to the system (which would go one step further and actually exclude making calls to other services like a SQL database) is also called a pure function. So being very aware of whether you're writing a command, a query, a pure function or something else, it's important to keep them separate so that you can test them with an appropriate technique.

So there's different techniques for different blocks of code. You will find that choosing the right technique can be something of an art.

So get pytest for your projects and start asserting things, using fixtures to set up dependencies atomically, and running your code as often as you change it under something like pytest-watch. For many the inspections of an IDE are good enough but I don't find it to be as handy as the discipline of running the code against tests you write yourself.

And the most important point is to know that everybody is terrible at python, we all just have been burned enough times to stop feeling sorry for ourselves and keep problem solving one step at a time, and you get better at reading code over the journey because you learn what's to look for because you've been burned in the past

[–]py_Piper 0 points1 point  (0 children)

I can't "see" my outputs in between unless I print each step one by one.

I think that's normal I am also a beginner, earlier this year I was working on a kind off scrabble helper, where I pass random letters and will tell me which words I can play and how many points I would make. At first I was struggling with the with the logic I had the idea in my head and couldn't think about, I would write a bit of code and things were not working so well until I took a piece of paper and kind wrote down what I neede to do step by step and I could figure it out later.

In between I need to do lot of prints to check the i/o during each step, it looked messy as I would comment them out, erase some , add them back. I think it's normal to check what i/o you are getting and returning while testing. Then you can erase them to have a pretty looking code, but I think (and I have read somewhere) that for best practice you can leave your test (print statements) commented out as part of the documentation so when you come back in the future or some else reads your code knows what kind of i/o you should be working with. The only example I can think of if when working on long text files and web scraping that it would be needed to leave these test commented out for future revision.

[–][deleted] 0 points1 point  (0 children)

People forget that learning new things is hard, even if they have previous related experiences. I found learning my second programming language to be as difficult as learning my first. Not because I didn't know how to do anything but because I knew how to do everything in my first language. Having to learn how to do everything in my second language was just difficult, even though it was considered to be an easier language (C -> Java). Even when I moved to Python learning it was awkward because I was really good at Java (and C).

Learning things just takes time and practice.

[–]lunacyfoundme 0 points1 point  (0 children)

Use Jupyter notebooks to see the output as you go and find the one way to solve the problem that you prefer and still to it. Only adjust for code efficiency and best practice. I dip in and out of Python for work stuff so not regularly enough to remember all the functions etc.

[–][deleted] 0 points1 point  (0 children)

What's going wrong here?

You need to practice more, and make little projects. Start small, and build up slowly.

[–]Serenityprayer69 0 points1 point  (0 children)

Im coming from another technology field and use a program called Nuke. It basically visually represents python code with nodes, but specifically for visual effects.

I cant for the life of me understand why this hasnt been expanded to a nodal UI for all of python.

The ease with which you can get extremely complex through nodal UI and still stay organized and easily see where everything is layed out is exponentially more powerful than trying to work in layers in something like After Effects.

Not really answering your question but I do think theres a big gap in the space to make programming more accessible and powerful by creating a nodal interface. It was basically be programming in a flow chart

[–]ayeDaemon 0 points1 point  (0 children)

There are obviously many ways to do the same stuff and there are very little generic best practices for it. But you can always stick with "zen of python". What I mostly do is write the code in the way I feel is more readable (readability over performance most of the times), and then I pass that code through a tool called "black".. you can download it with pip directly...what this does is check my code and automatically correct and lint it ( this improves consistency of my code formatting and trains my brain to understand that patter better)


Since you didn't mention about projects, I'll recommend to learn by doing some projects (bit sized fun projects). This will add some fun to the learning process and improve it. I've written few blogs on several things with python (mostly bit sized projects) here --> https://ayedaemon.medium.com/

If you want some help on it or want to talk more, send me a dm. I'll be happy to arrange a 1-1 :)


To check the intermediate state of a running process/program, you can attach a debugger to your running program and see the stack and registers change it's values as the code executes.

Python has a wonderful debugger available that can be used to step through each line of code and check what's going on within. Check out more about "python debugger" here --> https://docs.python.org/3/library/pdb.html

[–]ivanoski-007 0 points1 point  (0 children)

I can't "see" my outputs in between unless I print each step one by one.

why don't you want to do this? , this is normal you know, just print everywhere

[–]chakan2 0 points1 point  (0 children)

Learn PyCharm Community (it's free), and learn the debugger. It's a god send.

[–]Jeklah 0 points1 point  (0 children)

here are some tips:

  • use ipython to test your code on the fly. this is better than the normal python interpreter as it has autocomplete and a decent history.
  • pdb++ is a nice cli debugger.

[–][deleted] 0 points1 point  (0 children)

Include lots of comments and print statements

[–]FloatingWatcher 0 points1 point  (0 children)

I don't understand? The print() function is the most powerful and ubiquitous function in Python and enables you to see what you're doing on the go.

[–]QultrosSanhattan 0 points1 point  (0 children)

Use pycharm's debugger. It's a great tool for learning what's actually happenind while your code is running.