help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
%guiref -> A brief reference about the graphical user interface.
In [1]: runfile('/Users/progprim/Desktop/lecture06.py', wdir='/Users/progprim/Desktop')
The area is 200
In [2]: !ls -l
total 3256
drwxr-xr-x 7 progprim staff 238 21 Sep 15:12 Desktop
-rw-r--r-- 1 progprim staff 215268 22 Sep 09:33 PanoptoRecorderCrashReport_1-093333_bin.hang
-r--------@ 1 progprim staff 1312207 19 Sep 12:13 Python-for-Computational-Science-and-Engineering-slides.pdf
drwxr-xr-x 71 progprim staff 2414 21 Sep 10:53 code
-rw-r--r-- 1 progprim staff 444 20 Sep 11:57 funcexample1.py
-rw-r--r-- 1 progprim staff 80669 20 Sep 15:45 ipython03.html
-rw-r--r-- 1 progprim staff 217 21 Sep 09:23 lecture03.py
-rw-r--r-- 1 progprim staff 211 21 Sep 09:29 lecture04sum.py
-rw-r--r-- 1 progprim staff 467 21 Sep 14:40 lecture05-exceptions.py
-rw-r--r-- 1 progprim staff 609 21 Sep 15:16 lecture05-file-reading.py
-rw-r--r-- 1 progprim staff 513 21 Sep 13:58 lecture05.py
-rw-r--r-- 1 progprim staff 392 22 Sep 09:38 lecture06.py
-rw-r--r-- 1 progprim staff 16 21 Sep 14:41 linedata.txt
-rw-r--r-- 1 progprim staff 164 22 Sep 09:24 namespaces1.py
-rw-r--r-- 1 progprim staff 136 22 Sep 09:25 namespaces2.py
-rw-r--r-- 1 progprim staff 225 22 Sep 09:26 namespaces3.py
-rw-r--r-- 1 progprim staff 498 20 Sep 15:39 rangedouble.py
-rw-r--r-- 1 progprim staff 112 20 Sep 10:11 spyder-example1.py
In [3]: import lecture06
In [4]: dir(lecture06)
Out[4]:
['__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'area',
'f']
In [5]: lecture06.area(10, 20)
Out[5]: 200
In [6]: runfile('/Users/progprim/Desktop/lecture06-main.py', wdir='/Users/progprim/Desktop')
Reloaded modules: lecture06
The area is 200
In [7]: runfile('/Users/progprim/Desktop/lecture06-main.py', wdir='/Users/progprim/Desktop')
Reloaded modules: lecture06
The area is 200
__name__ is __main__
In [8]: runfile('/Users/progprim/Desktop/lecture06.py', wdir='/Users/progprim/Desktop')
Reloaded modules: lecture06
The area is 200
__name__ is __main__
In [9]: runfile('/Users/progprim/Desktop/lecture06-main.py', wdir='/Users/progprim/Desktop')
__name__ is lecture06
The area is 200
In [10]: runfile('/Users/progprim/Desktop/lecture06-main.py', wdir='/Users/progprim/Desktop')
Reloaded modules: lecture06
__name__ is lecture06
after import
The area is 200
In [11]: runfile('/Users/progprim/Desktop/lecture06-main.py', wdir='/Users/progprim/Desktop')
Reloaded modules: lecture06
__name__ is lecture06
after import
The area is 200
In [12]: assert 10 == 10
In [13]: assert 10 == 11
Traceback (most recent call last):
File "<ipython-input-13-16c7f02665d7>", line 1, in <module>
assert 10 == 11
AssertionError
In [14]: assert True
In [15]: assert False
Traceback (most recent call last):
File "<ipython-input-15-6e6df518a476>", line 1, in <module>
assert False
AssertionError
In [16]: runfile('/Users/progprim/Desktop/lecture06.py', wdir='/Users/progprim/Desktop')
Reloaded modules: lecture06
Running tests for area function
In [17]: import lecture06
In [18]: lecture06.test_area()
Running tests for area function
In [19]: lecture06.area
Out[19]: <function lecture06.area>
In [20]: area
Out[20]: <function __main__.area>
In [21]: id(area)
Out[21]: 4723046736
In [22]: id(lecture06.area)
Out[22]: 4723047416
In [23]: import lecture06
In [24]: import imp
In [25]: imp.reload(lecture06)
Marker 2
Out[25]: <module 'lecture06' from '/Users/progprim/Desktop/lecture06.py'>
In [26]: dir(lecture06)
Out[26]:
['__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'_internalf',
'area',
'f',
'test_area']
In [27]: runfile('/Users/progprim/Desktop/lecture06-main.py', wdir='/Users/progprim/Desktop')
Reloaded modules: lecture06
Marker 2
after import
The area is 200
In [28]: runfile('/Users/progprim/Desktop/defaultvalues.py', wdir='/Users/progprim/Desktop')
Reloaded modules: lecture06
the area is 2.5
the area is 9
the area is 2
the area is 3.75
In [29]: runfile('/Users/progprim/Desktop/defaultvalues.py', wdir='/Users/progprim/Desktop')
the area is 2.5
the area is 9
the area is 2
the area is 3.75
In [30]: runfile('/Users/progprim/Desktop/defaultvalues.py', wdir='/Users/progprim/Desktop')
the area is 2.5
the area is 9
the area is 2
the area is 3.75
In [31]: f(1, 2, 3)
a = 1, b = 2, c = 3
In [32]: f(c=3, a=1, b=2)
a = 1, b = 2, c = 3
In [33]: t = (1, 2, 3)
In [34]: f(*t)
a = 1, b = 2, c = 3
In [35]: f(t)
Traceback (most recent call last):
File "<ipython-input-35-e1e259ef2d0b>", line 1, in <module>
f(t)
TypeError: f() missing 2 required positional arguments: 'b' and 'c'
In [36]: f(c=3, a=1, b=2)
a = 1, b = 2, c = 3
In [37]: runfile('/Users/progprim/Desktop/namespaces1.py', wdir='/Users/progprim/Desktop')
I am local
I am global
In [38]: runfile('/Users/progprim/Desktop/namespaces2.py', wdir='/Users/progprim/Desktop')
I am global
In [39]: runfile('/Users/progprim/Desktop/namespaces2.py', wdir='/Users/progprim/Desktop')
Traceback (most recent call last):
File "<ipython-input-39-3b2b46b0a10e>", line 1, in <module>
runfile('/Users/progprim/Desktop/namespaces2.py', wdir='/Users/progprim/Desktop')
File "/Users/progprim/anaconda/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 714, in runfile
execfile(filename, namespace)
File "/Users/progprim/anaconda/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 89, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/Users/progprim/Desktop/namespaces2.py", line 17, in <module>
f()
File "/Users/progprim/Desktop/namespaces2.py", line 10, in f
print(x)
UnboundLocalError: local variable 'x' referenced before assignment
In [40]: runfile('/Users/progprim/Desktop/namespaces2.py', wdir='/Users/progprim/Desktop')
I am global
I am global
In [41]: runfile('/Users/progprim/Desktop/namespaces2.py', wdir='/Users/progprim/Desktop')
I am global
I am local
In [42]: %reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
In [43]: import namespaces2 as n
I am global
In [44]: n.f()
I am global
In [45]: x = "x in main program"
In [46]: n.f()
I am global
In [47]: from namespaces2 import f
In [48]: f
Out[48]: <function namespaces2.f>
In [49]: id(f)
Out[49]: 4723226824
In [50]: f()
I am global
In [51]: from namespaces1 import f
I am local
I am global
In [52]: f()
I am local
In [53]: id(f)
Out[53]: 4723225192
In [54]: %reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
In [55]: from namespaces2 import f
In [56]: f()
I am global
In [57]: runfile('/Users/progprim/Desktop/namespaces3.py', wdir='/Users/progprim/Desktop')
Reloaded modules: namespaces1, namespaces2
I am global x
I am local y
back in main:
I am global y
In [58]: #[2*i for i in range(10)]
In [59]: range(10)
Out[59]: range(0, 10)
In [60]: list(range(10))
Out[60]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [60]:
In [61]: tuple(range(10))
Out[61]: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
In [62]: [2*i for i in range(10)]
Out[62]: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
In [63]: [i for i in range(10)]
Out[63]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [64]: [i*0.5 for i in range(10)]
Out[64]: [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]
In [65]: [i*0.5 for i in range(6)]
Out[65]: [0.0, 0.5, 1.0, 1.5, 2.0, 2.5]
In [66]: def f(x):
...: return x**2
...:
In [67]: [f(i) for i in range(6)]
Out[67]: [0, 1, 4, 9, 16, 25]
In [68]: [i for i in ["cat", "dog", "ant"])
File "<ipython-input-68-22904ad882bb>", line 1
[i for i in ["cat", "dog", "ant"])
^
SyntaxError: invalid syntax
In [69]: [i for i in ["cat", "dog", "ant"]]
Out[69]: ['cat', 'dog', 'ant']
In [70]: [i.upper() for i in ["cat", "dog", "ant"]]
Out[70]: ['CAT', 'DOG', 'ANT']
In [71]: [2 * i for i in ["cat", "dog", "ant"]]
Out[71]: ['catcat', 'dogdog', 'antant']
In [72]: [2 * i, i.upper() for i in ["cat", "dog", "ant"]]
File "<ipython-input-72-90cadd8bbac3>", line 1
[2 * i, i.upper() for i in ["cat", "dog", "ant"]]
^
SyntaxError: invalid syntax
In [73]: [(2 * i, i.upper()) for i in ["cat", "dog", "ant"]]
Out[73]: [('catcat', 'CAT'), ('dogdog', 'DOG'), ('antant', 'ANT')]
In [74]: [f(i) for i in range(20)]
Out[74]:
[0,
1,
4,
9,
16,
25,
36,
49,
64,
81,
100,
121,
144,
169,
196,
225,
256,
289,
324,
361]
In [75]: [f(i) for i in range(10)]
Out[75]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
In [76]: [i for i in range(10)]
Out[76]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [77]: [i for i in range(10) if i > 5]
Out[77]: [6, 7, 8, 9]
In [78]: [i.upper() for i in ["cat", "dog", "ant", "horse"]]
Out[78]: ['CAT', 'DOG', 'ANT', 'HORSE']
In [79]: [i.upper() for i in ["cat", "dog", "ant", "horse"] if len(i) < 4]
Out[79]: ['CAT', 'DOG', 'ANT']
In [80]: [i.upper() for i in ["cat", "dog", "ant", "horse"] if len(i) < 4]
Out[80]: ['CAT', 'DOG', 'ANT']
In [81]: d = {}
In [82]: type(d)
Out[82]: dict
In [83]: d['today'] = 10
In [84]: d['tomorrow'] = 18
In [85]: d
Out[85]: {'today': 10, 'tomorrow': 18}
In [86]: str(d)
Out[86]: "{'tomorrow': 18, 'today': 10}"
In [87]: repr(d)
Out[87]: "{'tomorrow': 18, 'today': 10}"
In [88]: l = [10, 18]
In [89]: l[0]
Out[89]: 10
In [90]: l[1]
Out[90]: 18
In [91]: d
Out[91]: {'today': 10, 'tomorrow': 18}
In [92]: d['today']
Out[92]: 10
In [93]: d['tomorrow']
Out[93]: 18
In [94]: d['today'] = 14
In [95]: d
Out[95]: {'today': 14, 'tomorrow': 18}
In [96]: d['yesterday']
Traceback (most recent call last):
File "<ipython-input-96-46c9a5ecc514>", line 1, in <module>
d['yesterday']
KeyError: 'yesterday'
In [97]: d.keys()
Out[97]: dict_keys(['tomorrow', 'today'])
In [98]: list(d.keys())
Out[98]: ['tomorrow', 'today']
In [99]: d.values()
Out[99]: dict_values([18, 14])
In [100]: d.items()
Out[100]: dict_items([('tomorrow', 18), ('today', 14)])
In [101]: for key in d.keys():
...: print("{} -> {}".format(key, d[key]))
...:
...:
...:
tomorrow -> 18
today -> 14
In [102]: for day in d.keys():
...: print("{} -> {}".format(day, d[day]))
...:
tomorrow -> 18
today -> 14
In [103]: for day in d.keys():
...: print("the temperature {} is {}C".format(day, d[day]))
...:
the temperature tomorrow is 18C
the temperature today is 14C
In [104]: for day in d:
...: print("the temperature {} is {}C".format(day, d[day]))
...:
the temperature tomorrow is 18C
the temperature today is 14C
In [105]: for day in d:
...: print("the temperature {} is {}C".format(day, d[day]))
...:
the temperature tomorrow is 18C
the temperature today is 14C
In [105]:
In [106]: d
Out[106]: {'today': 14, 'tomorrow': 18}
In [107]: e = {}
In [108]: e['key1'] = (10, "cat", range(10))
In [109]: e
Out[109]: {'key1': (10, 'cat', range(0, 10))}
In [110]: e['key']
Traceback (most recent call last):
File "<ipython-input-110-fe4c2bfb9ec7>", line 1, in <module>
e['key']
KeyError: 'key'
In [111]: e['key1']
Out[111]: (10, 'cat', range(0, 10))
In [112]: e[(10, 20, 30)] = "some other value"
In [113]: e
Out[113]: {(10, 20, 30): 'some other value', 'key1': (10, 'cat', range(0, 10))}
In [114]: