SlideShare una empresa de Scribd logo
1 de 18
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 :
Functions
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.
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(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. 
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
>>> print5times('Javad')
Javad
Javad
Javad
Javad
Javad
>>> print5times('Hello, World!')
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
>>>
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. 
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(3, 4)
x = 3 , x = 4
arbitrary: ()
>>>arbitrary(3, 4, "Hello World", 5 ,6)
x = 3 , x = 4
arbitrary: ('Hello World', 5, 6)
$ 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']
$
$
$./testargv.py -h 199 --app firefox
['./testargv.py', '-h', '199', '--app', 'firefox']
$
$ ./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
$ 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])
~
~
$ ./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
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
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 .

Más contenido relacionado

La actualidad más candente

Python Unit Test
Python Unit TestPython Unit Test
Python Unit Test
David Xie
 
Python summer course play with python (lab1)
Python summer course  play with python  (lab1)Python summer course  play with python  (lab1)
Python summer course play with python (lab1)
iloveallahsomuch
 
Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...
Timo Stollenwerk
 
C++ course start
C++ course startC++ course start
C++ course start
Net3lem
 

La actualidad más candente (20)

Python Unit Test
Python Unit TestPython Unit Test
Python Unit Test
 
Introducing Elixir and OTP at the Erlang BASH
Introducing Elixir and OTP at the Erlang BASHIntroducing Elixir and OTP at the Erlang BASH
Introducing Elixir and OTP at the Erlang BASH
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
Python summer course play with python (lab1)
Python summer course  play with python  (lab1)Python summer course  play with python  (lab1)
Python summer course play with python (lab1)
 
Don't do this
Don't do thisDon't do this
Don't do this
 
Python
PythonPython
Python
 
Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...
 
FParsec Hands On - F#unctional Londoners 2014
FParsec Hands On -  F#unctional Londoners 2014FParsec Hands On -  F#unctional Londoners 2014
FParsec Hands On - F#unctional Londoners 2014
 
C++ course start
C++ course startC++ course start
C++ course start
 
Profiling php5 to php7
Profiling php5 to php7Profiling php5 to php7
Profiling php5 to php7
 
Python Async IO Horizon
Python Async IO HorizonPython Async IO Horizon
Python Async IO Horizon
 
Pyunit
PyunitPyunit
Pyunit
 
A deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio moduleA deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio module
 
Functions
FunctionsFunctions
Functions
 
Python Coroutines, Present and Future
Python Coroutines, Present and FuturePython Coroutines, Present and Future
Python Coroutines, Present and Future
 
Python - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave ParkPython - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave Park
 
Python unittest
Python unittestPython unittest
Python unittest
 
Perl one-liners
Perl one-linersPerl one-liners
Perl one-liners
 
Python Programming Essentials - M8 - String Methods
Python Programming Essentials - M8 - String MethodsPython Programming Essentials - M8 - String Methods
Python Programming Essentials - M8 - String Methods
 
Perl one liners
Perl one linersPerl one liners
Perl one liners
 

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

Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
Maulik Borsaniya
 
Clojure - A new Lisp
Clojure - A new LispClojure - A new Lisp
Clojure - A new Lisp
elliando dias
 

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

Python Learn Function with example programs
Python Learn Function with example programsPython Learn Function with example programs
Python Learn Function with example programs
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Python basic
Python basicPython basic
Python basic
 
Pythonlearn-04-Functions (1).pptx
Pythonlearn-04-Functions (1).pptxPythonlearn-04-Functions (1).pptx
Pythonlearn-04-Functions (1).pptx
 
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
 
Funkcija, objekt, python
Funkcija, objekt, pythonFunkcija, objekt, python
Funkcija, objekt, python
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Pemrograman Python untuk Pemula
Pemrograman Python untuk PemulaPemrograman Python untuk Pemula
Pemrograman Python untuk Pemula
 
Function in Python [Autosaved].ppt
Function in Python [Autosaved].pptFunction in Python [Autosaved].ppt
Function in Python [Autosaved].ppt
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
 
Php Intermediate
Php IntermediatePhp Intermediate
Php Intermediate
 
اسلاید اول جلسه پنجم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه پنجم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه پنجم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه پنجم کلاس پایتون برای هکرهای قانونی
 
Functions.pdf
Functions.pdfFunctions.pdf
Functions.pdf
 
Functionscs12 ppt.pdf
Functionscs12 ppt.pdfFunctionscs12 ppt.pdf
Functionscs12 ppt.pdf
 
Clojure - A new Lisp
Clojure - A new LispClojure - A new Lisp
Clojure - A new Lisp
 
Functions_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdf
 
Functions in python3
Functions in python3Functions in python3
Functions in python3
 
What's new in Python 3.11
What's new in Python 3.11What's new in Python 3.11
What's new in Python 3.11
 
Python_UNIT-I.pptx
Python_UNIT-I.pptxPython_UNIT-I.pptx
Python_UNIT-I.pptx
 

Más de Mohammad Reza Kamalifard

اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
Mohammad Reza Kamalifard
 

Más de Mohammad Reza Kamalifard (20)

Internet Age
Internet AgeInternet Age
Internet Age
 
Introduction to Flask Micro Framework
Introduction to Flask Micro FrameworkIntroduction to Flask Micro Framework
Introduction to Flask Micro Framework
 
Pycon - Python for ethical hackers
Pycon - Python for ethical hackers Pycon - Python for ethical hackers
Pycon - Python for ethical hackers
 
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacyTehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy
 
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
 
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
 
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
 
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونیاسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی
 
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
 
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
 
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
 

Último

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Último (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 

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

  • 1. Python for ethical hackers Mohammad reza Kamalifard kamalifard@datasec.ir
  • 2. Python language essentials Module 1: Introduction to Python and Setting up an Environment for Programing Part 5 : Functions
  • 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. Functions • Functions allow sections of code to be grouped better as per functionality
  • 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. 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. >>> print5times('Javad') Javad Javad Javad Javad Javad >>> print5times('Hello, World!') Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! >>>
  • 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. def add(x, y): """Return x plus y""" return x + y >>> add(4, 5) 9 >>> add(8, 3) 11 >>>
  • 10. def add(x, y = 5): """Return x plus y""" return x + y >>> add(4) 9 >>> add(8, 3) 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. $ vim testargv.py #!/usr/bin/env python import sys print sys.argv ~ ~
  • 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. $ ./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. $ 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. $ ./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. 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. 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 .