Python for Ethical Hackers
Mohammad reza Kamalifard
Python Language Essentials
Part 6 :
Creating Modules
Modules
Better way to orgnizing code
can define classes,functions and variables
Import a module:
import MODULE_NAME
form M...
Creating Module
$ vim calculator.py
#!/usr/bin/env python
'''
This is a simple calculator
add for addition
sub for subtrac...
Creating Module
$ vim calculator.py
#!/usr/bin/env python
import calculator
print '2 + 3 is equal to : %d ' % calculator.a...
from … import
$ vim testcalc.py
#!/usr/bin/env python
from calculator import add
print '2 + 3 is equal to : %d ' % calcula...
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
×

اسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونی

100
-1

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
100
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
  2. 2. Python Language Essentials Part 6 : Creating Modules
  3. 3. Modules Better way to orgnizing code can define classes,functions and variables Import a module: import MODULE_NAME form MODULE_NAME import ….
  4. 4. Creating Module $ vim calculator.py #!/usr/bin/env python ''' This is a simple calculator add for addition sub for subtraction ''' def add(a, b): return a+b def sub(a, b): return a-b ~ ~
  5. 5. Creating Module $ vim calculator.py #!/usr/bin/env python import calculator print '2 + 3 is equal to : %d ' % calculator.add(2,3) print '10 - 3 is equal to : %d ' % calculator.sub(10,3) ~ ~ $ chmod a+x testcalc.py $ ./testcalc.py 2 + 3 is equal to : 5 10 - 3 is equal to : 7
  6. 6. from … import $ vim testcalc.py #!/usr/bin/env python from calculator import add print '2 + 3 is equal to : %d ' % calculator.add(2,3) print '10 - 3 is equal to : %d ' % calculator.sub(10,3) ~ $ ./testcalc.py 2 + 3 is equal to : 5 Traceback (most recent call last): File "./testcalc.py", line 6, in <module> print '10 - 3 is equal to : %d ' % sub(10,3) NameError: name 'sub' is not defined
  7. 7. 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.

×