In [1]:
3+2
Out[1]:
5
In [2]:
print(2**3)
8
In [3]:
(3+5)-(2-1)
Out[3]:
7
In [4]:
x = 2
In [5]:
print (x)
2
In [6]:
x = 3
In [7]:
print (x)
3
In [8]:
x = 3.6789
In [9]:
print (x)
3.6789
In [10]:
my_num = 99
In [11]:
my_str = "luft ballons"
In [15]:
print (my_num, my_str, x)
99 luft ballons 3.6789
In [16]:
x
Out[16]:
3.6789
In [17]:
x + 3
Out[17]:
6.6789000000000005
In [18]:
x + my_num
Out[18]:
102.6789
In [19]:
z = x+my_num
In [20]:
print (z)
102.6789
In [27]:
def addtwo(myVar):
    print(myVar+2)
addtwo(3)
5
In [28]:
addtwo(5)
7
In [29]:
addtwo(x)
5.6789000000000005
In [30]:
%pylab inline
Populating the interactive namespace from numpy and matplotlib
In [31]:
mySum(3, 0.1)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-31-e44c90105b96> in <module>()
----> 1 mySum(3, 0.1)

NameError: name 'mySum' is not defined
In [34]:
def mySum(x, y):
    z = x+y
    print (z)
In [35]:
mySum(3, .1)
3.1
In [36]:
z = mySum(3,.1)
3.1
In [37]:
print (z)
None
In [39]:
def mySum(x, y):
    return x+y # return the value of x+y
In [40]:
z = mySum(3, .1)
In [41]:
print (z)
3.1
In [42]:
print (mySum(3, .1))
3.1
In [43]:
print(x)
3.6789
In [44]:
x = 3
In [45]:
cos(x)
Out[45]:
-0.98999249660044542
In [47]:
print (cos(2*pi*x))
1.0
In [48]:
print (floor(2.6893567))
2.0
In [49]:
print (ceil(2.576787))
3.0
In [53]:
sqrt(floor(2.567))
Out[53]:
1.4142135623730951
  • write a function called remainder that accepts two arguments x & y and returns the remainder of x/y
  • write a function calle remainder that prints x/y but returns the remainder of x/y
In [68]:
def getDecimal(var1, var2):
    return var1/var2 - floor(var1/var2)
In [65]:
def getRemainder(var1, var2):
    return getDecimal(var1, var2)*var2
In [69]:
print (getDecimal(3,2))
0.5
In [70]:
print (getRemainder(3,2))
1.0
In [75]:
plot([1,2,3,4], [-1, 4, 7, .1], 'ro',linewidth=5)
Out[75]:
[<matplotlib.lines.Line2D at 0x77583c8>]
In [76]:
myList = [1,2]
In [77]:
myList = ['hello', 2, .1, -1]
In [78]:
myList.append("goodbye")
In [81]:
print(myList)
['hello', 2, 0.1, -1, 'goodbye']
In [82]:
myList.remove(2)
In [83]:
print(myList)
['hello', 0.1, -1, 'goodbye']
In [84]:
myList.insert(3, "yay")
In [85]:
print (myList)
['hello', 0.1, -1, 'yay', 'goodbye']
In [86]:
help insert
  File "<ipython-input-86-700d733178fa>", line 1
    help insert
              ^
SyntaxError: invalid syntax
In [87]:
help(insert)
Help on function insert in module numpy.lib.function_base:

