Python for ethical hackers
Mohammad reza Kamalifard
kamalifard@datasec.ir
Python language essentials
Module 1:
Introduction to Python and Setting up an Environment for Programing
Part 5 :
Function...
Functions
• Functions are a construct to structure programs.
• Functions are used to utilize code in more than one place

...
Functions
• Functions allow sections of code to be grouped better as

per functionality
Functions
A function in Python is defined by a def statement. The
general syntax looks like this:
def function-name(Parame...
Functions
def function(arg1,arg2=default,..):
do_something
return value
>>> def print5times(input):
...
for count in range...
>>> print5times('Javad')
Javad
Javad
Javad
Javad
Javad
>>> print5times('Hello, World!')
Hello, World!
Hello, World!
Hello,...
return statement
• Function bodies can contain a return statement.
•  Can be anywhere in the function body.
• Ends the exe...
def add(x, y):
"""Return x plus y"""
return x + y
>>> add(4, 5)
9
>>> add(8, 3)
11
>>>
def add(x, y = 5):
"""Return x plus y"""
return x + y
>>> add(4)
9
>>> add(8, 3)
11
>>>
Arbitrary Number of Parameters
def arbitrary(x, y, *more):
print "x=", x, ", x=", y
print "arbitrary: ", more
>>>arbitrary...
$ vim testargv.py
#!/usr/bin/env python
import sys
print sys.argv
~
~
$ chmod a+x testargv.py
$ ./testargv.py
['./testargv.py']
$
$ ./testargv.py salam
['./testargv.py', 'salam']
$
$
$./testar...
$ ./function.py salam
salam
salam
salam
salam
salam
$
$ ./function.py salam hamid
salam
salam
salam
salam
salam
$
$ ./func...
$ vim function.py
#!/usr/bin/env python
import sys
def print5times(content_to_print):
for count in range(0,5):
print conte...
$ ./function.py salam
salam
salam
salam
salam
salam
$
$ ./function.py salam hamid
salam
salam
salam
salam
salam
$
$ ./func...
References
SPSE securitytube training by Vivek Ramachandran
SANS Python for Pentesters (SEC573)
Violent python
Security Po...
This work is licensed under the Creative Commons Attribution-NoDerivs 3.0 Unported License.
To view a copy of this license...
Upcoming SlideShare
Loading in …5
×

جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱

225 views

Published on

در این جلسه در خصوص توابع در پایتون صحبت کردیم
PySec101 Fall 2013 J5E1 By Mohammad Reza Kamalifard
Talk About:
Functions and function call

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
225
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱

  1. 1. Python for ethical hackers Mohammad reza Kamalifard kamalifard@datasec.ir
  2. 2. Python language essentials Module 1: Introduction to Python and Setting up an Environment for Programing Part 5 : Functions
  3. 3. Functions • Functions are a construct to structure programs. • Functions are used to utilize code in more than one place in a program. • The only way without functions to reuse code consists in copying the code.
  4. 4. Functions • Functions allow sections of code to be grouped better as per functionality
  5. 5. Functions A function in Python is defined by a def statement. The general syntax looks like this: def function-name(Parameter list): statements, i.e. the function body • The parameter list consists of none or more parameters. • The function body gets executed every time the function is called. 
  6. 6. Functions def function(arg1,arg2=default,..): do_something return value >>> def print5times(input): ... for count in range(0,5): ... print input ... >>> print5times('Hello') Hello Hello Hello Hello Hello
  7. 7. >>> print5times('Javad') Javad Javad Javad Javad Javad >>> print5times('Hello, World!') Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! >>>
  8. 8. return statement • Function bodies can contain a return statement. •  Can be anywhere in the function body. • Ends the execution of the function call and "returns" the result, i.e. the value of the expression following the return keyword, to the caller. • If there is no return statement in the function code, the function ends, when the control flow reaches the end of the function body. 
  9. 9. def add(x, y): """Return x plus y""" return x + y >>> add(4, 5) 9 >>> add(8, 3) 11 >>>
  10. 10. def add(x, y = 5): """Return x plus y""" return x + y >>> add(4) 9 >>> add(8, 3) 11 >>>
  11. 11. Arbitrary Number of Parameters def arbitrary(x, y, *more): print "x=", x, ", x=", y print "arbitrary: ", more >>>arbitrary(3, 4) x = 3 , x = 4 arbitrary: () >>>arbitrary(3, 4, "Hello World", 5 ,6) x = 3 , x = 4 arbitrary: ('Hello World', 5, 6)
  12. 12. $ vim testargv.py #!/usr/bin/env python import sys print sys.argv ~ ~
  13. 13. $ chmod a+x testargv.py $ ./testargv.py ['./testargv.py'] $ $ ./testargv.py salam ['./testargv.py', 'salam'] $ $ $./testargv.py -h 199 --app firefox ['./testargv.py', '-h', '199', '--app', 'firefox'] $
  14. 14. $ ./function.py salam salam salam salam salam salam $ $ ./function.py salam hamid salam salam salam salam salam $ $ ./function.py 'salam hamid' salam hamid salam hamid salam hamid salam hamid salam hamid
  15. 15. $ vim function.py #!/usr/bin/env python import sys def print5times(content_to_print): for count in range(0,5): print content_to_print print5times(sys.argv[1]) ~ ~
  16. 16. $ ./function.py salam salam salam salam salam salam $ $ ./function.py salam hamid salam salam salam salam salam $ $ ./function.py 'salam hamid' salam hamid salam hamid salam hamid salam hamid salam hamid
  17. 17. References SPSE securitytube training by Vivek Ramachandran SANS Python for Pentesters (SEC573) Violent python Security Power Tools python-course.eu ----------------------------http://www.python-course.eu/functions.php http://www.tutorialspoint.com/python/python_functions.htm
  18. 18. This work is licensed under the Creative Commons Attribution-NoDerivs 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/3.0/ Copyright 2013 Mohammad Reza Kamalifard All rights reserved. Go to Kamalifard.ir/pysec101 to Download Slides and Course martials .

×