if a:
print "Hello"
elif b:
print "Goodbye"
else:
print "I have nothing to say"
for char in 'aeiou':
print(char)
while i < 4:
i += 1
print i
def normalize(value, factor=1.0):
"""Normalize positive values,
truncating negative ones."""
if value < 0.0:
return 0.0
return value * factor
| int | x = 1; x = int('1') |
| float | x = 1.0; x = float(1) |
| complex | x = 1 + 2j |
| string | x = "s" |
| boolean | x = True; y = False |
| addition | 1 + 2.0 | 3.0 |
| subtraction | 2 - 3 | -1 |
| multiplication | 2 * 3 | 6 |
| division | 2 / 3 | 1 |
| modulo | 3 % 2 | 1 |
| initialize | L = [2, 1, 3, 4, 8, 9] | |
| append | L.append(5) | |
| sort | L.sort() | |
| remove | L.remove(5) |
| initialize | s = "acc ttg cta tcg" | |
| split | s.split() | ['acc', 'ttg', 'cta', 'tcg'] |
| join | " ".join(["a", "b"]) | "a b" |
| find | s.find('ttg') | 4 |
| count | s.count('t') | 4 |
| strip | ' a '.strip() | 'a' |
| open | f = open(filename, 'r') | or 'w' to write |
| write | f.write("Hello world") | |
| read all lines | f.readlines() | |
| read the next line | l = f.readline() |
| index | 'abcd'[1] | 'b' |
| negative index | 'abcd'[-1]'d' | |
| length | len('abcd') | 4 |
| slice | 'abcd'[1:3] | 'bc' |
| "Hello %s" % 'name!' | "Hello name!" |
| "%d, %0.2f" % (2, 0.3456) | "2 0.35" |
| from foo import bar |
| import foo.bar |