Last active
August 29, 2015 14:07
-
-
Save Krewn/b67c6e3c4272a17f2bc7 to your computer and use it in GitHub Desktop.
A breif introduction to python for the newbies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# | |
# ___ ,---, ___ ___ ,--, | |
#,-.----. ,--.'|_ ,--.' | ,--.'|_ ,--.'|_ ,--, ,--.'| | |
#\ / \ | | :,' | | : ,---. ,---, | | :,' ,--, | | :,' ,---. __ ,-.,--.'| | | : | |
#| : | : : ' : : : : ' ,'\ ,-+-. / | : : ' : ,'_ /| : : ' : ' ,'\ ,' ,'/ /|| |, : : ' | |
#| | .\ : .--,.;__,' / : | |,--. / / | ,--.'|' | .;__,' / .--. | | :.;__,' / / / |' | |' |`--'_ ,--.--. | ' | | |
#. : |: | /_ ./|| | | | : ' |. ; ,. :| | ,"' | | | | ,'_ /| : . || | | . ; ,. :| | ,',' ,'| / \ ' | | | |
#| | \ :, ' , ' ::__,'| : | | /' :' | |: :| | / | | :__,'| : | ' | | . .:__,'| : ' | |: :' : / ' | | .--. .-. || | : | |
#| : . /___/ \: | ' : |__ ' : | | |' | .; :| | | | | ' : |__ | | ' | | | ' : |__' | .; :| | ' | | : \__\/: . .' : |__ | |
#: |`-'. \ ' | | | '.'|| | ' | :| : || | | |/ | | '.'|: | : ; ; | | | '.'| : |; : | ' : |__ ," .--.; || | '.'| | |
#: : : \ ; : ; : ;| : :_:,' \ \ / | | |--' ; : ;' : `--' \ ; : ;\ \ / | , ; | | '.'|/ / ,. |; : ; | |
#| | : \ \ ; | , / | | ,' `----' | |/ | , / : , .-./ | , / `----' ---' ; : ; : .' \ , / | |
#`---'.| : \ \ ---`-' `--'' '---' ---`-' `--`----' ---`-' | , /| , .-./---`-' | |
# `---` \ ' ; ---`-' `--`---' | |
# `--` | |
#Author : Kevin Nelson kpie314(at)gmail.com | |
from pylab import * | |
#Tools for plotting and what not | |
import os | |
#Operating system interface tools | |
import csv | |
#Spread Sheet reader | |
b = True | |
n = 42 | |
q = 'What kind of variable typing does python use?' | |
a = 'Lets just say python was designed with flexibility in mind.' | |
if(len(q)>n): | |
print str(n) + ' is > the number of characters in "' + q + '"' | |
else: | |
print q +' is longer than '+str(n) + 'characters' | |
# Above we see some basic closures in python | |
# Notice the meaning of : & tabs, making {} available for other purposes ... | |
#2 basic groups in python are lists [] and dictionaries {} | |
_myDictionary = {} | |
for key in range(0,10): | |
value=key**2 | |
_myDictionary[key]=value | |
#make a dictionary of squares 0 - > 10 | |
print _myDictionary | |
# if we want to reformat the way that a dictionary is printed we can define a method...\\ | |
def printDictionary(Dictionary): | |
for k in Dictionary: | |
print str(k) +'\t'+ str(Dictionary[k]) | |
pass | |
printDictionary(_myDictionary) | |
# now that we have seen how to use dictionaries in python lets do somthing with a list | |
def getPrimes(n): | |
primes = [2] | |
# instantiates our list to a list of one element, 2 | |
k = 3 | |
while(len(primes) < n): | |
# python uses the prefix function len(var) for lists dictionaries and strings | |
k2 = 0 | |
isprime=True | |
#Vacuously true assumption that every number is prime unless | |
while(primes[k2]**2<=k): | |
if(k%primes[k2]==0): | |
isprime=False | |
break | |
k2+=1 | |
if(isprime):primes.append(k) | |
k+=2 | |
return(primes) | |
print getPrimes(n) | |
try: | |
#Python will run the code in the try block top to bottom and only excecute the except block when an error is encountered | |
printDictionary(getPrimes(n)) | |
except(IndexError): | |
print('LIST is not Dictionary') | |
#Now that we have a LIST of primes, we can't use our printDictionary fuction | |
def printList(List): | |
k2=1 | |
ret='' | |
#Returning a string gives us more flexibility as opposed to printing the contents in the function | |
for k in List: | |
#Iterating through each element of the list | |
ret+=str(k2)+'\t'+str(k)+'\n' | |
k2+=1 | |
return(ret) | |
print(printList(getPrimes(n))) | |
#Python lets you nest your function calls. | |
def readFile(n,delim): | |
Data={} | |
# Data will be returned as the contents of a standard spreadsheet accessible with row and column number | |
with open(n) as csvfile: | |
#n is the string name of the file, or path to a file | |
tableMain=csv.reader(csvfile,delimiter=delim) | |
k=0 | |
for row in tableMain: | |
#Converts the list of lists returned by default from csv.reader into a dictionary of lists [][] -> {[]} | |
Data[k]=row | |
k+=1 #Integer labels for the rows of the table. | |
return(Data) | |
if not os.path.exists('PythonTutorial'):# Check if the folder 'PythonTutorial' exists | |
os.makedirs('PythonTutorial') # If not make it | |
os.chdir('PythonTutorial') # Set 'PythonTutorial' as the working directory | |
OutPut = open('PythonTutorial.tsv',"w") #Open a file called 'PythonTutorial.tsv' | |
OutPut.write(printList(getPrimes(n))) #Write a list of tab separated values to the file | |
OutPut.close() #Close the file | |
printDictionary(readFile('PythonTutorial.tsv','\t')) # Read and reprint the file. | |
primes = getPrimes(n) | |
t = arange(0., float(len(primes)), 1.) | |
s = primes | |
plot(t, s) | |
xlabel('Primes') | |
ylabel('Values') | |
title('About as simple as it gets, folks') | |
grid(True) | |
savefig("primes.png") | |
show() | |
# ,----, | |
# ,/ .`| | |
# ,` .' : ,---, ,---,. | |
# ; ; /,--.' | ,' .' | ,---, | |
#.'___,/ ,' | | : ,---.' | ,---, ,---.'| | |
#| : | : : : | | .' ,-+-. / | | | : | |
#; |.'; ; : | |,--. ,---. : : |-, ,--.'|' | | | | | |
#`----' | | | : ' | / \ : | ;/|| | ,"' | ,--.__| | | |
# ' : ; | | /' : / / | | : .'| | / | | / ,' | | |
# | | ' ' : | | |. ' / | | | |-,| | | | |. ' / | | |
# ' : | | | ' | :' ; /| ' : ;/|| | | |/ ' ; |: | | |
# ; |.' | : :_:,'' | / | | | \| | |--' | | '/ ' | |
# '---' | | ,' | : | | : .'| |/ | : :| | |
# `--'' \ \ / | | ,' '---' \ \ / | |
# `----' `----' `----' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment