PDA

View Full Version : New to Python, pointers?


turtle
2013-12-19, 23:17
So I managed to write a program to convert a word to Pig Latin. From nothing. Pretty cool if you asked me since I've never been much of a coder. How did I get that far in 6 hours of training? Code Academy (http://www.codecademy.com/tracks/python).

Is anyone familiar with this or other great tool to help me learn Python? Seems it's now expected of me at my job so I'm being paid to learn it now, though I'm not progressing very fast. The benchmark is some guy who already knows java(script?) according to my boss that did the Code Academy track in 8 hours. I'm 8 hours in now and about 38% done. :\

At this point I really should finish to Code Academy method either way. The lead Python developer at my job is constructing a Python challenge that I'll have to complete before I can be considered proficient or at least competent in it. By that point I'm guessing I'll want more skills than just the Code Academy stuff. Any direction or assistance you can offer to get me doing this right?

Brad
2013-12-19, 23:42
I love Python. Pretty much all of my one-off helper scripts I write these days are either Bash or Python. I'd really like to know more about your coworker's "Python challenge" if you're allowed to share it. :)

Two book-website lessons that I'd recommend looking at are:

Learn Python the Hard Way (http://learnpythonthehardway.org/)
Dive Into Python (http://www.diveintopython.net/)

They are both a bit old (written in the early 2000s), but the basics are still pretty sound. The only potential gotcha is that these books cover Python 2.x, not Python 3. A fair number of things changed in Python 3, enough that it caused a small schism in the developer community. Some major third-party Python libraries still only work in 2.x. So, the books may be just fine for you if your coworkers are still using 2.x.

The last time I really looked at these two sites was several years ago. There are probably newer/better resources today. I may take a look around the interwebs this weekend and post if I find some.

Kickaha
2013-12-19, 23:46
Ditto for Dive Into Python, that's how I learned it.

Would also recommend: http://www.ibiblio.org/catalog/items/show/3775

ObNurdJoke: Silly turtle! Python doesn't have pointers!

Brad
2013-12-20, 00:31
Would also recommend: How to Think Like a Computer Scientist (http://www.ibiblio.org/catalog/items/show/3775)
Yes! I forgot about this one. I remember reading it years ago with the other two books when I was learning Python too. This one is probably especially good for you, turtle, because it's geared more towards the beginner programmer as opposed to programmers who are beginning to learn Python.

Oh, and this one is also for Python 2.x. One of the quickest way to recognize Python 3 versus an older Python version in a tutorial is that if you ever see a print statement without parentheses, the tutorial predates Python 3. :) Python 3 changed print from a statement to a proper function (among many other cleanup-type things).

tomoe
2013-12-20, 10:21
Seconding LPTHW. Went through it last year when I was getting started with Python and found it helpful. If you're interested in a project based online class using python, it might be worth trying Udacity's Intro to programming (https://www.udacity.com/course/cs101). Haven't done the class myself, but know people who have and they all liked it.

turtle
2013-12-22, 00:18
Thanks for the input on these guides/references. I've bookmarked the ones to be bookmarked and noted the others. I've enjoyed the little bit of "coding" I've done with the Code Academy, but looking forward to being more capable and proficient. I don't like feeling like I don't have a clue when I am having much more expected of me.

Brad
2013-12-22, 00:32
Seconding LPTHW. Went through it last year when I was getting started with Python and found it helpful. If you're interested in a project based online class using python, it might be worth trying Udacity's Intro to programming (https://www.udacity.com/course/cs101). Haven't done the class myself, but know people who have and they all liked it.

Wife of a colleague of mine did this one, too. She had no programming background, but from what my friend said, she seemed to enjoy it and it worked quite well.

tomoe
2013-12-22, 19:43
The Udacity course? I've been meaning to do their algorithms class...mainly because it's taught by Michael Littmann who is one of my heroes and his early work on POMDPs was influential on my dissertation.

Brad
2013-12-22, 19:52
Yeah, the Udacity course. The description of "build a search engine" for the one you linked sounds a lot like the one my colleague described. I haven't actually used any of them myself, though.

Brad
2013-12-24, 13:52
I reached out to some colleagues about this topic, and here's what they had to say. These are all really smart guys whom I respect and trust. I mentioned the three online Python books that have been posted here, and I asked what else they might recommend for an IT guy who wants to get started programming with Python. I'll quote some of their replies here:

First, create the universe: http://www.amazon.com/C-Programming-Language-2nd-Edition/dp/0131103628

OK, not the whole universe, but still, if you want the "basics..." (BTW, check out the price on that relatively thin, 25-year-old book.)

But seriously, the references you list are as good as any for getting started. You might also include the "official" tutorials (for Python 2 and 3, respectively):
http://docs.python.org/2/tutorial/index.html
http://docs.python.org/3/tutorial/index.html

Once you get the basic feel of the language, you're just going to program with the library reference in another window anyway.

I think between your references and Kevin's, your friend's bases should be well-covered. I think when I had to learn it, I took O'Reilly's "Learning Python" into a cave and read it start-to-end, but that's a fairly dense (and not so cheap) text and I was coming from a background of (supposedly) already knowing how to program in several other languages, so HMMV if he goes that route.

I just started python/django programming in earnest for one of my clients. I haven't read any books, just pretty much rely on stackoverflow and my C/Java knowledge. Get a good basis and any language is easy. Start with python or shell, and you will never really understand some of the core concepts that are required.

All good points, especially the throwaway line that "you're just going to program with the library reference in another window anyway". That's absolutely true and you shouldn't feel bad about it! Nobody should expect you to have the full standard library memorized for a rich language like Python. There are hundreds of functions spread across dozens of modules. The same goes for Java, Ruby, Cocoa/ObjC, PHP, etc etc. You should understand the syntax and know some core/common functions by heart, but keeping a reference manual at hand is a must.

Also, about the "C Programming Language" book... the more I think about it, that's actually a clever idea. Being able to write or at least understand C programs is an essential skill that all software developers should have. C is actually surprisingly simple, and many other modern languages are based on its principles, syntax, and keywords. Knowing C means you can probably pick up (or at least read and understand) several other languages with a small bit of effort.

This book is a gentle introduction to C that teaches by example. It's a bible that most programmers I know either own or have read at some point in their lives. If you don't want to swallow the ridiculous price tag for it, it's not hard to find scanned and OCRed as a PDF with a few searches, not that I could condone such a thing. ;)

If you really want to pursue programming as a larger part of your career, I would highly suggest giving the C book a read. If you just want to get enough Python-specific coding skills to get through your job, you could pass on it, but I'd still recommend reading it in the not too distant future.

turtle
2013-12-24, 23:01
Thanks for doing that Brad! I appreciate the input. I took intro to C++ my last semester of college and didn't do very well in that class at all. It really wasn't pretty. This is why for the longest time I've told myself, and others, that I'm a tech not a coder. I can edit code and manipulate it, but writing it is a whole different thing all together. I really want to learn Python at this point, so I hope to make it through this.

Brad
2013-12-25, 02:01
FWIW, C is simpler than C++ and should certainly be easier for a beginner to understand. C++ is a superset of C that changes some compiler behaviors, expands the standard library, and adds object-oriented features. C++ may be more powerful, but that power comes with more language complexity (though code you write may be simpler) and a steeper learning curve.

That said, unless you count recently flipping through the C book, I haven't read or written a line of C or C++ code in about ten years. :p

turtle
2013-12-25, 10:21
I didn't realize that at all. Back when I was a SysOP for WWIV (http://en.wikipedia.org/wiki/WWIV) (over 20 years ago) I paid for the source and modified it all the time. Made some major edits too, an this was in C++. I didn't realize they were different. :o

Well, at least I have a more likely chance of picking up and enjoying C or Python then.

Brad
2014-02-09, 21:57
Progress update! :)

That is to say the people need one!

How's the Python coming?

turtle
2014-02-10, 08:28
Still working through the code academy coursework. I'm about half way through it. I haven't put much time into learning this in my off time because of family obligations. I really need to because this pace is horribly slow. I feel like I'm getting the concepts, still not seeing a big picture of how I'm going to take these concepts and make them usable for tasking.

I still default to bash scripting in my head when thinking of ways to automate. I've built a few little things in this course that seem like they will be useful. I guess the more I think about it I haven't grasped building the overall plan for new code. I'm go with modifying in most cases because it's already been done.

I am going to get through this though and be good. I think I need a video lecture to watch to help me understand some of this stuff more. In electronics school in the Navy I was told to draw a big red button that said "I believe". Any time they hit topics that they weren't going to go more into detail they said push that button. I feel like I'm learning this doing the same thing. I want more understanding because it makes the little details easier to figure out.

I'm doing pretty good with the syntax at least! That was always a problem for me.

turtle
2014-11-02, 22:07
So I must be missing something that I would think it pretty basic. I learned how to write functions doing the codecademy coarse but don't know where to look to find out about functions already written. I understand the ones written custom for my organization won't be as easy to find, but how can I find functions that are already done and documented. You know, python-wide functions?

I've been tasked to convert some of our older bash scripts to python but don't want to reinvent the wheel and write new functions when they might already be done. Got any pointers? I'm having to use python 2.7 for my job.

Noel
2014-11-05, 09:49
Perhaps a cheat sheet, like this one (http://www.astro.up.pt/~sousasag/Python_For_Astronomers/Python_qr.pdf), is what you need?

To go a little deeper, the documentation (https://docs.python.org/2/library/) for the Python 2.7 standard library (i.e. all of the included modules/packages) is not half bad.

Kickaha
2014-11-05, 18:52
Yup. Use the standard docs, but google like mad.

drewprops
2014-11-05, 19:19
What does Python compile on? I've enjoyed dicking around with PHP for web work, but I don't have an environment set up locally to use it so it's only a web thang for me (and that's easy and fun).


...

ThunderPoit
2014-11-05, 22:50
Python doesn't compile, it's interpreted like php

Noel
2014-11-06, 14:13
Oh, and Stack Overflow (http://stackoverflow.com) has had an answer for just about every basic Python question I've ever had (usually asked by someone else before I even got there), in case the official docs aren't enough.

turtle
2014-11-10, 23:26
Thank you again for the help. I have written (and borrowed from Stack Overflow) some functions to do specific tasks I need. Now I'm stuck trying to figure out how to make them actually work together.

That is my biggest gripe with Codecademy, they taught me how to write functions which is great. How do I string them together to make a script that takes an input and reacts to that input though?

One of my coworkers is going to help me with this tomorrow if my workload is low enough to allow it. Then I will have converted my first bush script to python.

turtle
2014-11-21, 20:45
So I have submitted my first script to code review for deployment!

It checks our servers for an IP being blocked in the firewall. While this may not seem like a big deal, with the number of customers we have, it is.

I've just finished my second code to unblock an IP in our firewalls. I didn't get to submit it yet, but that's because I finished writing it tonight 2 minutes before heading home for the weekend. :D

So I have a question: Is sh just an avenue to call sbin binaries? I was wanting to run bash CLI commands and discovered that you would use sh differently.

Example:
sudo apf -u 192.168.1.2
would be written

if stuff:
with sh.sudo:
sh.apf(u='192.168.1.2')