English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Python provides manyBuilt-in functions, which can be used at any time in the Python prompt.
built-in functions such as input() and print() are widely used for standard input and output operations. Let's first look at the output part.
We use the print() function to output data to the standard output device (screen). We can also outputData output to a file, which will be discussed later.
Below is an example of its usage.
print('This sentence is output to the screen')
Output volume
This sentence is output to the screen
Below is another example:
a = 5 print('The value of a is', a)
Output volume
the value of a is5
In the second print() statement, we can notice that instringand the variableabetween the valuesSpace. This is the default setting, but we can change it.
The actual syntax of the print() function is:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Here, 'objects' is the value to be printed.
sep is the separator between values. The default is a space character.
After printing all the values, end will be printed. The default is a new line.
file is the object that prints values, with a default value of sys.stdout (screen). This is an example to illustrate this.
print(1, 2, 3, 4) print(1, 2, 3, 4, sep='*') print(1, 2, 3, 4, sep='#', end='&')
Output volume
1 2 3 4 1*2*3*4 1#2#3#4&
Sometimes we want to format the output to make it more convenient to read and view. This can be done by using the str.format() method. This method is visible for any string object.
>>> x = 5; y = 10 >>> print('The value of x is {} and the value of y is {}'.format(x,y)) The value of x is5The value of y is10
In this case, curly braces {} are used as placeholders. We can use numbers (tuple indices) to specify their printing order.
print('I love {0} and {1}'.format('bread','butter')) print('I love {1} and {0}'.format('bread','butter'))
Output volume
I love bread and butter I love butter and bread
We can even use keyword arguments to format strings.
>>> print('Hello {name}, {greeting}'.format(greeting = 'Goodmorning', name = 'John')) Hello John, Goodmorning
We can also format strings in the old style used in the C programming language with sprintf(). We use the % operator to complete this task.
>>> 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
So far, our program is static. The values of variables have been defined or hard-coded into the source code.
To provide flexibility, we may want to get input from the user. In Python, we have the input() function that allows this feature. The syntax is input():
input([prompt])
prompt is where we want the string to be displayed on the screen. It is optional.
>>> num = input('Enter a number: ') Enter a number: 10 >>> num '10'
Here, we can see the input value10It is a string, not a number. To convert it to a number, we can use the int() or float() function.
>>> int('10') 10 >>> float('10') 10.0
The eval() function can perform the same operation. But eval further. If the input is a string, it can even calculate expressions
>>> int('2+3') Traceback (most recent call last): File "", line 301, in runcode File "", line 1inValueError: invalid literal for int() with base 10: '2+3' >>> eval('2+3') 5
When our program becomes larger, it is a good idea to break it down into different modules.
Modules are files that contain Python definitions and statements.Python modulewith a filename and ending with the extension .py.
Can import definitions from one module to another or into Python's interactive interpreter. We use import Keyword to do this.
For example, we can import the module math by entering the following line:
import math
We can use the module in the following ways:
import math print(math.pi)
Output volume
3.141592653589793
Now, all definitions within the math module can be used in our scope. We can also use the from keyword to import only some specific attributes and functions. For example:
>>> from math import pi >>> pi 3.141592653589793
When importing modules, Python uses sys.path to look at multiple locations defined in it. It is a list of directory locations.
>>> 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'
We can also add our own position to this list.