Python Variables and Data Structures: Answers For Energy
Python Variables and Data Structures: Answers For Energy
Numbers Collections
Note: You can create your own classes of objects as well, or extend intrinsic types
Math + - * / % **
Assignment = += -= *= /= %= **=
Identity is is not
a = 5
b = 7
c = a * b # c = 35
d = float(a) * b # d = 35.0
e = float(a * b) # e = 35.0
>>> a = 5
>>> type(a)
<type 'int'>
>>> c = 2 + 5j
>>> type(c)
<type 'complex'>
a = 5 + 7j # A complex number
r = a.real # Get the real part
i = a.imag # Get the imaginary part
Concatenation ("addition"):
Replication ("multiplication"):
u = '-' * 10 # u = '----------'
x = s[1] # x = 'b'
y = s[2:5] # y = 'cde'
Examples
Refer: string_methods.py
Page 2-18 Siemens Power Academy TD-NA PSSE - Python Course
Adding Comments
x = [3, 2, 3, 4]
x.append(7) # x = [3, 2, 3, 4, 7]
x.insert(0, 8) # x = [8, 3, 2, 3, 4, 7]
x.extend([5, 6]) # x = [8, 3, 2, 3, 4, 7, 5, 6]
x.remove(3) # x = [8, 2, 3, 4, 7, 5, 6]
Y = x.pop(4) # x = [8, 2, 3, 4, 5, 6] # returns y = 7
x.sort() # x = [2, 3, 4, 5, 6, 8] # in place!
x = [5, 2, 3, 4, 3, 0]
if type(x) == list:
# do something
elif type(x) == tuple:
y = (3, 2, -3, 4, 0)
len(y) # Returns 5
min(y) # Returns -3
max(y) # Returns 4
sum(y) # Returns 6
sorted(y) # y = [-3, 0, 2, 3, 4]
Retrieval
Assignment
'[NUC-A 21.600]'
Refer: lists_tuples_dictionaries.py
None
False
Line-continuation character:
x = variable1 * variable2 + variable3 \
+ variable4 + a + lot + of + other \
+ variables inside * this / run-on \
* equation