If/elif/else
- Any value can be used as an if-test. The “zero” values all count as false: None, 0, empty string, empty list, empty dictionary.
- The boolean operators are the spelled out words and, or, not (Python does not use the C-style && || !).
Here is the list of available conditional operators.
==
equal to!=
not equal to<
less than>
greater than<=
less than or equal to>=
greater than or equal to
There are few logical operators to combine boolean values.
a and b
isTrue
only if botha
andb
are True.a or b
is True if eithera
orb
is True.not a
is True only ifa
is False.
return = scan_target('10.5.0.6')
if return == 0:
print "[+] Scan sucessful!!"
print packet
elif return ==1:
print "[-] Scan unsucessful!!"
print "[*] Inspect with wireshark"
else:
print "[!] Unknown error"
if speed >= 80:
print 'License and registration please'
if mood == 'terrible' or speed >= 100:
print 'You have the right to remain silent.'
elif mood == 'bad' or speed >= 90:
print "I'm going to have to write you a ticket."
write_ticket()
else:
print "Let's try to keep it under 80 ok?
While loop
Python also has the standard while-loop, and the break and continue statements work as in C++ and Java, altering the course of the innermost loop. The above for/in loops solves the common case of iterating over every element in a list, but the while loop gives you total control over the index numbers. Here’s a while loop which accesses every 3rd element in a list:
## Access every 3rd element in a list
i = 0
while i < len(a):
print a[i]
i = i + 3
For statement
Python provides for statement to iterate over a list. A for statement executes the specified block of code for every element in a list.
FOR - IN construct
- Python’s for and in constructs are extremely useful.
- The for construct –
for var in list
– is an easy way to look at each element in a list (or other collection). Do not add or remove from the list during iteration.
squares = [1, 4, 9, 16]
sum = 0
for num in squares:
sum += num
print sum ## 30
- The in construct on its own is an easy way to test if an element appears in a list (or other collection) –
value in collection
– tests if the value is in the collection, returning True/False.
list = ['larry', 'curly', 'moe']
if 'curly' in list:
print 'yay'
Range & Xrange
- Python for loop iterates over a range of numbers, list of elements.
- The range(n) function yields the numbers 0, 1, … n-1, and range(a, b) returns a, a+1, … b-1
>>> from socket import *
>>> open_ports = [21, 23, 25, 80, 139, 8080]
>>>
>>> for a in xrange(len(open_ports)):
... print "[+] Port num: {} {} is open.".format(open_ports[a], getservbyport(open_ports[a]))
...
[+] Port num: 21 ftp is open.
[+] Port num: 23 telnet is open.
[+] Port num: 25 smtp is open.
[+] Port num: 80 http is open.
[+] Port num: 139 netbios-ssn is open.
[+] Port num: 8080 http-alt is open.
>>>
- There is a variant xrange() which avoids the cost of building the whole list for performance sensitive cases (in Python 3000, range() will have the good performance behavior and you can forget about xrange().