PYTHON FOR ETHICAL HACKERS
Mohammad reza Kamalifard
Python Language Essentials
Part 4 :
Conditional Statements
if Statement
Like if in other languages but second if called elif
if test_condition1:
do stuff
do stuff
elif test_conditio...
Python Indentation
• Python uses whitespace indentation, rather than curly braces
or keywords, to delimit blocks; a featur...
C, C++
#include <iostream>
using namespace std;
int main(){
cout<<"Hello, World!";
return 0;
}
C, C++
#include <iostream>
using namespace std;int
main(){cout<<"Hello,World!";return 0;}
Indentation
if name == 'ali':
print 'Your name is ali'
Print ‘Always print this line‘
• 4 space
• Tab
• Google use 2 space...
$ vim if_statement.py
#!/usr/bin/env python
name = raw_input('Please Enter Your name! : ')
if name == 'reza':
print 'You a...
$ chmod a+x if_statement.py
$ ./if_statement.py
Please Enter Your name! : reza
You are reza
And a course instructor
$
$ ./...
While Loops
while statement_is_true:
do stuff
do stuff
break : get out if innermost loop
continue : start the next pass of...
$ vim while_statement.py
#!/usr/bin/env python
age = int(raw_input('Please Enter Your age ? : '))
while age > 10:
print 'Y...
$ chmod a+x while_statement.py
$ ./ while_statement.py
Please Enter Your age ? : 23
You age is grater than 10
You age is g...
$ vim while_statement.py
#!/usr/bin/env python
age = 20
while age > 10:
age = int(raw_input('Please Enter Your age ? : '))...
$ chmod a+x while_statement.py
$ ./ while_statement.py
Please Enter Your age ? : 23
You age is grater than 10
Please Enter...
$ vim while_statement.py
#!/usr/bin/env python
age = 20
while age > 10:
age = int(raw_input('Please Enter Your age ? : '))...
$ chmod a+x while_statement.py
$ ./ while_statement.py
Please Enter Your age ? : 23
You age is grater than 10
Please Enter...
For Loops
for item in object:
do stuff
do stuff
for item in [1, 2, 3]
for item in ['a', 2, '3']
for (x,y) in [('reza', 23)...
>>> for item in [1, 2, 3]:
... print item
...
1
2
3
item is a name can be anything
>>> names = ['Reza', 'Majid', 'Sara', 'Iman']
>>> names
['Reza', 'Majid', 'Sara', 'Iman']
>...
>>> info = [('Reza', 23), ('Sara', 20), ('Shayan', 30),
('AmirAli', 21)]
>>> info
[('Reza', 23), ('Sara', 20), ('Shayan', ...
Emulating C style for loops
C style loops : for(i=0; i<10; i++)
for(i=0; i<10; i++){
cout<<i<<',';
}
0,1,2,3,4,5,6,7,8,9,
range()
range(lower, upper, step)
create a list for use
range(n) – [0,...,n-1]
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9...
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10,2)
[1, 3, 5, 7, 9]
>>>
>>> for number in range(1,10,2):
... pri...
References
• Data model — Python v2.7.5 documentation
• http://docs.python.org/2/reference/datamodel.html#objects-values-a...
Upcoming SlideShare
Loading in...5
×

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

163

Published on

در این بخش در مورد
if , while , for
صحبت می کنیم

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
163
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 4 : Conditional Statements
  3. 3. if Statement Like if in other languages but second if called elif if test_condition1: do stuff do stuff elif test_condition2: do stuff do stuff elif test_condition3: do stuff do stuff else: do stuff
  4. 4. Python Indentation • Python uses whitespace indentation, rather than curly braces or keywords, to delimit blocks; a feature also termed the off- side rule. • An increase in indentation comes after certain statements; a decrease in indentation signifies the end of the current block.
  5. 5. C, C++ #include <iostream> using namespace std; int main(){ cout<<"Hello, World!"; return 0; }
  6. 6. C, C++ #include <iostream> using namespace std;int main(){cout<<"Hello,World!";return 0;}
  7. 7. Indentation if name == 'ali': print 'Your name is ali' Print ‘Always print this line‘ • 4 space • Tab • Google use 2 space • We use 4 space (PEP 8)
  8. 8. $ vim if_statement.py #!/usr/bin/env python name = raw_input('Please Enter Your name! : ') if name == 'reza': print 'You are reza' print 'And a course instructor' elif name == 'Arman': print 'You are Arman' print 'And a student' else: print 'I don't know you‘ ~ ~ ~
  9. 9. $ chmod a+x if_statement.py $ ./if_statement.py Please Enter Your name! : reza You are reza And a course instructor $ $ ./if_statement.py Please Enter Your name! : ali I don’t know you $ $ ./if_statement.py Please Enter Your name! : Arman You are Arman And a student $
  10. 10. While Loops while statement_is_true: do stuff do stuff break : get out if innermost loop continue : start the next pass of the innermost pass : do nothing, placeholder
  11. 11. $ vim while_statement.py #!/usr/bin/env python age = int(raw_input('Please Enter Your age ? : ')) while age > 10: print 'You age is grater than 10' ~ ~ ~
  12. 12. $ chmod a+x while_statement.py $ ./ while_statement.py Please Enter Your age ? : 23 You age is grater than 10 You age is grater than 10 You age is grater than 10 You age is grater than 10 You age is grater than 10 You age is grater than 10 You age is grater than 10 You age is grater than 10 You age is grater than 10 You age is grater than 10 You age is grater than 10 You age is grater than 10 You age is grater than 10 You age is grater than 10 You age is grater than 10 You age is grater than 10
  13. 13. $ vim while_statement.py #!/usr/bin/env python age = 20 while age > 10: age = int(raw_input('Please Enter Your age ? : ')) print 'You age is grater than 10' ~ ~ ~
  14. 14. $ chmod a+x while_statement.py $ ./ while_statement.py Please Enter Your age ? : 23 You age is grater than 10 Please Enter Your age ? : 15 You age is grater than 10 Please Enter Your age ? : 18 You age is grater than 10 Please Enter Your age ? : 8 You age is grater than 10 $
  15. 15. $ vim while_statement.py #!/usr/bin/env python age = 20 while age > 10: age = int(raw_input('Please Enter Your age ? : ')) if age > 10: print 'Your age is grater than 10' elif age < 10: print 'Your age is smaller than 10' else: print 'Your age is equal to 10' ~ ~
  16. 16. $ chmod a+x while_statement.py $ ./ while_statement.py Please Enter Your age ? : 23 You age is grater than 10 Please Enter Your age ? : 15 You age is grater than 10 Please Enter Your age ? : 8 You age is smaller than 10 $ $ ./ while_statement.py Please Enter Your age ? : 17 You age is grater than 10 Please Enter Your age ? : 10 You age is equal to 10 $
  17. 17. For Loops for item in object: do stuff do stuff for item in [1, 2, 3] for item in ['a', 2, '3'] for (x,y) in [('reza', 23), ('mohammad', 25)]
  18. 18. >>> for item in [1, 2, 3]: ... print item ... 1 2 3
  19. 19. item is a name can be anything >>> names = ['Reza', 'Majid', 'Sara', 'Iman'] >>> names ['Reza', 'Majid', 'Sara', 'Iman'] >>> for name in names: ... print name ... Reza Majid Sara Iman >>>
  20. 20. >>> info = [('Reza', 23), ('Sara', 20), ('Shayan', 30), ('AmirAli', 21)] >>> info [('Reza', 23), ('Sara', 20), ('Shayan', 30), ('AmirAli', 21)] >>> for (name, age) in info: ... print 'Your name is : %s and Your age is %d' % (name, age) ... Your name is : Reza and Your age is 23 Your name is : Sara and Your age is 20 Your name is : Shayan and Your age is 30 Your name is : AmirAli and Your age is 21 >>>
  21. 21. Emulating C style for loops C style loops : for(i=0; i<10; i++) for(i=0; i<10; i++){ cout<<i<<','; } 0,1,2,3,4,5,6,7,8,9,
  22. 22. range() range(lower, upper, step) create a list for use range(n) – [0,...,n-1] >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>>
  23. 23. >>> range(1,10) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(1,10,2) [1, 3, 5, 7, 9] >>> >>> for number in range(1,10,2): ... print number ... 1 3 5 7 9 >>>
  24. 24. References • Data model — Python v2.7.5 documentation • http://docs.python.org/2/reference/datamodel.html#objects-values-and-types • PEP 8 -- Style Guide for Python Code • http://www.python.org/dev/peps/pep-0008/ THIS IS A COPYRIGHTED MATERIAL ©2013 Mohammad Reza Kamalifard. ALL RIGHTS RESERVED.

×