jl@jerluc-mws ~ $ ipython
Python 2.7.7rc1 (2.7:ece24bcd1a6f+, Jun 16 2014, 19:34:26)
Type "copyright", "credits" or "license" for more information.
IPython 2.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
In [1]: i have no idea what i'm doing!
File "<ipython-input-1-5d2af8872bcc>", line 1
i have no idea what i'm doing!
^
SyntaxError: invalid syntax
The REPL is a safe place.
In [2]: 1 / 0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-2-b710d87c980c> in <module>()
----> 1 1 / 0
ZeroDivisionError: integer division or modulo by zero
Plus it's right, you know—all the time.
In [1]: 1 + 1
Out[1]: 2
In [2]: 0 - 1
Out[2]: -1
In [3]: 7 * 4
Out[3]: 28
In [4]: 81 / 9
Out[4]: 9
In [5]: 10 ** 2
Out[5]: 100
In [6]: (134 ** 6) * (25 % 5)
Out[6]: 0
In [1]: a = 1
In [2]: b = 12 / 6
In [3]: c = a + b
In [4]: a
Out[4]: 1
In [5]: b
Out[5]: 2
In [6]: c
Out[6]: 3
variable_name = value_or_expression
In [1]: hours_of_sleep = 8
In [2]: hours_of_sleep > 7
Out[2]: True
In [3]: hours_of_sleep < 7
Out[3]: False
In [4]: hours_of_sleep >= 7
Out[4]: True
In [5]: hours_of_sleep <= 7
Out[5]: False
In [6]: hours_of_sleep != 7
Out[6]: True
In [7]: hours_of_sleep == 7
Out[7]: False
In [1]: hours_of_sleep = 8
In [2]: if hours_of_sleep > 7:
...: print('Happy as can be')
...:
Happy as can be
if some_condition:
then_do_this_stuff
In [1]: hours_of_sleep = 6
In [2]: if hours_of_sleep > 7:
...: print('Happy as can be')
...: else:
...: print('Coffee is imminent')
...:
Coffee is imminent
if some_condition:
then_do_this_stuff
else:
do_this_stuff_instead
In [1]: days_of_week = ['S', 'M', 'T', 'W', 'Th', 'F', 'Sa']
In [2]: len(days_of_week)
Out[2]: 7
In [3]: days_of_week[0]
Out[3]: 'S'
In [4]: days_of_week[-1]
Out[4]: 'Sa'
In [5]: days_of_week + ['F']
Out[5]: ['S', 'M', 'T', 'W', 'Th', 'F', 'Sa', 'F']
my_list = [item1, item2, ..., item_n]
fourth_thing = my_list[3]
In [1]: days_of_week = ['S', 'M', 'T', 'W', 'Th', 'F', 'Sa']
In [2]: for day in days_of_week:
...: print(day)
...:
S
M
T
W
Th
F
Sa
for thing in some_list_of_things:
do_this_stuff
In [1]: days_of_week = ['S', 'M', 'T', 'W', 'Th', 'F', 'Sa']
In [2]: for day in days_of_week:
...: if day == 'M':
...: print('Why oh why?!')
...: else:
...: print('TGINM!')
...:
TGINM!
Why oh why?!
TGINM!
TGINM!
TGINM!
TGINM!
TGINM!
In [1]: def enough_sleep(hours_of_sleep):
...: if hours_of_sleep > 7:
...: print('Happy as can be')
...: else:
...: print('Coffee is imminent')
...:
In [2]: enough_sleep(8)
Happy as can be
In [3]: enough_sleep(4)
Coffee is imminent
def function_name(arg1, arg2, ..., arg_n):
do_this_stuff_with_args
In [1]: def avg(a, b):
...: sum = a + b
...: average = sum / 2.0
...: return average
...:
In [2]: avg(1, 2)
Out[2]: 1.5
In [3]: avg(1, 2) + avg(0, 1)
Out[3]: 2.0
In [4]: sum_of_averages = avg(1, 2) + avg(0, 1)
In [5]: sum_of_averages
Out[5]: 2.0
def function_name(arg1, arg2, ..., arg_n):
do_some_stuff
return this_value
my_hours_of_sleep = 10
def enough_sleep(hours_of_sleep):
if hours_of_sleep > 7:
return True
else:
return False
if enough_sleep(my_hours_of_sleep):
print('Taking care of business')
else:
1 / 0
jl@jerluc-mws ~ $ python sleepy.py
Taking care of business
For example, the output for 1-5 would yield:(1 * 2) + (3 * 2) + (5 * 2) = 18
Python has a special function for creating lists of numbers in a range:
In [1]: range(10)
Out[1]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
For checking if a number is odd or not, you can use the modulo operator:
In [1]: 10 % 2
Out[1]: 0
In [2]: 11 % 2
Out[2]: 1