Python Programming


NodeBox – Graphics with Python

NodeBox is a Mac app created for the purpose of creating graphics with Python.

Amazing app – I love it.

http://nodebox.net/code/index.php/Home

Download it and go through the tutorials.

You can also save images of what you create (and movies). Share what you make here :)

Ed


Python Remove Vowels from String

Problem: Remove Vowels from String

a straight forward way to solve this:


test = 'apple johnny seed'
for eachLetter in test: # eachLetter can be replaced with any other name
        if eachLetter in ['a','e','i','o','u']:
                test = test.replace(eachLetter, '')
print test

Download .py file


Blogs

http://koldfyre.wordpress.com/

http://pythonprog.wordpress.com/

http://iphoneaday.wordpress.com/

NEW – http://veryus.wordpress.com/
NEW – http://learnware.wordpress.com/

http://4pplescript.wordpress.com/


Eclipse + PyDev

I have completely fallen for the Eclipse + PyDev combination. The original IDLE for Python is also awesome, no doubts about that, but Eclipse totally pushes it up to a whole new level.

The good thing about Eclipse is that it suggests many things for you (such as methods, properties, etc) and you can run the python program in a console in the same window. Visually more aesthetic and all your files can be organized into a workspace that Eclipse makes. I would recommend making the workspace (where all your programs go and all) on your flash drive. This could slow it down (maybe) but it offers portability for Windows PCs or Macs that have Eclipse on them.

To get PyDev, download the new Eclipse (I recommend Java version because I use Java) and then download the new PyDev from here. Unzip both of them and then open the PyDev unzipped folder. There will be 2 folders that share the same name as the Eclipse unzipped folder. Take the contents of the PyDev folder and move them into the Eclipse folders.

Now when you open up Eclipse, enter where you want your workspace and then go to Window > Open Perspective > Other and then select PyDev. Only one more thing left to do – select Preferences under the “Eclipse” menu item (Command + ,). Add Python path and you’re done.


Python Evaluation of Strings

Python code:
nums = [1,2,3,4]
equation = '+'.join(nums)
# yields '1+2+3+4' with type(equation) string

How to evaluate this? A simple int(equation) will not give you the answer of 10 that you want...

Answer: eval(equation) will give you the right answer.

Special newb Treatment:

# prints 10
print eval('1+2+3+4')


Ctrl + C Doesn’t Work?!

Interesting nested try except code – why? Because we can…

def main():
	try:
		while 1:
			print "hi"
	except KeyboardInterrupt:
		try:
			while 1:
				print "ho"
		except KeyboardInterrupt:
			try:
				while 1:
					print "ha"
			except KeyboardInterrupt:
				try:
					while 1:
						print "he"
				except KeyboardInterrupt:
					try:
						while 1:
							print "hu"
					except KeyboardInterrupt:
						while 1:
							print "eeek"


Python Problem: Anagram Tester

Python Programming Problem:
Read in an input file – first line contains one word. Next line contains the number of words/line that will follow. Those words after that number will each be checked against the word in the first line to see if it is one of the following: identical, an anagram, or not an anagram.

If you do not understand, here are the original instructions. Get your sample input files and expected answers from that link, too.
Continue reading this entry »


Python Problem: Remove Vowels

Python Programming Problem:
Create a function to take in a string as an argument/parameter and then return a string with all the vowels removed.
Continue reading this entry »


Modules – Simple!

Modules are a really simple concept and easy to make. Modules are basically just python programs with additional functionalities. Modules make it possible to use functions made by other people or functions that you do not want to recreate.

First off, to create a module, create a regular .py file with many functions defined. For example:


# myfunctions.py
def doubleNum(x):
      return 2*x

Now, in a different file, instead of creating another doubleNum() function, just import this file.
Continue reading this entry »


Irish Programming Contest Problem 1

From here is some Irish Programming Contest Problems (3 of them).

I finished it and will most likely explain most of the code in a subsequent update.

Code [file: anagramTester.py]:

# http://www.compapp.dcu.ie/~cdaly/ire_comp/ire01/problems/prob1.html
# Tests whether strings are anagrams of a given word
answers = []

f = open('anagramTesterData.dat')
data = f.read()
data = data.split('\n')

original = data[0]
numberTests = data[2:len(data)]

for test in numberTests:
i = 0
if test == original:
answers.append('IDENTICAL')
else:
temp = original
temp = list(temp)
for char in test:
if char in temp:
temp.remove(char)
else:
i = 1
answers.append('NOT AN ANAGRAM')
break
if not (i == 1):
answers.append('ANAGRAM')

print answers

To test if it works [file: anagramTesterData.dat]:
cares
5
scare
races
cares
another
acres

With expected (correct) output:
ANAGRAM
ANAGRAM
IDENTICAL
NOT AN ANAGRAM
ANAGRAM