Python print function in module

Display output in Python 3 | print() function in Python 3

We can use print() function in Python to display some message or output on Python shell.

The Syntax of print function is:

 print(EXPRESSION)

Here EXPRESSION is the variable of any data type or string value that we want to display.

In previous versions of Python there was no need of parenthesis with print() function but in Python version 3 , it is mandatory to use pair of parenthesis with print() function.

Examples:

>>>print (“Welcome to Python”) # It is acceptable in Python version 3

print() automatically inserts a new line character at the end.

1. Displaying string values using print() in Python

We can use print() function in Python shell to display string values as follows:

You can see that, I have used print() function at >>> prompt twice as:

>>> print(“Welcome to Python”) Welcome to Python
>>> print(‘Welcome to Python’) Welcome to Python

2. Displaying numeric values using print function() in Python

We can use print() function in Python shell to display numeric values as follows:

I have used print() function at >>> prompt thrice as:

1. Integer value is displayed using print() function

>>>print(10)  10

2. float value is displayed using print() function

>>>print(2.55) 2.55

3. float value in scientific notation is displayed using print() function

>>>print(2.35E5) 235000.0

* ** This is because 2.35E5 means 2.35 multiplied by 10 5 . It will generate 235000.

3. Displaying Boolean values using print function() in Python

Python supports two Boolean values True and False where True represents 1 and False represents 0. We can use print() function in Python shell to display Boolean values as follows:

Читайте также:  Внешние компоненты 1с на java

I have used print() function to display Boolean values at >>> prompt twice as:

1. Boolean value True is displayed using print() function

>>>print(True) True

2. Boolean value False is displayed using print() function

>>>print(False) False

4. Displaying Complex values using print() function in Python

Python supports complex numbers that we must have studied in Mathematics in school. A complex number has two parts namely real part and imaginary part.

Example: 2+4i

In the above example 2 is real part and 4 is imaginary part. Alphabet i is used to represent imaginary part.

In Python, alphabet i is replaced by alphabet j.So above complex number will be represented in Python as 2+4j.

We can use print() function to display complex values as follows:

I have used print() function to display complex values at >>> prompt twice as:

1. Complex value 2+4j is displayed using print() function

>>>print(2+4j) (2+4j)

2. Complex value 2.5+3.5j is displayed using print() function

>>>print(2.5+3.5j)  (2.5+3.5j)

In Python,complex values can contain floating point numbers in real part as well as complex part.

5. Displaying collections using print() function

Python basically supports four types of collections:

We can display these sequences using print() function as follows:

>>> print([1,2,3,4,5]) [1,2,3,4,5]
>>> print((1,2,3,4,5)) (1,2,3,4,5)
  • Installation
    • Installing Python IDLE
    • Using Python IDLE
    • Installing Anaconda
    • Using Anaconda
    • Python Intro
    • Literals
    • Data types
    • Variables
    • print() function
    • input() function
    • eval() function
    • Basic operators
    • == and is
    • if Statement
    • Looping statements
    • List
    • List functions & Methods
    • Tuple
    • Tuple functions & Methods
    • Dictionary
    • Dictionary Functions & Methods
    • Creating a Function
    • Types of function
    • Types of function arguments
    • Modules and Packages
    • Mathematical functions
    • random module
    • statistics module
    • Steps to create data file
    • Writing content into a text file
    • Reading data from text file
    • Working with binary files
    • Write data into a Binary File
    • Reading data from a Binary File

    Источник

    How to list all functions in a Python module?

    In this article we will discuss how to list all the functions in a Python module.

    A Python module contains multiple different functions that allow for extensive code reusability making complex code simple. It also enhances portability of python program by changing platform dependent code into platform independent APIs

    Python standard library consists of modules written in C that provide access to system functionality and modules written in python that provide general solutions for everyday problems making the life of programmers easy as it prevents writing of long code for simple problems

    Using dir() to get functions in a module

    Python dir() function is used to display the names of all the functions and variables present in a module. The function produces the most relevant result rather than the complete result as it list public and non-public functions.

    Example

    The below code gives an example of using dir() to get the relevant functions of math module.

    # Importing math module import math as mt # Printing all the functions in math module using dir print(dir(mt))

    Output

    The output displays the most relevant function in the math module.

    ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

    Using __all__ to get functions in a module

    The __all__ gives a list of all public functions which are imported when using import *. It gives all the functions that do not start with an underscore (_) before them. Modules that do not define __all__ will throw an AttributeError if a user tries to get the functions within that module.

    Example

    The code below gives a demo of using __all__ to display different functions in re module.

    # Importing re module import re # Printing different functions in re module print(re.__all__)

    Output

    The output gives the different functions present in re module.

    ['match', 'fullmatch', 'search', 'sub', 'subn', 'split', 'findall', 'finditer', 'compile', 'purge', 'template', 'escape', 'error', 'Pattern', 'Match', 'A', 'I', 'L', 'M', 'S', 'X', 'U', 'ASCII', 'IGNORECASE', 'LOCALE', 'MULTILINE', 'DOTALL', 'VERBOSE', 'UNICODE']

    Using inspect to get functions in a module

    Python inspect library can be used to get the functions under any module. The getmembers() function gets all the functions and variables inside a module and then isfunction filters to show only the functions. Using inspect allows for all functions in a module to be displayed unlike dir().

    Example

    The code given below shows use of getmembers and isfunction in inspect library to get functions of a module.

    # Importing getmembers and isfunction from inspect from inspect import getmembers, isfunction # Importing math module import math as mt # Printing all the functions in math module print(getmembers(mt), isfunction)

    Output

    The output lists all the functions present in the math module.

    [(‘__doc__’, ‘This module provides access to the mathematical functions\ndefined by the C standard.’), (‘__loader__’, ), (‘__name__’, ‘math’), (‘__package__’, »), (‘__spec__’, ModuleSpec(name=’math’, loader=, origin=’built-in’)), (‘acos’, ), (‘acosh’, ), (‘asin’, ), (‘asinh’, ), (‘atan’, ), (‘atan2’, ), (‘atanh’, ), (‘ceil’, ), (‘comb’, ), (‘copysign’, ), (‘cos’, ), (‘cosh’, ), (‘degrees’, ), (‘dist’, ), (‘e’, 2.718281828459045), (‘erf’, ), (‘erfc’, ), (‘exp’, ), (‘expm1’, ), (‘fabs’, ), (‘factorial’, ), (‘floor’, ), (‘fmod’, ), (‘frexp’, ), (‘fsum’, ), (‘gamma’, ), (‘gcd’, ), (‘hypot’, ), (‘inf’, inf), (‘isclose’, ), (‘isfinite’, ), (‘isinf’, ), (‘isnan’, ), (‘isqrt’, ), (‘ldexp’, ), (‘lgamma’, ), (‘log’, ), (‘log10’, ), (‘log1p’, ), (‘log2’, ), (‘modf’, ), (‘nan’, nan), (‘perm’, ), (‘pi’, 3.141592653589793), (‘pow’, ), (‘prod’, ), (‘radians’, ), (‘remainder’, ), (‘sin’, ), (‘sinh’, ), (‘sqrt’, ), (‘tan’, ), (‘tanh’, ), (‘tau’, 6.283185307179586), (‘trunc’, )]

    Источник

Оцените статью