Python 2
Python 2
RELATED BOOK
Python For Kids For Dummies
Add to Cart
By Brendan Scott
Programming is an important skill. Python will serve you well for years to come. The tables here give you the core words,
built-ins, standard library functions, and operators that you’ll use most when you’re coding with Python.
Page 1 of 12
Keywor Summary Example
d
and Logical operator to test whether two things are <conditional expression> and
both True. <conditional expression>
x>2 and x<10
as Assign a file object to a variable. Used with with. with open(<name of file>,<file mode>)
Let your code refer to a module under a different name as <object name>:
(also called an alias). Used with import. import cPickle as pickle
continue Skip balance of loop and begin a new iteration. for i in range(10):
if i%2 ==0:
continue
Page 2 of 12
elif Add conditional test to an if clause. See if.
for Create a loop which iterates through elements of a list for <dummy variable name> in
(or other iterable). <sequence>:
for i in range(10):
from Import specific functions from a module without from <module name> import <name of
importing the whole module. function or object>
from random import randint
Page 3 of 12
print(“x is not greater than 3, nor is
it 1 one or 2”)
import Use code defined in another file without retyping it. import <name of module>
import random
lambda Shorthand function definition. Usually used where a lamda <dummy variables>:
function needs to be passed as an argument to another <expression using dummy variables>
function. times = lambda x, y: x*y
command=lambda x:
self.draw_line(self.control_points)
pass Placeholder keyword. Does nothing but stop Python for i in range (10):
Page 4 of 12
complaining that a code block is empty. pass
return Return from the execution of a function. If a value is return <value or expression>
specified, return that value, otherwise return None. return x+2
while Execute a code block while the associated condition while <conditional expression>:
is True. while True:
pass
with Get Python to manage a resource (like a file) for you. with open(<name of file>,<file mode>)
as <object name>:
Python Built-ins
getattr Get an attribute of an object by a name. Useful for getattr(<name of object>, <name of
introspection. attribute>)
Page 6 of 12
help(getattr)
object A base on which other classes can inherit from. class CustomObject(object):
open Open a file on disk, return a file object. open(<path to file>, <mode>)
open(‘mydatafile.txt’, ‘r’) # read
(opens a file to read data from)
open(‘mydatafile.txt’, ‘w’) # write
(creates a new file to write to, destroys
any existing file with the same name)
open(‘mydatafile.txt’, ‘a’) # append
(adds to an existing file if any, or
creates
a new one if none existing already)
print Reimplementation of print keyword, but as a function. from future import print_function
Need to import from the future to use it (srsly!) print (‘Hello World!‘)
Page 7 of 12
range Gives numbers between the lower and upper limits range(10)
specified (including the lower, but excluding the upper range(5,10)
limit). A step may be specified. range(1,10,2)
raw_input Get some text as a string from the user, with an optional prompt = ‘What is your guess? ‘
prompt. players_guess = raw_input(prompt)
Use the work that others have already done. Try these modules from the Python standard library.
pickle, Save and load objects pickle.load(<file object to load from>), pickle.dump(<object to
cPickle to/from a file. dump>, <file object to save to>)
Page 8 of 12
random Various functions random.choice(<sequence to choose from>), random.randint(<lower
relating to random limit>, <upper limit>), random.shuffle(<name of list to shuffle>)
numbers.
Python Operators
Page 10 of 12
‘aa‘
% Remainder Give the remainder when dividing the left number by >>> 10%3
(Modulo) the right number. 1
Formatting operator for strings.
= Assignment Assign the value on the right to the variable on the >>> a = 1
left.
== Equality Is the left side equal to the right side? Is True if so; >>> 1 == 1
is False otherwise. True
>>> ‘a’ == ‘a’
True
!= Not equal Is the left side not equal to the right side? Is True if >>> 1 != 1
so; is False otherwise. False
>>> 1 != 2
True
>>> ‘a’ != ‘a’
Page 11 of 12
True
> Greater than Is the left side greater than the right side? >>> 2 > 1
>= means greater than or equal to True
< Less than Is the left side less than the right side? >>> 1 < 2
<= means less than or equal to True
& (or And Are both left and right True? >>> True & True
and) Typically used for complex conditions where you True
want to do something if everything is True: >>> True and False
while im_hungry and you_have_food: False
>>> True & (1 == 2)
False
Page 12 of 12