Demo¶
UW Geospatial Data Analysis
CEE498/CEWA599
David Shean
import os
os.path.split?
Signature: os.path.split(p)
Docstring:
Split a pathname. Returns tuple "(head, tail)" where "tail" is
everything after the final slash. Either part may be empty.
File: /srv/conda/envs/notebook/lib/python3.8/posixpath.py
Type: function
p = /srv/conda/envs/notebook/lib/python3.8/posixpath.py
File "<ipython-input-3-17c087e07115>", line 1
p = /srv/conda/envs/notebook/lib/python3.8/posixpath.py
^
SyntaxError: invalid syntax
p = "/srv/conda/envs/notebook/lib/python3.8/posixpath.py"
p
'/srv/conda/envs/notebook/lib/python3.8/posixpath.py'
mytuple = os.path.split(p)
mytuple
('/srv/conda/envs/notebook/lib/python3.8', 'posixpath.py')
mytuple[0] = 'something different'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-48bae65e1a1d> in <module>
----> 1 mytuple[0] = 'something different'
TypeError: 'tuple' object does not support item assignment
a = 0
a
0
a = 'cool string'
a
'cool string'
print(a)
cool string
type(a)
str
a.capitalize()
'Cool string'
a = range(10)
a
range(0, 10)
a.start
0
t = tuple(a)
t
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
l = list(a)
l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
len(l)
10
len(t)
10
0 in l
True
True?
Type: bool
String form: True
Namespace: Python builtin
Docstring:
bool(x) -> bool
Returns True when the argument x is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.
i = 'asdf' in l
i
False
if 5 in l:
print("Hooray")
Hooray
l[2]
2
l[-2]
8
l[-5:-1]
[5, 6, 7, 8]
l[::2]
[0, 2, 4, 6, 8]
l[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
l.reverse()
l
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
l[0] = 0
l
[0, 8, 7, 6, 5, 4, 3, 2, 1, 0]
for i in l:
print(i)
0
8
7
6
5
4
3
2
1
0
1 + 2
3
'asdf'+'jkl;'
'asdfjkl;'
'asdf'+1
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-37-5d8bb59cdbd3> in <module>
----> 1 'asdf'+1
TypeError: can only concatenate str (not "int") to str
out = [i/2 for i in l]
out
[0.0, 4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0]
Find all numbers that contain a given integer¶
l = list(range(100))
25 in l
True
result = []
for i in l:
if i % 7 == 0:
result.append(i)
result
[0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]
result = []
for i in l:
if i % 5 == 0:
result.append(i)
result
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
result = []
for i in l:
if i % 10 == 0:
result.append(i)
result
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
Create a function¶
def find_multiples_list(inlist, n):
result = []
for i in inlist:
if i % n == 0:
#if str(n) in str(i):
result.append(i)
return result
out = find_multiples_list(l, 5)
out
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
find_multiples_list(l, 7)
[0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]