You are on page 1of 2

Ques: 4: (a): Functions: A function is a block of organized, reusable code that is used to perform a

single, related action. Functions provide better modularity for your application and a high degree of
code reusing. Python gives you many built-in functions like print(), etc. but you can also create your
own functions. These functions are called user-defined functions.

Example: # Function definition is here


def printme( str ):
"This prints a passed string into this function"
print str
return;

# Now you can call printme function


printme("Call user ")
printme("Again Call user ")

Modules: A module allows you to logically organize your Python code. Grouping related code into a
module makes the code easier to understand and use. A module is a Python object with arbitrarily
named attributes that you can bind and reference. Simply, a module is a file consisting of Python
code. A module can define functions, classes and variables. A module can also include runnable code.

Example: def print_func( par ):


print "Hello : ", par
return
# Import module support
import support

# Now you can call defined function that module as follows


support.print_func("Ram is a good boy")

Ques:4: (b): It represents a categorical variable. Categoricals are a pandas data type that corresponds to
the categorical variables in statistics. Such variables take on a fixed and limited number of possible
values. For examples – grades, gender, blood group type etc.
Also, in the case of categorical variables, logical order is not the same as categorical data e.g. “one”,
“two”, “three”. But the sorting of these variables uses logical order.

Example: # Python code explaining 


# numpy.pandas.Categorical()
  
# importing libraries
import numpy as np
import pandas as pd
  
# Categorical using dtype
c = pd.Series(["a", "b", "d", "a", "d"], dtype ="category")
print ("\nCategorical without pandas.Categorical() : \n", c)
  
  
c1 = pd.Categorical([1, 2, 3, 1, 2, 3])
print ("\n\nc1 : ", c1)
  
c2 = pd.Categorical(['e', 'm', 'f', 'i',
                     'f', 'e', 'h', 'm' ])
print ("\nc2 : ", c2)

You might also like