Python- tutoriaal

Python TUIS Python Intro Python Begin Python-sintaksis Python-kommentaar Python veranderlikes Python-datatipes Python-nommers Python Casting Python Strings Python Booleans Python-operateurs Python-lyste Python Tuples Python-stelle Python Woordeboeke Python As...Anders Python While Loops Python vir lusse Python-funksies Python Lambda Python-skikkings Python-klasse/-voorwerpe Python Erfenis Python Iterators Python-omvang Python-modules Python-datums Python Wiskunde Python JSON Python RegEx Python PIP Python Probeer ... Behalwe Python-gebruikersinvoer Python String Formatering

Lêerhantering

Python-lêerhantering Python Lees lêers Python Skryf/skep lêers Python verwyder lêers

Python-modules

NumPy Tutoriaal Panda Walkthrough Scipy Tutoriaal

Python Matplotlib

Matplotlib Intro Matplotlib Begin Matplotlib Pyplot Matplotlib Plotte Matplotlib Merkers Matplotlib-lyn Matplotlib-etikette Matplotlib-rooster Matplotlib Subplotte Matplotlib Scatter Matplotlib Bars Matplotlib Histogramme Matplotlib sirkeldiagramme

Masjienleer

Aan die gang kom Gemiddelde mediaanmodus Standaard afwyking Persentiel Dataverspreiding Normale dataverspreiding Strooi plot Lineêre regressie Polinoomregressie Meervoudige regressie Skaal Trein/toets Besluitboom

Python MySQL

MySQL Begin MySQL Skep databasis MySQL Skep tabel MySQL-insetsel MySQL Kies MySQL Waar MySQL Bestel deur MySQL verwyder MySQL Drop Table MySQL-opdatering MySQL-limiet MySQL Sluit aan

Python MongoDB

MongoDB Begin MongoDB Skep databasis MongoDB Skep versameling MongoDB-insetsel MongoDB Vind MongoDB-navraag MongoDB Sorteer MongoDB verwyder MongoDB Drop Collection MongoDB-opdatering MongoDB-limiet

Python-verwysing

Python Oorsig Python ingeboude funksies Python-stringmetodes Python Lys Metodes Python Woordeboek Metodes Python Tuple Metodes Python Stel metodes Python-lêermetodes Python sleutelwoorde Python-uitsonderings Python Woordelys

Moduleverwysing

Ewekansige module Versoeke Module Statistiek Module Wiskunde Module cMath-module

Python Hoe om

Verwyder lys duplikate Draai 'n snaar om Voeg twee getalle by

Python voorbeelde

Python voorbeelde Python-samesteller Python-oefeninge Python Vasvra Python-sertifikaat

Python- modules


Wat is 'n module?

Beskou 'n module as dieselfde as 'n kodebiblioteek.

'n Lêer wat 'n stel funksies bevat wat jy by jou toepassing wil insluit.


Skep 'n module

Om 'n module te skep, stoor net die kode wat jy wil hê in 'n lêer met die lêeruitbreiding .py:

Voorbeeld

Stoor hierdie kode in 'n lêer met die naam mymodule.py

def greeting(name):
  print("Hello, " + name)

Gebruik 'n module

Nou kan ons die module wat ons pas geskep het, gebruik deur die importstelling te gebruik:

Voorbeeld

Voer die module genaamd mymodule in en roep die groetfunksie:

import mymodule

mymodule.greeting("Jonathan")

Let wel: Wanneer 'n funksie van 'n module gebruik word, gebruik die sintaksis: module_name.function_name .


Veranderlikes in Module

Die module kan funksies bevat, soos reeds beskryf, maar ook veranderlikes van alle tipes (skikkings, woordeboeke, voorwerpe, ens.):

Voorbeeld

Stoor hierdie kode in die lêer mymodule.py

person1 = {
  "name": "John",
  "age": 36,
  "country": "Norway"
}

Voorbeeld

Voer die module genaamd mymodule in en kry toegang tot die persoon1 woordeboek:

import mymodule

a = mymodule.person1["age"]
print(a)


Noem 'n module

Jy kan die modulelêer noem wat jy wil, maar dit moet die lêeruitbreiding hê .py

Hernoem 'n module

Jy kan 'n alias skep wanneer jy 'n module invoer, deur die assleutelwoord te gebruik:

Voorbeeld

Skep 'n alias vir mymodulegenoem mx:

import mymodule as mx

a = mx.person1["age"]
print(a)

Ingeboude modules

Daar is verskeie ingeboude modules in Python, wat jy kan invoer wanneer jy wil.

Voorbeeld

Voer in en gebruik die platformmodule:

import platform

x = platform.system()
print(x)

Gebruik die dir() funksie

Daar is 'n ingeboude funksie om al die funksiename (of veranderlike name) in 'n module te lys. Die dir()funksie:

Voorbeeld

Lys al die gedefinieerde name wat aan die platformmodule behoort:

import platform

x = dir(platform)
print(x)

Let wel: Die dir()-funksie kan op alle modules gebruik word, ook dié wat jy self skep.


Voer vanaf module in

Jy kan kies om slegs dele uit 'n module in te voer, deur die fromsleutelwoord te gebruik.

Voorbeeld

Die module genoem mymodulehet een funksie en een woordeboek:

def greeting(name):
  print("Hello, " + name)

person1 = {
  "name": "John",
  "age": 36,
  "country": "Norway"
}

Voorbeeld

Voer slegs die persoon1-woordeboek uit die module in:

from mymodule import person1

print (person1["age"])

Let wel: Wanneer jy die sleutelwoord invoer from , moenie die modulenaam gebruik wanneer jy na elemente in die module verwys nie. Voorbeeld: person1["age"], nie mymodule.person1["age"]


Toets jouself met oefeninge

Oefening:

Wat is die korrekte sintaksis om 'n module genaamd "mymodule" in te voer?

 mymodule