Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet159/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   155   156   157   158   159   160   161   162   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Using an Alias


Often, the name of what you’d like to import can be lengthy. Rather than having to write out an entire name each time you’d like to use it, you can give an “alias” or nickname when importing:

# using the 'as' keyword to create an alias for imports from math import floor as f print( f(2.5) )

Go ahead and run the cell. We’ll get the same output as we do in the previous two cells, except this time we were able to reference the floor function as just the letter “f “. This is because of how we wrote our import statement using the “as” keyword. You can rename anything that is imported, although it’s generally best to only do so on larger names.

Creating Our Own Module


Now that we know how to import and call a module, let’s create our own. Go ahead and open any text editor you have on your computer like Notepad or TextEdit. Write the following code in the file, and save it within the same folder that your “Week_09” file is located, with the name “test.py”. If the two files aren’t in the same directory, it produces an error:

# creating our own module in a text editor
# variables to import later length = 5 width = 10 # functions to import later def printInfo(name, age): print( "{ } is { } years old.".format(name, age) )

See Figure 9-1 for an example of what the code will look like within a text editor.

Figure 9-1. test.py module with code in text editor (notepad++)
You’ve just written your first module! Remember that modules are nothing more than code written in other files that we can import in any of our projects. Now let’s see how to use them.

Using Our Module in Jupyter Notebook


In any other circumstance, you’d import the variables and function we wrote in test.py with the import and from keywords. Jupyter Notebook, however, works a little differently when using modules that you’ve created. We’ll use the “run” command in order to load in the entire module that we’ve created. After we run the file, we can use the variables and functions that we wrote within the module. Let’s check out how to do so:

# using the run command with Jupyter Notebook to access our own modules
%run test.py print(length, width)
printInfo("John Smith", 37) # able to call from the module because we ran the file in Jupyter above

Go ahead and run the cell. You’ll notice that we’re able to output the variables and function print statement that we declared within our test.py module. Keep in mind that the run command runs the file as if it were a single cell. Any function calls or print statements within our module would run immediately. To test this out, try putting a print statement at the bottom of the module. When you work in a development environment (IDE), you’ll write the import as you would normally, like the following:
>>> from test import length, width, printInfo
ChAptEr 9 AdvAnCEd topiCs ii: ComplExity
This is just how Jupyter Notebook works with files that we create.
Note you can place any modules you create within the python folder on your hard drive. once the files are there, they can be accessed normally rather than using the run command.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   155   156   157   158   159   160   161   162   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish