ShadowXL
ShadowXL

Reputation: 503

Using import in Python

So I’m a new programmer and starting to use Python 3, and I see some videos of people teaching the language and use “import”. My question is how they know what to import and where you can see all the things you can import. I used import math in one example that I followed along with, but I see other videos of people using import JSON or import random, and I’m curious how they find what they can import and how they know what it will do.

Upvotes: 1

Views: 165

Answers (3)

Mahsa
Mahsa

Reputation: 271

In all programming languages, whenever you actually need a library, you should import it. For example, if you need to generate a random number, search for this function in your chosen programming language, find the appropriate library, and import it into your code.

Upvotes: 1

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

Generally, you look in the python standard library reference, or on the Python Package Index for a module that contains methods you want to use. Then you import them.

A module in python is essentially a way of doing namespaces, same as most other languages. Generally, googling "how to do ______ in python" will provide some result of someone using the module you're aiming for. Then you can look up the documentation for that module to determine what functions and classes it provides (or alternatively, import modulename the module in a python console and then do help(modulename).

Upvotes: 3

Derek Eden
Derek Eden

Reputation: 4638

as a starting point, the list of all python's built-in modules can be viewed here, along with documentation for each:

https://docs.python.org/3/py-modindex.html

non built-ins are usually downloaded via pip and are available here:

https://pypi.org/

documentation is your friend

edit:

as stated in another comment, there is a vast amount of information at these resources..generally doing some kind of web search (google/stack/etc.) will point you towards a module you are looking for for your specific usage, then look at the examples given or check the docs

Upvotes: 2

Related Questions