Skip to content

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

Open

Description

@maneesha

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

Activity

  1. quist00 commented on Feb 27, 2024

    @quist00
    Contributor

    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?

  2. maneesha commented on Feb 27, 2024

    @maneesha
    ContributorAuthor

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

  3. self-assigned this
    on Feb 27, 2024
  4. quist00 commented on Feb 27, 2024

    @quist00
    Contributor

    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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions