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

Intermediate level Python tips and functions #76

Open
AlinderS opened this issue Oct 28, 2023 · 3 comments · May be fixed by #79
Open

Intermediate level Python tips and functions #76

AlinderS opened this issue Oct 28, 2023 · 3 comments · May be fixed by #79
Labels
enhancement New feature or request

Comments

@AlinderS
Copy link
Collaborator

There are a number of things in Python that are not required to write working code but that make your code better/cleaner/safer if you use them. For this reason, when the objective is to teach people the basics of programming so that they can do anything at all, these things tend to be skipped. We have an opportunity to provide this knowledge to students at the appropriate level.

Here are some examples of what I have in mind:

  1. Using enumerate() instead of manually incrementing an indexing variable in a for-loop.
  2. How to use zip() to iterate over two lists at the same time.
  3. What generators are, how to make them, and why they should be used.
  4. Using pathlib instead of os.path, and Path.open instead of open.
  5. That .extend() exists and when to use it instead of .append().
  6. The ord() and chr() functions ( I have not found a use for them in a scientific context but they are useful for general programming).
  7. The set data type and its uses.
  8. The functional programming tools map() and filter().

I'm not sure where this could go (though there is no room to spare anywhere except session 4), maybe this should not be in the presentations at all and left as a compendium, either in a manual or as its own document.

@AlinderS AlinderS added the enhancement New feature or request label Oct 28, 2023
@eerovaher
Copy link
Collaborator

Most of these would be good to teach (except ord() and chr(), which probably aren't worth the effort), but it's not clear how much should be added to this course. The Dynamical Astronomy notebooks make use of some of the things you've listed, so they teach by example and, furthermore, their use can be pointed out in the computer labs. Have you had a look at those notebooks?

@AlinderS AlinderS linked a pull request Nov 5, 2023 that will close this issue
@AlinderS
Copy link
Collaborator Author

AlinderS commented Nov 6, 2023

This idea seemed popular among the students, which made me wonder if one could go further still. I am now considering writing a similar guide covering what I see as the two remaining topics that the students could benefit from; Classes and type annotations.
Classes can be incredibly useful when dealing with complex data and type annotations fall squarely in the "not needed for Python to run but makes the code better in some way"-category.

@eerovaher
Copy link
Collaborator

You could easily have a full course that would be just about classes in general, and you could easily have a full course that would be just about how classes are implemented in Python. The topic is so broad that when you say you want to introduce classes here I really have no idea what you mean by that.

The way I see it using type annotations comes in three tiers: 1) annotating the public API for readability and better autocompletion, 2) making mypy happy, 3) making mypy in its strict mode happy. Only 1) would be in scope here. If there is time to discuss it then why not. The Dynamical Astronomy notebooks are already trying to teach this by example.

@AlinderS AlinderS linked a pull request Sep 4, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants