Thu 25th Feb 2010 - 16.00 UTC - Ubuntu Opp Dev Week Prep: Intro to Python for programmers - Rick Spencer
Before I even quit my job, I tried my hand at contributing to solving this "how do I get started" problem by writing some "chapters". They are here, but there are some wrongnesses in them. Feel free to grab them, read them, modify them, share them, etc... However, beware, there are some wrong things in them that I need to fix. For example, the code samples us one space for indents instead of 4, I didn't understand how underscores worked, or the difference between "==" and "is". I know there are more, but hey, it was a start.
- A rather better place to start is probably Dive into Python
- But the Language Reference is necessary
- As is the Library Reference
Here are my notes so far. I will go through and make code samples and such to get ready and make the session very productive.
Language Concepts
1. Indentation levels
- controls scoping instead of {}, "end", etc...
- standard is to use 4 spaces for each indentation level
- you will get used to it, and probably format you code this way anyway
- at first, many errors will be caused by this
2. Strong Dynamic Typing
- Object and sibclasses, int, float, etc...
- A variable can change type
- don't declare variables with types
- can convert a *variable* to another type, not casting and assigning
3. Types
- Numeric types
- Integer, Long Integer, Float, Complex
- Normal operands and conversion functions float(), int(), etc...
- Can mix and match them in math, will convert to use least "restrictive" type
- Strings
- lots of functions
- not difference between characters and strings
- concatenation with +, +=, etc...
- ",' are the same
- """ can included \n, ",', etc...
- None
- like Null, null, etc...
- None is an object of type NoneType
- undefined variables are not None, just undefined errors
4. Lists and Tuples
- Like an array
- Not types, can contain anything
- Tuples are immutable lists
- how to append, remove, etc...
5. Dictionaries
- Like hash tables
- "foo in bar" syntax
- how to append, remove, etc...
6. Comparisons and branching
- True and False
- if, ==, !=, <=, >=, etc....
- is, is not
- or, and
- else, elif
7. Loops
- for foo in bar:
- while loops
- for i in range(10)
8. Error Handling
- try, except
- Exception type: except Exception
- Exception instance: except Exception instance:
9. comments and doc comments
- #comment is just for folks reading code
- using """ in strategic places == doc comments, pydoc
Modules, Files, Classes
1. file overview
- shbang
- tell bash what interpreter to use
- imports section
- like #include
- import os
- from foo import bar
- code section
- if __name__ == "__main__"
- runs if module is called directly not from another module
- start your program, also useful for writing test apps
2. Code section
- class overview
- class decleration
- define superclass, call super.__init__(self)
- __init__
- member functions, what's this "self" thing?
- member variables
- in a function
- outside of functions
- can define multiple classes in a file
- can define functions variables outside classes
3. OO syntax
- instantiating object
- no private variables, really?
- convention: start with _ for members that should be private and inheritable
- start with __ for members that should be private and not heritable (munging)
Loops: for i in range(10):
ReplyDeleteYou mean "for i in xrange(10)"
Yeah the difference was weird to me too and it's no wonder they got rid of it in Python 3.
"- member functions, what's this "self" thing?"
ReplyDeletehttp://metadirc.nl/MetaBin/show/518
I hope this clears things up.
PS: This textfield is very very annoying! It won't let me write multiple spaces and it doesn't work without JavaScript. On top of that: I can't copy text!
Tanks for writing this. I wrote blog posts about "It was sad and frustrating that it was so hard to figure out what languages, libraries, and tools to use" over the last few days. Ubuntu has missed many big opportunities to build a large developer community over the years. It's quite sad.
ReplyDeleteIn fact, if you go to the Ubuntu Developer page on the Ubuntu Wiki, you don't get anything but how to get involved in packaging and some information about governance. The equivalent pages for Windows and OS X include extensive advice about how to set up your environment, what languages to use, what IDEs to use, and where the videos and tutorials are.
Quite sad, really.
Here's the full thing:
http://blog.ibeentoubuntu.com/2010/02/making-myself-clear-about-ubuntu.html
@Martijn - thanks for the pointer. Just for clarity though, that was a rhetorical question to cover with folks when I try to induct them from their current languages into Python. I'll for sure check out your link though.
ReplyDeleteCheers, Rick
I agree with Martijn, this textfield is so much of the suck. You can't move the cursor with the keyboard, and can't use home/end keys. The only way to move the cursor is by using the mouse!
ReplyDeleteOtherwise good article. Been trying to pick up a bit of Python myself to do some stuff with Bazaar.
"can convert a *variable* to another type, not casting and assigning"
ReplyDelete—that sentence does not make sense; only values/objects can be cast; values/objects can be cast in any language... it's just type conversion.
what you meant to say was probably that you can you can assign a value of a different type to a variable than what it was previously holding, but then the sentence "A variable can change type" already says it.