TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Quick way to get started with python? (for php developers)

16 pointsby andreover 14 years ago
any tips, tutorials or books to start out strong with python?

9 comments

Figsover 14 years ago
Python's a ridiculously easy language to learn. Here are a few examples of bits of syntax.<p><pre><code> # comments start with a "#" symbol [1,2,3,4] # that's a list {"x":1, "y":2, "z":3} # that's a dictionary blah = 6 # you can assign things to variables asdf = [3,6,1,2] # including lists, dictionaries, etc. def foo(x,y,z): if x &#62; y: print "X is greater than Y" else: print "X &#60; Y!" return z - 1 </code></pre> That's a function called foo taking parameters x,y,z. "def" defines a function. Notice that you indent where you normally would (after the def, after the if and else). Python code is typically very readable because of this.<p>Here's an example of a class in Python (I assume you're familiar with classes?):<p><pre><code> class Foo: def __init__(self, x, y): """The __init__ function is basically like a constructor. This thing here is a docstring, it's used to document a function or class.""" self.x = x self.y = y def sum(self): # The pointer to the instance is the first parameter. # It's usually called 'self', but the name can be anything. return self.x + self.y #note: self.&#60;whatever&#62; for member variables def __str__(self): """This function is used to make a human readable representation.""" # there are various ways to do this, but % should be familiar to # programmers of C-like languages. (it's like printf) return "Foo(%d, %d)" % (self.x, self.y) foo = Foo(1,2) # note: Python is case-sensitive print foo # calls foo.__str__() and displays the result -- "Foo(1, 2)" print foo.sum() # prints 3 </code></pre> Python allows functions to be passed to functions and returned from functions, for example:<p><pre><code> def makeAFunctionThatAddsXtoY(x): def myNewFunction(y): return x + y # x gets stored in the new function for later use return myNewFunction f = makeAFunctionThatAddsXtoY(5) # f is a function print f(10) # prints 15, as expected </code></pre> Some people care about this ability more than other people. Python's an easy way to start learning about functional programming if you're not familiar with it yet.<p>There's also some other fairly handy things to learn, like list comprehensions:<p><pre><code> foo = [3,1,6,206,24,1230,0,1,112,30, 96] bar = [x for x in foo if x % 2 == 0] # all even numbers in foo baz = [3*x for x in foo if x % 3 == 0] # triple everything divisible by 3 </code></pre> You can access list elements with familiar array syntax:<p><pre><code> foo[0] == 3 # evaluates to True (note: True and False are capitalized!) </code></pre> You can also take "slices" of lists to get a range of elements:<p><pre><code> foo[0:2] == [3,1] # this statement also evaluates to True </code></pre> You can get a description of most functions and classes by typing help(name_of_thing_you_want_to_know_about) into the Python interpreter.<p>The best way to learn Python is to read the tutorial (<a href="http://docs.python.org/tutorial/" rel="nofollow">http://docs.python.org/tutorial/</a>) and play around with it. Have fun!
评论 #2011481 未加载
mickeyckmover 14 years ago
just learn web framework django(<a href="http://docs.djangoproject.com/en/1.2/intro/overview/" rel="nofollow">http://docs.djangoproject.com/en/1.2/intro/overview/</a>) and use diveintopython(<a href="http://diveintopython.org/" rel="nofollow">http://diveintopython.org/</a>) as reference :)<p>all the best.
bradleylandover 14 years ago
For someone already familiar with any programming, Zed Shaw's 'Learn Python the Hard Way' is an excellent resource. If you have the discipline to hammer through it, I believe it is the fastest way to come up to speed with Python.<p><a href="http://learnpythonthehardway.org/" rel="nofollow">http://learnpythonthehardway.org/</a><p>As someone with a music background, I found this book interesting, as it follows a similar structure to the way you learn an instrument. When learning an instrument, you don't spend days reviewing music theory, you learn the basic notes, then some scales. Then you do that over and over and over until you're proficient. Then you start playing music. LPtHW reminds me of that approach, and it worked really well for me.
评论 #2011485 未加载
runjakeover 14 years ago
Use search (bottom of page) to get more info. This question or one close to it gets asked almost weekly and there's a wealth of pointers.
tswicegoodover 14 years ago
Dive into Python and Python the Hard Way are both really good books that are available online to read through. Python.org provides some good docs too along with a pretty decent tutorial.<p>I assume since you're coming from PHP you're going to want to do some web stuff with it. Django has excellent docs and there's DjangoBook.com as well.
akxover 14 years ago
If you want to do Python web dev with something that's reasonably close to PHP (that is, not a lot of handwavey magic stuff or wiring), I heartily recommend Flask, <a href="http://flask.pocoo.org" rel="nofollow">http://flask.pocoo.org</a>
phamiltonover 14 years ago
The biggest thing I think for a php developer is understanding the MVC concept behind frameworks (and most well designed software). Php is often quick and dirty scripts, and that doesn't translate very well.
评论 #1974746 未加载
mtrnover 14 years ago
<a href="http://stackoverflow.com/questions/1908250/how-to-become-a-good-python-coder/1908266#1908266" rel="nofollow">http://stackoverflow.com/questions/1908250/how-to-become-a-g...</a>
zitstifover 14 years ago
Come up with a project that you want to program and then develop it in python.