Normal
$ python foo.py
Interactive
$ python
iPython (recommended)
$ ipython
Jupyter / iPython notebook (also recommended)
$ jupyter notebook
This is similar to iPython but browser based. It has some very cool features. For development, I have a slight preference for the simpler command-line iPython.
IDEs: PyCharm or Visual Studio Code
You are on your own to figure this out. It may be very nice once setup, but you also may have some startup costs to configure it to work nicely with anaconda, matplotlib, etc. I will not support issues with IDEs.
Windows open:
This mimics MATLAB or R desktop environments.
Plan
A cell is a block of code. Click the cell below and press the "play" button above or type Shift+Enter. The code will run, print output, and then advance to the next cell. Type Ctrl+Enter to run but not advance.
x = 2*x
print(x)
Create a file in the same directory called hello.py
with this line
print 'hello world'
Now you can run it from jupyter:
%run hello.py
%matplotlib inline
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2.3, 3.5, 4.8, 20]
plt.plot(x, y)
plt.show()
X=5 #Set X to integer object equal to 5
print("1: X",X)
X=5.0 #Set X to float object equal to 5.0
print("2: X=",X)
X=True #Set X to boolean True
print("3: X=",X)
X=500000000000000000000000000000000000000000000000000000000000000 # Python 3 integers have arbitrary precision
print("4: X=",X)
X="This is a string" #Set X to a string object defined using double quotes
print(X)
Y='So is this!' #Set X to a string object defined using single quotes
print(Y)
print(X[0])
print(X[0:5] )
print(X.split())
print(X + Y)
print(X + '. ' + Y)
X = (1,2,3,4,5) #Define tuple
print(type(X))
print("X=", X) #Print the tuple
print("X[0]=", X[0]) #Print the first element of the tuple
print( X[4] ) #Print the last element of the tuple
print( X[-1] ) #Print the last element of the tuple
print(len(X)) #Pring length of tuple
X[0]=5 #Set element of tuple ! ERROR (tuples are immutable)
X = [1,2,3,4,5] #Define list
print(type(X)) #Show type of X
print(X) #Print the list
print(X[0]) #Print the first element of the list
print(X[4])
print(X[-1]) #Print the last element of the list
print(len(X))
X[0] = 5 #Change the list.
print(X)
X.append(6) # Append *one* item to list
print(X)
X = [1, 2, 3, 4, 5, 6]
X.extend([7, 8]) # add elements 7, 8 to end of X
print(X)
Y = X + [7,8] # concatenate and retern a *new* list
print(X)
print(Y)
X = [1, 2, 3, 4, 5]
Y=X*2 # concatenate two copies of X (think: X+X)
print(Y)
X = {"Bob": 192731, "Mary": 281927, "Joe": 103124} #Define a dictionary of key-value pairs
print("type(X)",type(X))
print("X=", X) #Print the dictionary
print('X["Joe"]=', X["Joe"]) #Print the value for Joe
print('len(X)', len(X)) #Print the number of items
print('X.keys()=',X.keys()) #Print the list of keys
print('X.values()=',X.values()) #Print the list of values
X["Joe"]=772345 #Change an element
print("X=", X)
X["Alice"]=111111 #Add a new element
print("X=", X)
X = [1,"Foo",False,[1,2,3],(1,True)] #Lists can contain mixed types!
print("X=", X) #Print the list
#How can we get the second item in the tuple at the end of the list?
print(X[-1][1])
A = 5 #Assign the object "5" to the name A
B = A #Assign the object that A refers to to the name B
print("A=",A,"B=",B )
B = 7
print("A=",A,"B=",B)
A = [1,2,3,4,5]
B = A
print("A=",A,"B=",B )
A = [1,2,3,4,5]
B = A
B = [5,4,3,2,1]
print ("A=",A,"B=",B )
A = [1,2,3,4,5]
B=A
A.append(6)
B.append(7)
print ("A=",A,"B=",B )
A = [1,2,3,4,5]
B = A
B[0]=-1
print ("A=",A,"B=",B)
#Basic If-Elif-Else
X=5
if(X<0):
print("X is negative")
elif(X==0):
print("X is zero")
else:
print("X is positive")
#Basic for loop using range()
A = [1, 2, 3, 4, 5]
for i in range(len(A)):
print(i, A[i])
#Basic while loop
x=5
while(x > 0):
print(x),
x-=1
print ("2**4=", 2**4 ) # Computing Powers
print ("2/4=", 2/4 ) # "True division" of int by int
# Note: the / operator for ints was "floor division"
# in Python 2.x, so would give a result of 0 for this example
print ("2//4=", 2//4 ) # "Floor division"
print ("2/4.=", 2/4.) # Division of int by float (True division)
print ("2./4=", 2./4) # Division of float by int
print ("True or False=", True or False) #Logical or
print ("True and False=", True and False) #Logical and
print ("not(False)=", not(False)) #Logical not
def f(a): #Define a basic function of 1-argument
return a**2
print ("f(2)=",f(2))
def g(h,a,b): #Define a function of 3-arguments where h is a function and a and b are numbers
return h(a)+h(b)
print ("g(f,2,3)=",g(f,2,3))
import numpy as np
X = np.array([[1,2],[3,4],[5,6]])
print ("X=\n",X)
print ("X.shape=", X.shape)
print ("X[0,2]=", X[0,1])
print ("X[0,:]=", X[0,:])
Y = X[0,:]
print(Y.shape)
print ("X[:,1]=", X[:,1])
print ("2*X=\n", 2*X)
print ("3: 5+X=\n", 5+X)
Y = np.array([[-1,1,-1]])
print ("Y=\n",Y)
print ("6: Y.T=\n", Y.T)
print ("Y.dot(X)=\n", Y.dot(X))
print ("np.dot(Y, X)=\n", np.dot(Y, X))
print ("Y.T + X =\n", Y.T + X)