# Python Tutorial

Scipy/Numpy

Python(x,y)

Enthought

Python Bootcamp

Matplotlib

Software Carpentry: Introductory material on Python

If you need help with a Python problem, but don't know where to find the answer, maybe we can be of service. Contact us at python@hpc.uark.edu.

1. ### Introduction: Basic Python

• Hello World
• print "Hello World"


• 3D Contour
• from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

fig = plt.figure()
ax = axes3d.Axes3D(fig)
X, Y, Z = axes3d.get_test_data(0.05)
cset = ax.contour(X, Y, Z, 16, extend3d=True)
ax.clabel(cset, fontsize=9, inline=1)

plt.show()


• # By Armin Moser
#Courtesy of Matplotlib examples: http://matplotlib.sourceforge.net/examples/index.html

from mpl_toolkits.mplot3d import Axes3D
import matplotlib
import numpy as np
from matplotlib import cm
from matplotlib import pyplot as plt
step = 0.04
maxval = 1.0
fig = plt.figure()
ax = Axes3D(fig)

# create supporting points in polar coordinates
r = np.linspace(0,1.25,50)
p = np.linspace(0,2*np.pi,50)
R,P = np.meshgrid(r,p)
# transform them to cartesian system
X,Y = R*np.cos(P),R*np.sin(P)

Z = ((R**2 - 1)**2)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet)
ax.set_zlim3d(0, 1)
ax.set_xlabel(r'$\phi_\mathrm{real}$')
ax.set_ylabel(r'$\phi_\mathrm{im}$')
ax.set_zlabel(r'$V(\phi)$')
ax.set_xticks([])
plt.show()


• 3D Surface
• #Courtesy of Matplotlib examples: http://matplotlib.sourceforge.net/examples/index.html
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet)

plt.show()


• Basic Conditionals
• list2=[1,2,"cat","dog"]

if "goat" in list2:
print "We have a goat!"
elif "weasel" in list2:
print "We have a weasel!"
else:
print "We have nothing!"


• Basic Function
• def mid(x,y):
return(x+y)/2

print mid(0,10)
print mid(0,11)


• Basic Function 2
• def mid(x,y):
return(x+y)/2.0

print mid(0,10)
print mid(0,11)


• Basic Import
• import math
print math.sin(3)

from math import sin
print sin(3)

#Uncomment the "help(math)" below to see all the function in the math module
#help(math)


• Basic Lists
• #A simple list
list1=[1,2,3,4]
print list1

#A list of mismatched things
list2=[1,2,"cat","dog",12.24356]
print list2

#An empty list
empty=[]
print empty

#We can add things to lists
list1.append(5)
print list1

#Lists are indexable
print list1

#We can "slice" lists to get subsequences
print list1[2:4]
print list1[:4]

#We can even index from the end of the list
print list1[-1]
print list1[-2:]

#We can see if something is in a list
"dog" in list1
"dog" in list2


• Basic Math
• #Subtraction
print 5-12
print 5.0-12.0

#Divide two integers
print 5/12

#Divide two floating point numbers
print 5/12.0
print 5.0/12
print 5.0/12.0

#Python supports big integers
print 2**65
print 2**128

#and complex numbers
print 3+4j
print 3+4j - 1+2j
print (3+4j) - (1+2j)


• FizzBuzz
• #this is the fizzbuzz test
for i in range(16):
if i % 3 == 0 and i % 5 == 0:
print 'fizzbuzz'
elif i % 3 == 0:
print 'fizz'
elif i % 5 == 0:
print 'buzz'
else:
print i


2.

3. ### Data Analysis

• Basic Arrays
• Basic Stats
• Make Data
4.

6.