insert(arr, obj, values, axis=None)
    Insert values along the given axis before the given indices.
    
    Parameters
    ----------
    arr : array_like
        Input array.
    obj : int, slice or sequence of ints
        Object that defines the index or indices before which `values` is
        inserted.
    
        .. versionadded:: 1.8.0
    
        Support for multiple insertions when `obj` is a single scalar or a
        sequence with one element (similar to calling insert multiple
        times).
    values : array_like
        Values to insert into `arr`. If the type of `values` is different
        from that of `arr`, `values` is converted to the type of `arr`.
        `values` should be shaped so that ``arr[...,obj,...] = values``
        is legal.
    axis : int, optional
        Axis along which to insert `values`.  If `axis` is None then `arr`
        is flattened first.
    
    Returns
    -------
    out : ndarray
        A copy of `arr` with `values` inserted.  Note that `insert`
        does not occur in-place: a new array is returned. If
        `axis` is None, `out` is a flattened array.
    
    See Also
    --------
    append : Append elements at the end of an array.
    concatenate : Join a sequence of arrays together.
    delete : Delete elements from an array.
    
    Notes
    -----
    Note that for higher dimensional inserts `obj=0` behaves very different
    from `obj=[0]` just like `arr[:,0,:] = values` is different from
    `arr[:,[0],:] = values`.
    
    Examples
    --------
    >>> a = np.array([[1, 1], [2, 2], [3, 3]])
    >>> a
    array([[1, 1],
           [2, 2],
           [3, 3]])
    >>> np.insert(a, 1, 5)
    array([1, 5, 1, 2, 2, 3, 3])
    >>> np.insert(a, 1, 5, axis=1)
    array([[1, 5, 1],
           [2, 5, 2],
           [3, 5, 3]])
    
    Difference between sequence and scalars:
    >>> np.insert(a, [1], [[1],[2],[3]], axis=1)
    array([[1, 1, 1],
           [2, 2, 2],
           [3, 3, 3]])
    >>> np.array_equal(np.insert(a, 1, [1, 2, 3], axis=1),
    ...                np.insert(a, [1], [[1],[2],[3]], axis=1))
    True
    
    >>> b = a.flatten()
    >>> b
    array([1, 1, 2, 2, 3, 3])
    >>> np.insert(b, [2, 2], [5, 6])
    array([1, 1, 5, 6, 2, 2, 3, 3])
    
    >>> np.insert(b, slice(2, 4), [5, 6])
    array([1, 1, 5, 2, 6, 2, 3, 3])
    
    >>> np.insert(b, [2, 2], [7.13, False]) # type casting
    array([1, 1, 7, 0, 2, 2, 3, 3])
    
    >>> x = np.arange(8).reshape(2, 4)
    >>> idx = (1, 3)
    >>> np.insert(x, idx, 999, axis=1)
    array([[  0, 999,   1,   2, 999,   3],
           [  4, 999,   5,   6, 999,   7]])

In [88]:
nameList=['youngsun', 'janet', 'aleksandra', 'tayshana']
print(nameList)
['youngsun', 'janet', 'aleksandra', 'tayshana']
In [89]:
nameList.append("laura")
In [90]:
print (nameList)
['youngsun', 'janet', 'aleksandra', 'tayshana', 'laura']
In [91]:
nameList[3]
Out[91]:
'tayshana'
In [112]:
nameList.remove('janet')
In [92]:
nameList[0]
Out[92]:
'youngsun'
In [93]:
print(len(nameList))
5
In [98]:
print (nameList[len(nameList)-1])
laura
In [ ]:
 
In [99]:
print (nameList[4])
laura
In [108]:
newList = [1, 2]
In [101]:
print(newList[1])
2

(): used in declaring, calling functions; math order of operations []: used to declare a list; access an element of the list

In [102]:
newList[1]
Out[102]:
2
In [103]:
print(newList)
[1, 2]
In [104]:
newList.remove(2)
In [105]:
print(newList)
[1]
In [110]:
z = newList[1]
In [111]:
print(z)
2
In [113]:
newList = [1,2,3]
In [114]:
del newList[1]
In [115]:
print(newList)
[1, 3]
In [116]:
xList = [1,2,3]
yList1 = [4,5,6]
yList2 = [1,2,3]
In [119]:
plot(xList, yList1, "go-", linewidth=2)
plot(xList, yList2, 'r+-', linewidth=5)
Out[119]:
[<matplotlib.lines.Line2D at 0x783f6a0>]
In [120]:
xs = array([0,1,2,3])
In [123]:
xs = [0,1,2,3]
print (xs+3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-123-3937f58ab34e> in <module>()
      1 xs = [0,1,2,3]
----> 2 print (xs+3)

TypeError: can only concatenate list (not "int") to list
In [137]:
xs = array([0,1,2,3])
print (xs+3)
ys = xs+3
[3 4 5 6]
In [126]:
xs = linspace(-5,5, 200) # create an array from -5 to 5 with 200 equally spaced values
In [127]:
myGauss = exp(-xs**2)
In [136]:
plot(xs, myGauss, 'r', label="a gaussian plot!")
legend()
xlabel("my x")
ylabel('my y')
Out[136]:
<matplotlib.text.Text at 0x8b81860>
In [138]:
myX = array([0, 1, 2, 3, 4])
myY = myX**2
In [140]:
print(myY[4])
16
In [141]:
plot(myX, myY)
Out[141]:
[<matplotlib.lines.Line2D at 0x8c21128>]
In [142]:
mat = array([[1,2,3],[10,11,12]])
In [143]:
print (mat)
[[ 1  2  3]
 [10 11 12]]
In [144]:
print(mat[1][2])
12
In [145]:
myRow = mat[0, :]
In [146]:
print(myRow)
[1 2 3]
In [147]:
myCol = mat[:,0]
In [148]:
print (myCol)
[ 1 10]
In [ ]: