Python-tulolähtö (I / O) Sisääntulon () ja tulostuksen () avulla

Tämä opetusohjelma keskittyy kahteen sisäänrakennettuun toimintoon: print () ja input () I / O-tehtävän suorittamiseen Pythonissa. Opit myös tuomaan moduulit ja käyttämään niitä ohjelmassa.

Video: Python Take User Input

Python tarjoaa lukuisia sisäänrakennettuja toimintoja, jotka ovat helposti käytettävissä Python-kehotteessa.

Jotkut toiminnoista pitävät input()ja print()ovat laajalti käytössä tavallisissa syöttö- ja tulostustoiminnoissa. Katsotaanpa ensin lähtöosa.

Python-tulostus käyttämällä print () -toimintoa

Käytämme print()toimintoa lähtödatan standardin tulostuslaite (näyttö). Voimme myös lähettää tietoja tiedostoon, mutta tästä keskustellaan myöhemmin.

Alla on esimerkki sen käytöstä.

 print('This sentence is output to the screen')

Tuotos

 Tämä lause lähetetään näytölle

Toinen esimerkki on annettu alla:

 a = 5 print('The value of a is', a)

Tuotos

 A: n arvo on 5

Toisessa print()lauseessa voidaan huomata, että merkkijonon ja muuttujan a arvon väliin lisättiin välilyönti. Tämä on oletuksena, mutta voimme muuttaa sitä.

print()Funktion todellinen syntakse on:

 tulosta (* objektit, sep = '', loppu = ' n', tiedosto = sys.stdout, huuhtelu = väärä)

Tässä objectson tulostettava arvo (t).

sepErotin käytetään arvojen välillä. Se oletuksena avaruusmerkiksi.

Kun kaikki arvot on tulostettu, endtulostetaan. Se oletuksena uudelle riville.

Se fileon objekti, johon arvot tulostetaan, ja sen oletusarvo on sys.stdout(näyttö). Tässä on esimerkki tämän havainnollistamiseksi.

 print(1, 2, 3, 4) print(1, 2, 3, 4, sep='*') print(1, 2, 3, 4, sep='#', end='&')

Tuotos

 1 2 3 4 1 * 2 * 3 * 4 1 # 2 # 3 # 4 &

Tuloksen muotoilu

Joskus haluaisimme muotoilla tuotoksemme niin, että se näyttää houkuttelevalta. Tämä voidaan tehdä str.format()menetelmällä. Tämä menetelmä näkyy kaikille merkkijono-objekteille.

 >>> x = 5; y = 10 >>> print('The value of x is () and y is ()'.format(x,y)) The value of x is 5 and y is 10

Tässä kiharaisia ​​aaltosulkeita ()käytetään paikkamerkkeinä. Voimme määrittää järjestyksen, jossa ne tulostetaan, käyttämällä numeroita (kaksinkertainen hakemisto).

 print('I love (0) and (1)'.format('bread','butter')) print('I love (1) and (0)'.format('bread','butter'))

Tuotos

 Rakastan leipää ja voita Rakastan voita ja leipää

Voimme jopa käyttää avainsana argumentteja muotoilla merkkijono.

 >>> print('Hello (name), (greeting)'.format(greeting = 'Goodmorning', name = 'John')) Hello John, Goodmorning

We can also format strings like the old sprintf() style used in C programming language. We use the % operator to accomplish this.

 >>> x = 12.3456789 >>> print('The value of x is %3.2f' %x) The value of x is 12.35 >>> print('The value of x is %3.4f' %x) The value of x is 12.3457

Python Input

Up until now, our programs were static. The value of variables was defined or hard coded into the source code.

To allow flexibility, we might want to take the input from the user. In Python, we have the input() function to allow this. The syntax for input() is:

 input((prompt))

where prompt is the string we wish to display on the screen. It is optional.

 >>> num = input('Enter a number: ') Enter a number: 10 >>> num '10'

Here, we can see that the entered value 10 is a string, not a number. To convert this into a number we can use int() or float() functions.

 >>> int('10') 10 >>> float('10') 10.0

This same operation can be performed using the eval() function. But eval takes it further. It can evaluate even expressions, provided the input is a string

 >>> int('2+3') Traceback (most recent call last): File "", line 301, in runcode File "", line 1, in ValueError: invalid literal for int() with base 10: '2+3' >>> eval('2+3') 5

Python Import

When our program grows bigger, it is a good idea to break it into different modules.

A module is a file containing Python definitions and statements. Python modules have a filename and end with the extension .py.

Definitions inside a module can be imported to another module or the interactive interpreter in Python. We use the import keyword to do this.

For example, we can import the math module by typing the following line:

 import math

We can use the module in the following ways:

 import math print(math.pi)

Output

 3.141592653589793

Nyt kaikki mathmoduulin sisällä olevat määritelmät ovat käytettävissä soveltamisalassamme. Voimme myös tuoda vain tiettyjä määritteitä ja toimintoja vain fromavainsanalla. Esimerkiksi:

 >>> from math import pi >>> pi 3.141592653589793

Tuodessaan moduulia Python tarkastelee useita kohdassa määriteltyjä paikkoja sys.path. Se on luettelo hakemistojen sijainneista.

 >>> import sys >>> sys.path ('', 'C:\Python33\Lib\idlelib', 'C:\Windows\system32\python33.zip', 'C:\Python33\DLLs', 'C:\Python33\lib', 'C:\Python33', 'C:\Python33\lib\site-packages')

Voimme myös lisätä oman sijaintimme tähän luetteloon.

Mielenkiintoisia artikkeleita...