You are on page 1of 3

Your function takes some input or inputs and give some output / outputs

Basically u r telling python that you want to create function by using def

Def function_name (parameter/argument):

Code or logic

Return - This is used to exit the function

Reading data from different file formats like xlsx, csv, json, txt, database,

Create a function where your code will read data from different file formats

Purpose of the function is reading the data (put comments along the code)

4D

Data -

Descriptive - 100

Develop - 500

Deploy – 200 lines

Positional arguments

Default arguments

Named arguments

def string_operations(inp_str):
  upper_case_chars = 0
  lower_case_chars = 0
  for char in inp_str:
    if char.isupper():
      upper_case_chars = upper_case_chars + 1
    elif char.islower():
      lower_case_chars = lower_case_chars + 1
  return upper_case_chars, lower_case_chars

objective of the code is to count the number of lowercase and uppercase characters

Use of for loop is to travel the whole string (inp_str) – the argument which is passed inside the
function
Now check the condition if the character is uppercase, if so then add to uppercase count

Then check the condition if the character is lowercase,if so then add to lowercase count

In summary,

Function is not logic. It is just design of summarising and organising your


code so that it can be reused again.

Function structure:
Def function_name (argument/parameter):
Lines of code
Return

Different argument types in functions:


Positional arguments - multiply(5,7) mostly used
Default arguments - multiply(number1, number2=5)
Named arguments - divide(num2=3, num1=12)
Variable length arguments - *args – When the number of parameters to be
passed inside the function is not defined – widely used
Variable length arguments - **kwargs – When we want our parameters in a
key value pair

Supporting functions in python:


Purpose of lambda function is to reduce the lines of code
Map is used when the size of the input list is equal to size of the output list
(Eg: Square of each element of a list)
Filter is used when input list size is not equal to output list size (Eg: Filtering
out odd numbers or even numbers)
Reduce is used when input list size is not equal to output list size (Eg: Sum of
multiple numbers)

Practice 100+ questions from Great Learning


Most important thing is understanding every question as well as the solution
You will get confident only when you are first able to retain these 100 +
solutions

You might also like