I've written my first actual useful and working piece of Python yesterday. It's a script that converts coordinates from decimal degrees to degrees/minutes/seconds, and I found such a nice way to do it in less than a dozen lines of code. Python is amazing.
you can probably get that less than a dozen lines down to less than seven. I love python.
Oh absolutely. Here's my finished, neat-ified code. Could be muuuch more simple but this was an assignment to show that I can, in fact, write a script.
LatCoord = input('Enter the latitude coordinate here: ')
LongCoord = input('Enter the longitude coordinate here: ')
#Puts the coordinates in a list
coordinate = [LatCoord, LongCoord]
#Check if the coordinates are valid, and if it isn't, end script.
if abs(LongCoord) > 180 or abs(LatCoord) > 90:
print: 'Invalid coordinate!'
break
#Loop to convert the list of coordinates
for x in coordinate:
D = int(x) #Calculate the Degree
decimal1 = abs(x - D) #Calculate the absolute Minutes (only D can be pos/neg)
minutes = 60 * decimal1 #from the remaining decimals
M = int(minutes)
decimal2 = minutes - M #Calculate the Seconds
seconds = 60 * decimal2 #from the remaining decimals
S = int(round(seconds)) #and round the number
if x == LatCoord: #Checks if the num is lat or long
print "Your Latitude is: ", D, M, S
else:
print "Your Longtitude is: ", D, M, S
That is a very neat script, reminds me of the days when I first started using it, and wasn't hacking together a MUD server/client. I really need to go back and do more with that... I've written some awesome tools for work to automate some things, notifications being one of them. And damn if it isn't the first or second prettiest code I've ever written.
I love this language. I'll never understand it but damn if it isn't perfect. A cold beauty.
It is a very lovely and elegant language - I wish I had more opportunity to use it.
I worked through "Learn Python the Hard Way" a while back, and really enjoyed that. At that time there was a lot of turbulence about Python 2 vs Python 3; not sure if that dust has settled yet or not.
they've announced python 4... holy crap, I still haven't converted to 3...
(my post before I heard 4 was going to drop) If you're just starting, definitely go with 3. It really is superior (enough to count) and you'll be ahead of the curve. It's hard to get out of old python habits.