NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io



### **Python Function**

def hello():
print('Hello World!')

# call the function
hello()

print('Outside function')

# function with two arguments
def add_numbers(num1, num2):
sum = num1 + num2
print("Sum: ",sum)

# function call with two values
add_numbers(15, 41)


# function definition
def find_square(num):
result = num * num
return result

# function call
square = find_square(3)

print('Square:',square)

import math

# sqrt computes the square root
square_root = math.sqrt(4)

print("Square Root of 4 is",square_root)

# pow() comptes the power
power = pow(2, 3)

print("2 to the power 3 is",power)

### **Benefits of Using Functions**

# function definition
def get_square(num):
return num * num

for i in [1,2,3]:
# function call
result = get_square(i)
print('Square of',i, '=',result)

### **Function Argument with Default Values**

def add_numbers( a = 7, b = 8):
sum = a + b
print('Sum:', sum)


# function call with two arguments
add_numbers(2, 3)

# function call with one argument
add_numbers(a = 2)

# function call with no arguments
add_numbers()

# Python Keyword Argument

def display_info(first_name, last_name):
print('First Name:', first_name)
print('Last Name:', last_name)

display_info(last_name = 'Iqbal', first_name = 'Tamim')

# Python Function With Arbitrary Arguments

# program to find sum of multiple numbers
def find_sum(*numbers):
result = 0

for num in numbers:
result = result + num

print("Sum = ", result)

# function call with 3 arguments
find_sum(1, 2, 3)

# function call with 2 arguments
find_sum(4, 9)

### **Python Recursive Functiont**

def factorial(x):

if x == 1:
return 1
else:
return (x * factorial(x-1))


num = 5
print("The factorial of", num, "is", factorial(num))

### **Python Lambda/Anonymous Function**



```
# We use the lambda keyword instead of def to create a lambda function.
lambda argument(s) : expression
Here,
argument(s) - any value passed to the lambda function
expression - expression is executed and returned
```



# declare a lambda function
greet = lambda : print('Hello World')

# call lambda function
greet()

# lambda that accepts one argument
greet_user = lambda fname, lname : print('First Name:', fname, ' Last Name: ', lname)

# lambda call
greet_user('Atique', 'Rahman')

### **Global, Local and Nonlocal**

# Python Local Variables

def greet():

# local variable
message = 'Hello'

print('Local', message)

greet()

# try to access message variable
# outside greet() function
print(message)

# declare global variable
message = 'Hello'

def greet():
# declare local variable
print('Local', message)

greet()
print('Global', message)

# outside function
def outer():
message = 'local'

# nested function
def inner():

# declare nonlocal variable
nonlocal message

message = 'nonlocal'
print("inner:", message)

inner()
print("outer:", message)

outer()

def outer_function():
num = 20

def inner_function():
global num
num = 25

print("Before calling inner_function(): ", num)
inner_function()
print("After calling inner_function(): ", num)

outer_function()
print("Outside both function: ", num)

     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.