0% found this document useful (0 votes)
3 views3 pages

Python Function Examples With Explanations

The document provides examples of various Python programming concepts including lists, functions, lambda expressions, and functional programming methods such as map, reduce, and filter. It demonstrates how to create lists, define functions, perform mathematical operations, and manipulate data using these concepts. Each example is accompanied by code snippets illustrating the functionality.

Uploaded by

seungmiin1020
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Python Function Examples With Explanations

The document provides examples of various Python programming concepts including lists, functions, lambda expressions, and functional programming methods such as map, reduce, and filter. It demonstrates how to create lists, define functions, perform mathematical operations, and manipulate data using these concepts. Each example is accompanied by code snippets illustrating the functionality.

Uploaded by

seungmiin1020
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Function / Lambda / Map / Reduce / Filter

Examples

List example
Create a list from 1 to 10 and print the list and its length.
L = list(range(1,11))
print(L)
print(len(L))

Function example
Define a function that prints each element of a list.
def print_list(a):
for i in a:
print(i)

print_list(L)

Simple function
Function without parameters that prints two sentences.
def boy():
print('I am a boy.')
print('You are a girl')

boy()

Condition function
Compare two numbers and print the relation between them.
def functon(a,b):
if a > b:
print('a is greater than b')
elif a == b:
print('a is equal to b')
else:
print('a is less than b')

functon(10,100)

Digit counting
Convert a number to string and count the number of digits.
def numOfDigit(a):
if a > 0:
print(len(str(a)))

numOfDigit(31823719827391823)
Multiplication table
Print multiplication tables starting from a given number.
def square(a):
for i in range(a,10):
for j in range(1,10):
print(f'{i} x {j} = {i*j}')

square(6)

Return example
Return the value of a linear function y = ax + b.
def f1(x,a,b):
return a*x + b

c = f1(10,10,10)
print(c)

Triangle area
Return the area of a triangle using integer division.
def triangle(a,b):
return a*b//2

print(triangle(10,10))

Lambda example
Anonymous function that adds two numbers.
b = (lambda x,y: x + y)(10,20)
print(b)

Map example
Apply a function to every element in a list.
print(list(map(lambda x: x*2, [1,2,3,4,5])))

Reduce example
Accumulate values in a list using a function.
from functools import reduce
print(reduce(lambda x,y: x+y, [1,2,3,4,5]))

Filter example
Filter elements that satisfy a condition.
print(list(filter(lambda x: x <= 5, range(10))))

You might also like