Python Forum
Code runs as pure python but not jupyter notebook
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code runs as pure python but not jupyter notebook
#1
Hello to the forum. As you can tell from the title, I am just learning python, and its use as a jupyter notbook.

I have this cell with a few lines of code that seems to run fine, under pycharm, when I use it in a file type of pure python. But when I use that same code in a file type ipynb, I get an "invalid syntax" error.

def quicksort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) // 2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return quicksort(left) + middle + quicksort(right)

print quicksort([3, 6, 8, 10, 1, 2, 1])


Here is the error:

print quicksort([3, 6, 8, 10, 1, 2, 1])
syntax error



Thank You
Tom
Reply
#2
This code runs fine on Jupyter. However, you will get 'invalid syntax' if you try print quicksort([3, 6, 8, 10, 1, 2, 1]). You have to print(quicksort([3, 6, 8, 10, 1, 2, 1])) because you probably have Python 3 kernel (have a look at Installing the IPython kernel)

You don't need 'print' to have cell output. Just enter 'quicksort([3, 6, 8, 10, 1, 2, 1])' in cell and hit shift + enter.

By default you will have output of last expression in cell:

1 + 1
1 + 2
Output:
3
If you want to have all outputs of expressions in cell at once then you have to run:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
After that you will have output of all expressions in cell (previous example will produce 2 and 3) without need of 'print'.
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
(Aug-22-2018, 04:32 AM)miner_tom Wrote: I have this cell with a few lines of code that seems to run fine, under pycharm,
That because you run it with python 2 in PyCharm Configuring Python Interpreter.
As mention bye @ perfringo print() is a function in Python 3.
print(quicksort([3, 6, 8, 10, 1, 2, 1]))
You should run Python 3.6 --> both local and Jupyter Notebook.
Here a couple of tutorials Python 3.6/3.7 and pip installation under Windows | Anaconda.
Reply
#4
Thank you for the replies. I will try out your suggestions later on today and they make a lot of sense, because I did specifically set up the pycharm environment/interpreter as python 2.7. I did that because the documentation for the notebook that I downloaded from gitub noted that the notebook was built under python 2, specifically. I'll try changing interpreters and see what happens.

Thanks again
Tom
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Importing a function from another file runs the old lines also dedesssse 6 232 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  It says there is no keras in jupyter notebook. it is wrong there is a keras install Led_Zeppelin 0 327 Apr-27-2021, 08:29 PM
Last Post: Led_Zeppelin
  importing matplotlib in jupyter notebook Led_Zeppelin 4 738 Feb-25-2021, 07:28 PM
Last Post: snippsat
  how to change the range of read CSV file every time python file runs greenpine 6 763 Dec-08-2020, 10:11 PM
Last Post: greenpine
  Databricks, Python Notebook Data file use issue khalid2200 0 477 Nov-25-2020, 03:36 AM
Last Post: khalid2200
  Jupyter Notebook cell ? flaviu2 5 948 Sep-29-2020, 11:06 AM
Last Post: flaviu2
  jupyter notebook varsh 0 508 Sep-25-2020, 08:56 AM
Last Post: varsh
  Runs perfect in Python but fails to print last statement when converted to .exe. Help sunil422 3 732 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  how to install pyramid.arima in jupyter notebook bntayfur 1 1,128 Aug-06-2020, 04:37 AM
Last Post: ndc85430
  Help with pie chart in Jupyter notebook Rony28 0 472 Aug-01-2020, 06:03 PM
Last Post: Rony28

Forum Jump:

User Panel Messages

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