Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semicolons are not typically used in Python code and may cause confusion #604

Open
maneesha opened this issue Feb 27, 2024 · 3 comments
Open
Assignees

Comments

@maneesha
Copy link
Contributor

What is the problem?

Semicolons aren't wrong but are not commonly used in Python code and have not been introduced in this lesson. Line breaks and indents are more critical to properly formatted Python code. Consider either explaining why semicolons are used here and no where else, or remove the semicolons.

Location of problem (optional)

https://datacarpentry.org/python-ecology-lesson/02-starting-with-data.html#quick-easy-plotting-data-using-pandas

@quist00
Copy link
Contributor

quist00 commented Feb 27, 2024

To the best of my knowledge, the purpose in this case is just to suppress arcane output about memory space from the figure that is generated. If removing them means regenerating all those screenshots to match, then I would prefer explaining how they can be used and how they are being used here. Do you know specifically if that suppression of output is an anomaly in in Juptyer that we are taking advantage of? Like would it only output that memory address information for the last item and by using a semi-colon we are tricking it into thinking our figure is not that last item in the cell?

@maneesha
Copy link
Contributor Author

I don't know why semicolons are used here. I don't have a preference about removing them or adding an explanation about them.

@quist00 quist00 self-assigned this Feb 27, 2024
@quist00
Copy link
Contributor

quist00 commented Feb 27, 2024

For later reference. It does seem to be a trick to assign None to the last item in the cell to suppress the printing of it.

Semicolon in the interpreter
Having read the answers, I still miss one important aspect of using semicolons, possibly the only one where it really makes a difference...

When you're working in an interpreter REPL (the Python interactive shell, IDLE, and IPython) the value of the last expression is printed to the screen and usually this is the intended behavior.

Using an expression for side effects
But in some cases you want to evaluate an expression for its side effects only, e.g., to see the results of your simulation plotted by matplotlib.

In this cases you (probably) don't want to see the screenful of reprs of matplotlib objects that are sometimes returned by a call to a matplotlib function and, in IPython at least, one of the possibilities you have is to append a semicolon to the overly verbose statement, now IPython sees the input line as composed by two expressions, the matplotlib invocation and a null statement, so that the value of the compound expression is None and nothing is printed to the screen by the interpreter (the other possibility being assignment, as in _ = plot(...) but I find that a bit more intrusive).

Personal remark
IMHO, the use of the semicolon to suppress not desired output in the interpreter has become more relevant following the introduction of the IPython notebook, that permits to save the input and the output, including graphical output, of an interpreter session for documentation and eventual reuse.

https://stackoverflow.com/questions/8236380/why-is-semicolon-allowed-in-this-python-snippet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants