Last login: Tue Apr 5 08:55:46 on ttys000
dhcp-10-105-149-162:~ kbicknell$ python
Python 2.7.11 (default, Mar 1 2016, 18:40:10)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 + 2
3
>>> 3 * 2
6
>>> 4 / 2
2
>>> 4 - 10
-6
>>> 4 ** 2
16
>>> 5 % 2
1
>>> 3 + 3 * 4
15
>>> (3 + 3) * 4
24
>>> 3 * / 2
File "", line 1
3 * / 2
^
SyntaxError: invalid syntax
>>> x = 3
>>> x * 3
9
>>> x = "blah"
>>> x
'blah'
>>> x = True
>>> x
True
>>> x = False
>>> x
False
>>> x = "False"
>>> x = 3
>>> x = "3"
>>> print x
3
>>> type(3)
>>> type(3.5)
>>> type('blah')
>>> type(False)
>>> str(3)
'3'
>>> int('3')
3
>>> int('3.5')
Traceback (most recent call last):
File "", line 1, in
ValueError: invalid literal for int() with base 10: '3.5'
>>> type(3 + 5)
>>> type(3 + 3.5)
>>> 'abc' + 'def'
'abcdef'
>>> 'abc' * 3
'abcabcabc'
>>> 'abc' * 3.5
Traceback (most recent call last):
File "", line 1, in
TypeError: can't multiply sequence by non-int of type 'float'
>>> 3 * 3.5
10.5
>>> 3 / 2
1
>>> type(3 / 2)
>>> type(3 / 2.0)
>>> 3 / 2.0
1.5
>>> from __future__ import division
>>> 3 / 2
1.5
>>> 'abc' + 4
Traceback (most recent call last):
File "", line 1, in
TypeError: cannot concatenate 'str' and 'int' objects
>>> x = 4
>>> x = x + 10
>>> x
14
>>> x += 10
>>> x
24
>>> x < 100
True
>>> x > 100
False
>>> x < 24
False
>>> x <= 24
True
>>> x == 24
True
>>> x == 25
False
>>> x = 24
>>> x == 24 and x < 100
True
>>> x == 24 or x < 100
True
>>> not (x == 24 or x < 100)
False
>>> if x == 24:
... print "This is 24"
... print "it's so great"
...
This is 24
it's so great
>>> if x == 24 and x < 100:
... print "both true"
...
both true
>>> x = [20, 30, 50, 80]
>>> x
[20, 30, 50, 80]
>>> x = [20, 'abc', True, 80.5]
>>> x
[20, 'abc', True, 80.5]
>>> len(x)
4
>>> type(x)
>>> len('abc')
3
>>> len(3.5)
Traceback (most recent call last):
File "", line 1, in
TypeError: object of type 'float' has no len()
>>> x
[20, 'abc', True, 80.5]
>>> x[0]
20
>>> x[3]
80.5
>>> x[4]
Traceback (most recent call last):
File "", line 1, in
IndexError: list index out of range
>>> x
[20, 'abc', True, 80.5]
>>> x[2] = False
>>> x
[20, 'abc', False, 80.5]
>>> x[1:3]
['abc', False]
>>> x[1:]
['abc', False, 80.5]
>>> x[:2]
[20, 'abc']
>>> a[1]
Traceback (most recent call last):
File "", line 1, in
NameError: name 'a' is not defined
>>> x[1]
'abc'
>>> x[1][2]
'c'
>>> x
[20, 'abc', False, 80.5]
>>> x + [3]
[20, 'abc', False, 80.5, 3]
>>> x + 3
Traceback (most recent call last):
File "", line 1, in
TypeError: can only concatenate list (not "int") to list
>>> x[2] = [3]
>>> x
[20, 'abc', [3], 80.5]
>>> if x == 24:
... print 'a'
... print 'b'
...
>>> fruits = ['apple', 'pear', 'banana']
>>> fruits
['apple', 'pear', 'banana']
>>> print "I really like " + fruits[0]
I really like apple
>>> print "I really like " + fruits[0] + 's'
I really like apples
>>> print "I really like " + fruits[1] + 's'
I really like pears
>>> print "I really like " + fruits[2] + 's'
I really like bananas
>>> print "I really like " + 's' + fruits[2]
I really like sbanana
>>> for fruit in fruits:
... print "I really like " + fruit + 's'
...
I really like apples
I really like pears
I really like bananas
>>> for fruit in fruits:
... if fruit == 'pear':
... print "I found it"
...
I found it
>>> for fruit in fruits:
...
dhcp-10-105-149-162:~ kbicknell$ python
Python 2.7.11 (default, Mar 1 2016, 18:40:10)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> fruits = ["pear", "apple", "pear", "peach"]
>>> for fruit in fruits:
... if fruit == 'pear':
... print "i found pear"
...
i found pear
i found pear
>>> for fruit in fruits:
... if fruit == 'pear':
... print "i found pear"
... break
...
i found pear
>>> for fruit in fruits:
... if fruit == 'apple':
... continue
... print "I really like " + fruit + 's'
...
I really like pears
I really like pears
I really like peachs
>>> 'pear' in fruits
True
>>> 'banana' not in fruits
True
>>> for fruit in fruits:
... fruit = 'banana'
...
>>> fruits
['pear', 'apple', 'pear', 'peach']
>>> for i, fruit in enumerate(fruits):
...
dhcp-10-105-149-162:~ kbicknell$ python
Python 2.7.11 (default, Mar 1 2016, 18:40:10)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> fruits = ['apple', 'pear', 'banana']
>>> for i, fruit in enumerate(fruits):
... if fruit == 'apple':
... fruits[i] = 'banana'
...
>>> fruits
['banana', 'pear', 'banana']
>>> (1, 3)
(1, 3)
>>> x = (1, 3)
>>> x[0]
1
>>> x[0] = 3
Traceback (most recent call last):
File "", line 1, in
TypeError: 'tuple' object does not support item assignment
>>> fruits = ['banana', 'pears', 'banana']
>>> fruits
['banana', 'pears', 'banana']
>>> for i, fruit in enumerate(fruits):
... fruit_length = len(fruit)
... print fruit[fruit_length - 1]
...
a
s
a
>>> for i, fruit in enumerate(fruits):
... fruit_length = len(fruit)
... if fruit[fruit_length - 1] == 's':
... fruits[i] = fruit[:(fruit_length - 1)]
...
>>> fruits
['banana', 'pear', 'banana']
>>>
dhcp-10-105-149-162:~ kbicknell$