Before getting started let us understand what are built-in-functions?
Any function that is provided as part of a high-level language and can be executed by a simple reference with or without the specification of arguments — Credits: Yourdictionary
There are many built-in-functions available in python. It is highly impossible to remember the name and the syntax of every built-in-functions. So below is the documentation of all the built-in-function available in python available in alphabetical order provided by the python.org — I call it as the cheat sheet of built-in-function, have a look below:
The input()
and the print()
built-in-functions are one of the most commonly used functions for performing standard input and output operations. There is no output()
function in python, rather print()
is used to perform the standard output operations.
The print function in python is normally used to print the output on the screen. As the name suggests print
meaning to print something where?
on the screen. Now you don’t have to write the entire body (contents) of the print function every time, personally I don’t even know how the body of the print function looks like. All you have to do is just call the function by passing the parameters. Whatever you write inside the print function are called parameters. Let us see how to print something onto the screen.
variable = "Welcome to Python Tutorials"
print(variable)
Welcome to Python Tutorials
Like I said the variable is the parameter that is passed along with the print function, you don't need to write the body of the print function you just need to call the print function. The actual syntax of the print function is given below:
Even you can try this just call help(print) and the help function will provide you the help you need (Duh ;))
help(print)
Help on built-in function print in module builtins: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.
Now let us see how to pass more than one parameter inside the print function and what would be the result?
variable = "Welcome to Python Tutorials"
variable2 = ",This is a good place to learn programming"
print(variable, variable2)
Welcome to Python Tutorials ,This is a good place to learn programming
You can pass as many parameters inside the print function but make sure that you separate the variable from one another.
Let us use a “sep” (separator) inside a print function and see the output.
print(1, 2, 3, 4, 5, sep="--->")
1--->2--->3--->4--->5
The “end” parameter is used to append a character to the end of the string which we pass inside the print function.
print(1, 2, 3, 4, 5, end=" This is the end")
1 2 3 4 5 This is the end
There might be some situations wherein formatting the output is required, in that case, we can use the str.format()
method.
variable = "Python"
variable2 = "Programming"
print("I love {0} and {1}".format(variable,variable2))
I love Python and Programming
Further, the format() method can do a great job such as adding the comma separator to the string of integers as shown below:
print(format(1234567, ",d"))
1,234,567
Similarly, we can format the output using the “%
” operator as shown below:
number = 12.3456
print("The value of x is %1.2f" %number)
The value of x is 12.35
print("The value of x is %1.5f" %number)
The value of x is 12.34560
In the past, we used to hard code the value of the variable inside our main program. But sometimes we need to take the input from the users. This can be achieved by using the Input function.
The Input function as the name suggests taking the user's input. The syntax of the input function is:
input(prompt = ‘ ’)
Here the prompt is the one that would be displayed on the screen.
number = input("Enter the number of your choice: ")
print("The number that you have entered is: %s" %number)
Enter the number of your choice: 100 The number that you have entered is: 100
Here, the prompt is “Enter the number of your choice”, here you might wonder why did I use “%s” when I entered an integer as an input. It is because all the variables are stored as string in Python, to explicitly typecast it use can use either int(variable_name), or float(variable_name) and many more. Go try it and let me know.
Sometimes, we need to import some libraries from a different module (a module is a python file that comprises definitions and statements). In those cases, we can import those methods or modules by using the import keyword along with the name of the module.
Consider an example, think you want to see what is the value of “pi”, all you can do is rather than doing (22/7, or memorizing 3.1423… I don't know further), just import the math module and call the math method and then your job is done.
import math
print(math.pi)
3.141592653589793
Also using the from keyword we can access specific attributes from the module. For example:
from math import pi
print(pi)
3.141592653589793
It's more like “From this module import this function, method or attribute”.
This is the end of the “Python Input, Output and Import” tutorial. Hope you enjoyed it. If you have any comments or suggestions let me know in the comment section below. Until then Goodbye !!!.