print |
print "Something" . print "This", 25 the , allows us to join a string and another string, number or variable together. , also allows us to join things on the same line (space separated): print "this",; print "that" . |
""" |
Can be used as either a code block (wrap with three """ or to print a block of text print """ ) |
== != |
Equal to, Not equal to |
< > <= >= |
Less-than, Greater-than, less-than-equal, greater-than-equal |
+= -= |
Original variable with + or - the value to the right of the symbol. E.g x = 1 so x += 1 is now equal to 2 |
; |
The semi-colon allows us to write commands on one line, e.g variableOne = 1; variable2 = 2 |
%d %s %r %f |
String formats. Integer, String, Raw, float (integer with decimal point) |
% |
Set the string formats. print "%d" % 20 or number = 1; text = "string"; print "%d %s" % (number, text) |
true or false |
Boolean operators |
\ \n \t |
\ escapes a character, \n is newline, \t tab. There are quite a few more but find them when needed |
int() float() |
For converting into integers or floated numbers (0 or 0.0). e.g. int(raw_input()) |
import x |
Generic import. i.e. import math => math.sqrt() |
from x import y |
Import module from a library. from sys import argv |
from x import * |
Imports all functions from module, but without the need for module prefix. i.e. from math import sqrt => sqrt() |
argv |
Holds the argument variables. i.e. script, first, second, third = argv (doing this unpacks argv and assigns these variables to it) |
open() |
Open a file. open("file.txt",'w') (there are various flags you can use) |
read() |
Read the file open("file.txt").read() parameters are r , w , a as well as r+ , w+ , a+ . |
close() |
Close the file. You should always close the file, but if you use a variable such as read = open('file.txt').read() Python should automatically close the file once that variable has been used. When you're separating the open() and read() functions, you'd need to close the file after |
seek() |
"Seek" through a file, so you can perform actions on it. Default returns beginning of the file |
readline() |
Reads the current line, and sets next readline to read the next newline. |
truncate() |
Delete file contents (not required if open(file,'w') parameter is used) |
write() |
Write to the file |
return |
Return something within the function for later use |
*args |
Pass all arguments to the function (note the * ) |