r/learnpython • • Mar 05 '21

Jupyter Notebooks - Great tool for beginners

Just wanted to share with anyone who is struggling to learn AND implement code. I'm in my 4th week of Intro To Python and I have to say, I got a little overwhelmed. My teacher recommended using Jupyter Notebook (Google it, it's Free!) and it's helped out a shitload. Basically it's an open-source web app that allows you to share documents that contain live code. The BEST part is you can easily toggle between text for note-taking and code AND RUN THE CODE right in the document. This really changed the game for me and if you're out there struggling to bounce between your notes and your interpreter/compiler, check it out. Good luck

378 Upvotes

72 comments sorted by

View all comments

Show parent comments

2

u/Useful-Pomelo Mar 05 '21

I am really struggling to see the difference between a .py file in Spyder and a reason to use Jupyter Notebooks. Notebooks seem more linear, first this, then that; while a .py file can have branching and conditionals and loops.

2

u/BoJackHorseMan53 Mar 05 '21

You can write everything you'd write in a .py file in a single Jupyter notebook cell and it'd work.

2

u/[deleted] Mar 05 '21

What do you mean by loops and conditionals?

Jupyter notebooks have the possibility of formatting cells with markdown, so you can write actual text. I think of it as you have to do some work that needs coding to be done, but in which the code is not the important part, rather its results.

Again from my physics experiences: often i have to hand in a document with theory, conclusions, etc. The topic is the important part, the code could very well not be there. But if my assignment is computational, it's important to include the code so the assesser can run it and see that it does work to give the results I talk about. Notebooks are meant to be stand alone documents that allow python to run within them. .py scripts are more meant to be used in large projects that require running everything in tandem. .py have no flow to them, the goal is to get it done, they aren't meant to be read (per se, of course someone will want to see the code at some point). ipynb's are meant to be read as documents

1

u/Useful-Pomelo Mar 05 '21

Thanks. By loops and conditionals I mean testing to see if an expression is true, and as a result do something. In Notebooks, are entire chunks of code blocks often skipped like an if-then-else clause?

if var >=2:
    print("2 or more")
else:
    print("less than 2")

I don't often see those included in Notebook examples, but rather it's some sort of statistical analysis or data processing or the steps to create a plot. More of a linear step-by-step description that walks through each and every cell, and less a reactive, flexible, jump around-y program.

4

u/[deleted] Mar 05 '21

Ah I see. I definitely use loops and conditionals in my notebooks. I even write def statements in my notebooks. If I end up having a lot of them, I write them all into a .py script that I keep in the same folder as the notebook, and then import it at the beginning of the notebook so I can just call the functions.

A good example of what I mean is in that '3GB variables' thing I was talking about. If I want to analyze the data I store in these variables:

I store the data in var in my first cell (after all imports, setups etc)

Then, on the next cell I want to see a histogram. I set one up. I run the cell, and it returns that histogram.

But I forgot to add my axes labels, maybe I want to add a textbox with average, std deviation, etc. So I just write those lines in the histogram cell, re-run only that cell. Since my variables are already defined, I don't need to run that first cell again.

If I were to do this on a .py script, I would probably run the whole file each time I change it. That means defining a 3GB variable every time I run the code, and trust me, it takes my laptop about 3 minutes to store the data in the variable before the script proceeds. You may say 'But then you can run the script and after you just write whatever new commands in the terminal' and that's legit. But with a notebook, the input and output is clearly organized and safe: if I kill the kernel, the code stays there. If I killed the spyder kernel, the commands I ran in the terminal would be lost.

.py are compact, are compatible and easy to implement with many applications.

.ipynb may lack in compatiblity, but they compensate in self-containment and flexibility.

The two just have different purposes. But as far as python goes, they can do the exact same thing.

2

u/yuckfoubitch Mar 05 '21

I use jupyter and I use loops and conditionals quite often

2

u/DaveMoreau Mar 05 '21

AFAIK, you can put any valid Python code in your notebook. You can create classes in one cell and instantiate object in another.

What gets weird for me is the dependencies between cells and tracking which ones you need to re-run if you change something upstream. I also feel a little dirty with all the global variables that are visible to each cell.