Created
April 17, 2012 20:30
-
-
Save alts/2408799 to your computer and use it in GitHub Desktop.
playforge python class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# numbers | |
>>> 1 | |
1 | |
>>> 1 + 1 | |
2 | |
>>> 1 - 5 | |
-4 | |
>>> 8 * 9 | |
72 | |
>>> 4 / 2 | |
2 | |
>>> 5 / 2 | |
2 | |
>>> 2.5 | |
2.5 | |
>>> type(5/2) | |
<type 'int'> | |
>>> type(2.5) | |
<type 'float'> | |
>>> 5.0 / 2 | |
2.5 | |
>>> 6 ** 7 | |
279936 | |
>> 5 % 2 | |
1 | |
>> 5 + 90 * 7 - 4 / 2 | |
633 | |
# boolean | |
>>> True | |
True | |
>>> False | |
False | |
>>> if True: | |
... 7 | |
... | |
7 | |
>>> if False: | |
... 7 | |
... | |
>>> if False: | |
... 7 | |
... else: | |
... 8 | |
... | |
8 | |
>>> if False: | |
... 7 | |
... elif False: | |
... 8 | |
... elif False: | |
... 9 | |
... else: | |
... 10 | |
... | |
10 | |
>>> 7 < 8 | |
True | |
>>> 7 <= 8 | |
True | |
>>> 7 == 8 | |
False | |
>>> 7 != 8 | |
True | |
>>> 7 > 8 | |
False | |
>>> 7 >= 8 | |
False | |
>>> if 7 < 8: | |
... 9 | |
... | |
9 | |
>>> not False | |
True | |
>>> not True | |
False | |
>>> True or False | |
True | |
>>> True and False | |
False | |
# strings | |
>>> "hello" | |
'hello' | |
>>> print "hello" | |
hello | |
>>> print "hello", "jello" | |
hello jello | |
>>> print "hello", 3, None, "rules" | |
hello 3 None rules | |
>>> "hello" == "jello" | |
False | |
>>> "hello" == "hello" | |
True | |
>>> 'h' in "hello" | |
True | |
>>> 'j' in "hello" | |
False | |
>>> "h" in "hello" | |
True | |
>>> "j" in "hello" | |
False | |
>>> "hell" in "hello" | |
True | |
>>> "hello" + "hello" | |
'hellohello' | |
>>> "hello" - "hello" | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: unsupported operand type(s) for -: 'str' and 'str' | |
>>> "hello" + "hello" + "hello" | |
'hellohellohello' | |
>>> "hello" * 3 | |
'hellohellohello' | |
>>> "hello" / 3 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: unsupported operand type(s) for /: 'str' and 'int' | |
# lists | |
>>> [] | |
[] | |
>>> [None, 1, 2, -4, 7.0, True, False, 7 < 8, "hello", "hello" + "there", []] | |
[None, 1, 2, -4, 7.0, True, False, True, 'hello', 'hellothere', []] | |
>>> [1,2,3][0] | |
1 | |
>>> [1,2,3][1] | |
2 | |
>>> [1,2,3][-1] | |
3 | |
>>> 3 in [1,2,3] | |
True | |
>>> 4 in [1,2,3] | |
False | |
>>> [1,2,3] + 4 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: can only concatenate list (not "int") to list | |
>>> [1,2,3] + [4] | |
[1, 2, 3, 4] | |
>>> [1,2,3] * 3 | |
[1, 2, 3, 1, 2, 3, 1, 2, 3] | |
# assignment | |
>>> value = [1,2,3] | |
>>> value | |
[1, 2, 3] | |
>>> eight = 8 | |
>>> eight * 7 | |
56 | |
>>> people_in_room = 8 | |
>>> people_in_room = people_in_room + 1 | |
>>> people_in_room | |
9 | |
>>> tw_devs = ["Jeff", "Niels", "Nick"] | |
>>> for dev in tw_devs: | |
... print dev, "is awesome" | |
... | |
Jeff is awesome | |
Niels is awesome | |
Nick is awesome | |
# functions | |
>>> def make_awesome(name): | |
... return name + " is awesome" | |
... | |
>>> make_awesome("Tami") | |
'Tami is awesome' | |
>>> [make_awesome] | |
[<function make_awesome at 0x10041bcf8>] | |
# wallet | |
>>> wallet = 500 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment