In Python, functions provide organized blocks of reusable code.
Typically, this allows a programmer to write a block of code to perform a
single, related action. Functions are reusable pieces of programs. They
allow you to give a name to a block of statements and you can run that
block using that name anywhere in your program and any number of times.
This is known as calling the function.
Function Definition: The def and return Statements:
Functions are defined using the def keyword.
We create a function with a def statement and The return statement specifies the result value of the function :
def odd( spin ):
"""Return true if this spin is odd."""
if spin % 2 == 1:
return True
return False
Function Parameters
A function can take parameters which are just values you supply to the function so that the function can do something utilising
those values. These parameters are just like variables except that the values of these variables are defined when we call the
function and are not assigned values within the function itself.
Parameters are specified within the pair of parentheses in the function definition, separated by commas.
Keyword Arguments :
If you have some functions with many parameters and you want to specify only some of them, then you can give values for such
parameters by naming them - this is called keyword arguments
Using Keyword Arguments
#!/usr/bin/python
def func(a, b=5, c=10):
print 'a is', a, 'and b is', b, 'and c is', c
func(3, 7)
func(25, c=24)
func(c=50, a=100)
Output :
>>> python func.py
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
In the first usage, func(3, 7), the parameter a gets the value 3, the parameter b gets the value 5 and
c gets the default value of 10.
In the second usage func(25, c=24), the variable a gets the value of 25 due to the position of the
argument. Then, the parameter c gets the value of 24 due to naming i.e. keyword arguments. The variable
b gets the default value of 5.
In the third usage func(c=50, a=100), we use keyword arguments completely to specify the values.
Type conversion :
Python provides a collection of built-in functions that convert values from one type to another. The int function takes any value and converts it to an integer, if possible :
>>> int("32")
32
>>> int("Hello")
ValueError: invalid literal for int(): Hello
If you like this post or have any question, please feel free to comment!
Function Definition: The def and return Statements:
Functions are defined using the def keyword.
We create a function with a def statement and The return statement specifies the result value of the function :
def odd( spin ):
"""Return true if this spin is odd."""
if spin % 2 == 1:
return True
return False
Function Parameters
A function can take parameters which are just values you supply to the function so that the function can do something utilising
those values. These parameters are just like variables except that the values of these variables are defined when we call the
function and are not assigned values within the function itself.
Parameters are specified within the pair of parentheses in the function definition, separated by commas.
Keyword Arguments :
If you have some functions with many parameters and you want to specify only some of them, then you can give values for such
parameters by naming them - this is called keyword arguments
Using Keyword Arguments
#!/usr/bin/python
def func(a, b=5, c=10):
print 'a is', a, 'and b is', b, 'and c is', c
func(3, 7)
func(25, c=24)
func(c=50, a=100)
Output :
>>> python func.py
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
In the first usage, func(3, 7), the parameter a gets the value 3, the parameter b gets the value 5 and
c gets the default value of 10.
In the second usage func(25, c=24), the variable a gets the value of 25 due to the position of the
argument. Then, the parameter c gets the value of 24 due to naming i.e. keyword arguments. The variable
b gets the default value of 5.
In the third usage func(c=50, a=100), we use keyword arguments completely to specify the values.
Type conversion :
Python provides a collection of built-in functions that convert values from one type to another. The int function takes any value and converts it to an integer, if possible :
>>> int("32")
32
>>> int("Hello")
ValueError: invalid literal for int(): Hello
If you like this post or have any question, please feel free to comment!
This comment has been removed by a blog administrator.
ReplyDeleteThanks for sharing your valuable information
ReplyDeletesalesforce cpq training
salesforce cpq online training
Thanks for sharing your valuable information
ReplyDeletePython Online Course Training
Best Python Online Course
I-C-Q 752822040
ReplyDeleteTeLe GrAm @killhacks
All types of Fresh FUllZ Available in bulk quantity
SSN+DOB
SSN+DOB+DL
High Credit Scroes Fullz (USA)
CC FULLZ WITH CVV (vbv/non vbv)
DUMPS WITH PIN CODES TRACK 101 & 202
HAC-KING/SPA-MMING/CAR-DING/SCR-iPTING
All Tools & Complete Tutorials Guide
Cpan-els/Shells
Key-loggers/RAT-S
SM-TP/RDP
MAIL-ERS
DE-EP/DAR-K WEB COMPLETE COURSE
I-C-Q 752822040
TeLe GrAm @killhacks
CONTACT