\(f(x) = 2\)
In [10]:
def f(x):
return 2
In [4]:
f(2)
Out[4]:
2
In [5]:
-f(2)
Out[5]:
-2
In [6]:
def functionprinter(g):
print(g(2))
In [14]:
functionprinter(f)
*
*
None
In [12]:
def f(x):
for i in range(x):
print('*')
In [13]:
f(5)
*
*
*
*
*
In [15]:
1 + 4
Out[15]:
5
In [16]:
dir(1)
Out[16]:
['__abs__',
'__add__',
'__and__',
'__bool__',
'__ceil__',
'__class__',
'__delattr__',
'__dir__',
'__divmod__',
'__doc__',
'__eq__',
'__float__',
'__floor__',
'__floordiv__',
'__format__',
'__ge__',
'__getattribute__',
'__getnewargs__',
'__gt__',
'__hash__',
'__index__',
'__init__',
'__int__',
'__invert__',
'__le__',
'__lshift__',
'__lt__',
'__mod__',
'__mul__',
'__ne__',
'__neg__',
'__new__',
'__or__',
'__pos__',
'__pow__',
'__radd__',
'__rand__',
'__rdivmod__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rfloordiv__',
'__rlshift__',
'__rmod__',
'__rmul__',
'__ror__',
'__round__',
'__rpow__',
'__rrshift__',
'__rshift__',
'__rsub__',
'__rtruediv__',
'__rxor__',
'__setattr__',
'__sizeof__',
'__str__',
'__sub__',
'__subclasshook__',
'__truediv__',
'__trunc__',
'__xor__',
'bit_length',
'conjugate',
'denominator',
'from_bytes',
'imag',
'numerator',
'real',
'to_bytes']
In [18]:
(1).__add__(2)
Out[18]:
3
In [70]:
class MyNumber:
def __init__(self, value):
self.value = value
def __repr__(self):
return 'MyNumber("{}")'.format(self.value)
def __str__(self):
return self.value
def __add__(self, other):
if self.value == 'one' and other.value == 'one':
return MyNumber('two')
In [71]:
one = MyNumber('one')
In [72]:
one + one
Out[72]:
MyNumber("two")
In [73]:
one + one + one
In [74]:
a = 1, 2, 3
In [80]:
a = 1, 2, 3
In [81]:
a, b = 1, 2
In [82]:
c = 1, 2
In [83]:
a, b = c
In [76]:
type(a)
Out[76]:
tuple
In [77]:
a
Out[77]:
(1, 2, 3)
In [78]:
b = [1, 2, 3]
In [79]:
type(b)
Out[79]:
list
In [86]:
a = ((((((1))))))
In [87]:
type(a)
Out[87]:
int
In [89]:
a = 1,
In [90]:
a
Out[90]:
(1,)
In [91]:
import numpy
In [93]:
t = (1, 2)
z = numpy.zeros(t)
In [101]:
a = [1, 2, 3]
b = a
b[1] = 999
a
Out[101]:
[1, 999, 3]
In [99]:
a is b
Out[99]:
False
In [102]:
a = 1
b = a
In [103]:
b is a
Out[103]:
True
In [104]:
b = 2
In [107]:
a = (1, 2, 3)
b = a
b[2] = 999
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-107-cf1c42a0b4da> in <module>()
1 a = (1, 2, 3)
2 b = a
----> 3 b[2] = 999
TypeError: 'tuple' object does not support item assignment
In [109]:
a + a
Out[109]:
(1, 2, 3, 1, 2, 3)
In [110]:
b = [1, 2, 4]
In [111]:
b.append(3)
In [112]:
b
Out[112]:
[1, 2, 4, 3]
In [113]:
s = '1234'