Conversation started Jul 3, 2011 at 18:00.
Jul 3, 2011 6:00 PM
We're going to use the pre-installed version of Python. On your Ubuntu box, this should be either 2.6 or 2.7.
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
As you can see, you don't need to declare any variables. Just assign a name to any expression. This is all you need to do
it doesn't make a difference, but if you want to have the " character inside of your string, you can write this and it won't cause an error:
A list is a collection of objects. The length of the list is arbitrary and the types of objects inside of it can be mixed:
the same thing is true for Tuples. They are exactly like lists, but you can not modify them. Once you create a tuple, it can never change
if I talk about set, I might as well mention this on the side: In python, you can have a complex number as well. The syntax for this is:
1+4j
a dictionary (dict) is a collection of key-value mappings. Just like a telephone book maps names to numbers, a dict maps keys to values:
most of those, but not all, are to do with control flow. In this section we'll cover "if", "for", "while" and "def"
if condition: do_something() elif other_condition: do_something_else() elif yet_other_condition: do_another_thing() else: do_nothing()
if "hello" in my_list: print "hello is in my list" if "hello" not in my_list: my_list.append("hello")
python comes with a bunch of built-in functions already defined for you. For example, there is a built in function called "sorted()"
in addition to the bulitins, there are a bunch of "modules" shipped with python, containing even more functions (and other stuff)
note the "." connecting the two objects. "math" is a module and sqrt is a function inside of that module
if you are running the interactive interpreter, you can check out what things belong to any object by using the "dir()" method:
>>> dir("hello")
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalp…
Jul 3, 2011 7:01 PM
@LexFridman @Gigili join here to ask questions: http://chat.stackexchange.com/rooms/707/ask-ubuntu-classroom-discussion
**question** % is the equivalent of the "mod" function in other languages?
**question** you talked about built-in functions! what is the quickest way to list them? And how do i know what modules contain what functions while coding? Like say... I want to "split" something. How do I find out what modules has split?
**question** Can u use blocks (), [], {} to block in code instead of pressing space four times? (for "if " statements) ?
**question** % is the equivalent of the "mod" function in other languages?
**question** you talked about built-in functions! what is the quickest way to list them? And how do i know what modules contain what functions while coding? Like say... I want to "split" something. How do I find out what modules has split?
**question** Can u use blocks (), [], {} to block in code instead of pressing space four times? (for "if " statements) ?
where object is the thing that your class "inherits" from. So, in this example, "Point" is a sort of object
every method (which is what you call a function when it belongs to a class) gets a first argument named "self"
@Rinzwind it's not a constructor in the traditional sense. It's merely a function that automatically runs when you create a new object. And it receives the arguments you pass in the instantiation
there is also a constructor, called
__new__()
, but you won't be using it for a long time (as in: years)
Now that we have a bunch of widgets, we want to hook them up to our code. You're going to do this a lot:
the first step, go to the Signals tab on the right and find the signal you want to handle. In our case, find the "clicked" signal for the two buttons and give them a name. Glade will suggest names for you:
the second step, add a new method to your class, with a signature like this:
def on_button1_clicked(self, widget, *data):
you'll see that we define a few arguments. the first one, self, is just the one every method recieves.
def on_button1_clicked(self, widget, *data): self.builder.get_object("label1").set_label("you've clicked ok!")
You'll not know all of the methods and things that you can use. This is what the PyGTK reference manual is for: pygtk.org/docs/pygtk/index.html
this is where you find out that "gtk.Builder" has a method "get_object(name)" that returns the object, which is, in this case a GtkLabel
similarly, the reference manuals says that every GtkLabel has a method "set_label(text)" that changes the text of the label, and so on
stefano:~$ python test.py test.py:10: RuntimeWarning: missing handler 'on_button2_clicked' self.builder.connect_signals(self)
so, if your app is complicated, you should sketch it out on a whiteboard first, then set it up in Glade, and then write the code to glue it all together
import gtk class Test (object): def __init__(self): self.builder = gtk.Builder() self.builder.add_from_file("test.glade") self.builder.connect_signals(self) def on_button1_clicked(self, widget, *data): self.builder.get_object("label1").set_label("you've clicked ok!") def on_button2_clicked(self, widget, *data): hbox = self.builder.get_object("hbox1") hbox.set_property("border-width", 32) def run(self, *args): self.builder.get_object("window1").show_all()
you will find that, for most of the stuff you'll want to write, there is already a module in the standard library
the first three bullet points deserve their own session. On the fourth: take some time to browse docs.python.org/library
when you find cool stuff there, use it to write a program. Over time, you'll get to know all of the modules and their functions and classes
when you write apps, you'll probably be asking a lot of questions such as "how do I delete a dictionary key" or "how do I change the order of a list"
If you want to run an external program, the Standard library has a module called "commands" that helps with that
Conversation ended Jul 3, 2011 at 20:17.
Introduction to Computer Programming using Python, 3rd July 2011 18:00 UTC
Jul '113
Participants
- Stefano Palazzo 88%
- Rinzwind 11%
all times are UTC