Hi, guys, as mentioned in the post earlier, everything in python is object although there are few built-in objects which can be referred as built-in data types in Python. Let’s see built-in data types and functions in python and categorize them:

Built-in Data Types in Python

  • Numbers (separate sub-types; three are integer types)
    • Integer
      • Boolean
      • Long integer
    • Floating point real number
    • Complex number
  • String
  • List
  • Tuple
  • Dictionary

All the different object types in Python are classified into the above given built-in datatypes. For example, int type consists of boolean, long integer, floating numbers as well as complex numbers. It should be noted that only Python provides and allows the user to take input in the Complex Numbers and process it. You can do all kinds of mathematical operations with Complex Numbers in Python without doing any kind of pre-processing on it unlike other programming languages.

Let’s move on to functions now.

Functions in Python

There are four types of functions in Python:

In Python each function definition by the user is of the form:

def name of function (list of formal parameters):

  body of function

For example, we could define the function max by the code

Click here to Copy the Code

Note: There is no function overloading in Python.

Let’s take an example to understand scoping in user-defined functions in Python:

Click here to Copy the Code

Output:

Click here to Copy the Code

Let’s trace the above example and its output:

First what we did was defined a function f in which we passed x as a formal parameter. Now we are calling this function after defining the values of x and y at z. Now in this case at line 8, x is used as actual parameter when called at z = f(x) and the value of x is passed as 3. Thus, the function returns that value of x, which is 4 due to addition at line 3. Hence, the first line of output where x=4, is actually signifying the value of x inside the scope of function f where it’s value at line 4.

Also, as the function f returns with value 4, the value of z becomes 4, which is printed in the line 2 of output. At line 3 and line 4 of output, the value of x and y are printed which are initialized at the beginning of the program and are out of the scope of the function f. Hope, this clears the scoping in user-defined function. Scoping works nearly in the same manner across all the different types of functions.

These are the functions which are already built-in the python interpreter and the user can directly call it as and when required. We will some important built-in functions.

abs()

One of the most popular built-in Python function is abs(). The abs() function returns the absolute value of a number. A negative value’s absolute is that value’s positive.

Click here to Copy the Code

all()

The all() function takes a container as an argument. This Built in Functions returns True if all values in a python iterable have a Boolean value of True. An empty value has a Boolean value of False.

Click here to Copy the Code

any()

Like all(), it takes one argument and returns True if, even one value in the iterable has a Boolean value of True.

Click here to Copy the Code

bin()

bin() converts an integer to a binary string.

Click here to Copy the Code

bool()

bool() converts a value to Boolean.

bytearray()

bytearray() function returns a python array of a given byte size.

Lambda Function

Lambda function definition does not include a “return” statement — it always contains an expression which is returned. Also note that you can put a lambda definition anywhere a function is expected, and you don’t have to assign it to a variable at all.

Example:

Click here to Copy the Code

You can now create multiple different incrementor functions and assign them to variables, then use them independent from each other. As the last statement demonstrates, you don’t even have to assign the function anywhere — you can just use it instantly and forget it when it’s not needed anymore.

These are the functions which are recursive in nature. We can understand it directly using an example:

Click here to Copy the Code

The recursiveness in the example is clearly visible here.

Let us go in-depth of user-defined functions and its various sub-types:

Functions without Returns

  • Even if there is no return line written inside the code, all Python functions have a return value
  • Python functions without a return statement returns a special value None
    • None is a special constant in the python language
    • None in python is used like NULL, void, or nil in other languages
    • None in python is also logically equivalent to False
    • The interpreter’s REPL doesn’t print None

Default Values for Arguments

  • You can provide default values for a function’s arguments
  • These arguments are optional when the function is called. Example:
Click here to Copy the Code

All of the above function calls return 8.

Keyword Arguments

  • We can call a python function with some or all of its arguments out of order as long as we specify their names
Click here to Copy the Code
  • Can be combined with defaults, too
Click here to Copy the Code

So guys, in this post I told everything on functions in Python as well as we saw built-in datatypes. In next post, I will write about specifications, global variables,  Modules and Packages in Python.