1. Home
  2. Docs
  3. Python 101 – Python Programming for Beginners
  4. Built in Functions

Built in Functions

Built in Functions

 

Python has number of built in functions. And are always available without,

importing any external python code via modules or packages.

 

List of built in function in Python 3.6.3

 

built in functions in python

Most common ones are.
In this section you be familiarized with most common built in function.

Generic built in functions

print()

Syntax:

print(…)

print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)

You can also specify optional keywords arguments.

sep – Shorthand for separator, which string inserted between values, by default values are separted by a space

end – A string appended after last value, defaut is a newline

file – File object, where the output to be printed, default is sys.stdout

flush – Whether the stream to be forcibly flushed.

The order of the arguments doesn’t matter.

Perhaps the most used common built in function.

You have already used this in Hello, World! program.

print() – Prints the values to a stream, default output stream is sys.stdout.

in Python 2.7.x version print was just statement and in python 3 its a function.

On Python shell, if you type “hello, World” , it will return same string back,

When a print() function used on string, it prints the values to stream.

 

>> “Hello, World”

‘Hello, World’

>>> print(“Hello, World”)
Hello, World
>>>

Observe the quotes.

Lets look at some more print() examples

Examples:

1. Without any argument
>>> print()

>>>

 

Prints nothing on screen

2. print a string to screen

>>> print(“This is a string”)
This is a string

3. Print two string with a “:” separator

>>> print(“this is a string”, “this is another string”, sep = “:” )
this is a string:this is another string

 

4.end the print with a ‘\t’ instead of newline

>>> print(“this is a string”, “this is another string”, sep = “:”, end = “\t”)

this is a string:this is another string >>>

 

Note compared to previous output, now the prompt is along with the output string.

 

help()

One of the most useful built-in function.

It’s an interactive utility to get help while programming.

You can get help on, keywords, modules, symobols, topics, builtins etc.

Lets look at the some examples

help() can be used in two ways

on Python interpreter

1. Use help()funciton with an argument

Ex:

help(“modules”) # note modules is in quoates
help(“int”)
help(“str”)
2. First invoke help(),

it starts help utility and then check required topic

Ex:
help()
>>> help()

help>modules # note modules is not in quoates

#This lists availabe modules
to get the help on a perticular module,enter module name.

Ex:
>>> os

 

To get a list of modules installed.
help(“modules”)

Lists available symbols.

help(“symobols”)

Lists the available topics

help(“topics”)

Gets the list of built-in functions, exception and other objects

help(“builtins”)

 

dir()

Buit-in function lists the attributes of the object in the current namespace.

If used without argument, returns names in current namespace.

Examples:

dir() without an argument

>>> dir()
[‘__annotations__’, ‘__builtins__’, ‘__doc__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’]

After importing a module in the namespace, note ‘os’

>>> import os
>>> dir()
[‘__annotations__’, ‘__builtins__’, ‘__doc__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘os’]

 

Another module is imported, note ‘os’,’sys’

>>> import sys
>>> dir()
[‘__annotations__’, ‘__builtins__’, ‘__doc__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘os’, ‘sys’]

dir() with an argument

Example 1.

Names of string object

>>> say_hello = “Hello, World!”

Lists all names of string object

>>> dir(say_hello)
[‘__add__’, ‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__getnewargs__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__iter__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__mod__’, ‘__mul__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__rmod__’, ‘__rmul__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘capitalize’, ‘casefold’, ‘center’, ‘count’, ‘encode’, ‘endswith’, ‘expandtabs’, ‘find’, ‘format’, ‘format_map’, ‘index’, ‘isalnum’, ‘isalpha’, ‘isdecimal’, ‘isdigit’, ‘isidentifier’, ‘islower’, ‘isnumeric’, ‘isprintable’, ‘isspace’, ‘istitle’, ‘isupper’, ‘join’, ‘ljust’, ‘lower’, ‘lstrip’, ‘maketrans’, ‘partition’, ‘replace’, ‘rfind’, ‘rindex’, ‘rjust’, ‘rpartition’, ‘rsplit’, ‘rstrip’, ‘split’, ‘splitlines’, ‘startswith’, ‘strip’, ‘swapcase’, ‘title’, ‘translate’, ‘upper’, ‘zfill’]

Example 2.

Lists all names of list object

>>> a = []
>>> dir(a)
[‘__add__’, ‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__delitem__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__gt__’, ‘__hash__’, ‘__iadd__’, ‘__imul__’, ‘__init__’, ‘__init_subclass__’, ‘__iter__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__mul__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__reversed__’, ‘__rmul__’, ‘__setattr__’, ‘__setitem__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘append’, ‘clear’, ‘copy’, ‘count’, ‘extend’, ‘index’, ‘insert’, ‘pop’, ‘remove’, ‘reverse’, ‘sort’]

id()

input()

len()

enumerate()

type()

vars()

open()

 

 

Numeric built in functions:

int()

float()

complex()

abs()

pow()

round()

devmod()

 

#Sequence built-ins

 

str()

list()

tuple()

unicode()

bytes()

bytearray()

len()

max()

min()

reversed()