Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pknowledge/d0ef8b64ca2508be60ffb4e32accf8cf to your computer and use it in GitHub Desktop.
Save pknowledge/d0ef8b64ca2508be60ffb4e32accf8cf to your computer and use it in GitHub Desktop.
Python Numpy Tutorial for Beginners Python List Vs Numpy Array
C:\Users\ProgrammingKnowledge\PycharmProjects\NumPySamples\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.2\helpers\pydev\pydevconsole.py" 64655 64656
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['C:\\Users\\ProgrammingKnowledge\\PycharmProjects\\NumPySamples', 'C:/Users/ProgrammingKnowledge/PycharmProjects/NumPySamples'])
PyDev console: starting.
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
>>> import numpy as np
>>> L = [1, 2, 3]
>>> NA = np.array(L)
>>> for i in L:
... print(i)
...
1
2
3
>>> for i in NA:
... print(i)
...
1
2
3
>>> L1 = L + [4]
>>> L1
[1, 2, 3, 4]
>>> L1.append(5)
>>> L1
[1, 2, 3, 4, 5]
>>> NA1 = NA + [4]
>>> NA1
array([5, 6, 7])
>>> NA.append(8)
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'append'
>>> NA2 = NA + NA
>>> NA2
array([2, 4, 6])
>>> L2 = L + L
>>> L2
[1, 2, 3, 1, 2, 3]
>>> L3 = []
>>> for i in L:
... L3.append(i + i)
...
>>> L3
[2, 4, 6]
>>> 2*Na
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'Na' is not defined
>>> 2*NA
array([2, 4, 6])
>>> 2*L
[1, 2, 3, 1, 2, 3]
>>> NA**2
array([1, 4, 9], dtype=int32)
>>> L**2
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
>>> for i in L:
... print(i*i)
...
1
4
9
>>> np.sqrt(NA)
array([1. , 1.41421356, 1.73205081])
>>> np.log(NA)
array([0. , 0.69314718, 1.09861229])
>>> np.exp(NA)
array([ 2.71828183, 7.3890561 , 20.08553692])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment