Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

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

در این جلسه در خصوص توابع در پایتون صحبت کردیم

Mohammad reza Kamalifard

November 18, 2013
Tweet

More Decks by Mohammad reza Kamalifard

Other Decks in Education

Transcript

  1. Python language essentials Module 1: Introduction to Python and Setting

    up an Environment for Programing Part 5 : Functions
  2. 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.
  3. 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.
  4. 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 Functions
  5. >>> print5times('Javad') Javad Javad Javad Javad Javad >>> print5times('Hello, World!')

    Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! >>>
  6. 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.
  7. def add(x, y): """Return x plus y""" return x +

    y >>> add(4, 5) 9 >>> add(8, 3) 11 >>>
  8. def add(x, y = 5): """Return x plus y""" return

    x + y >>> add(4) 9 >>> add(8, 3) 11 >>>
  9. 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)
  10. $ 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'] $
  11. $ ./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
  12. $ 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]) ~ ~
  13. $ ./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
  14. 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
  15. 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 .