Python Function-Practice Question

Monalisha Kumari
2 min readNov 3, 2022

--

Functions

1. Write a function to find out length of a string without using inbuilt( len) function.

def test(s):
"""this function will help you to find length of string"""
count=0
for i in s:
#if type(i)==str:(we can use also or not use)
count=count+1
return count

Call a function-

  test("mona")
-->4

2. Write a function which will able to print a index of list element without using index function.

l=[1,2,3,4,"mona","lisha"] # This is a listdef test1(li):
for i in range(0,len(li)):
print(li[i],":",i)

Call a function-

test1(l)
-->
1 : 0
2 : 1
3 : 2
4 : 3
mona : 4
lisha : 5

3. Write a function which will be able to print ip address of system.

import socket #socket is a librarysocket.gethostbyname(socket.gethostname()) #This will give ip addr.def test2():
ip=socket.gethostbyname(socket.gethostname())
return ip

Call a function-

    test2()
--> 127.0.0.1

4. Write a function which will take input as a list with any kind of numeric value and give an out as a multiplication of all the numeric data.

l=[3.5,6.56,4,5,"mona","lisha","bootcamp 2.0"]
def test3(l):
mul=1
for i in l:
if type(i)==int or type(i)==float:
mul=mul*i
return mul

Call a function-

   test3(l)
-->459.19999999999993

5. Write a function which will shutdown your system.

import os
os.system("shutdown /s /t 4") # shutdown system after 4 second
# /s-shutdown
# /r-restart
# /t timer

Below video will help you to do more practice on python main function.-

I hope this will help you to learn about python function. Till then Keep learning and Keep growing..

--

--