background preloader

Python Tutorial

Facebook Twitter

Python Object-oriented Programming. In this chapter we’ll take a look at some of the basic features Python offers for object-oriented programming.

Python Object-oriented Programming

Python Tutorial. Improve Your Python: 'yield' and Generators Explained. Prior to beginning tutoring sessions, I ask new students to fill out a brief self-assessment where they rate their understanding of various Python concepts.

Improve Your Python: 'yield' and Generators Explained

Some topics ("control flow with if/else" or "defining and using functions") are understood by a majority of students before ever beginning tutoring. There are a handful of topics, however, that almost all students report having no knowledge or very limited understanding of. Of these, "generators and the yield keyword" is one of the biggest culprits.

Understanding Python's "with" statement. Fredrik Lundh | October 2006 | Originally posted to online.effbot.org Judging from comp.lang.python and other forums, Python 2.5’s new with statement (dead link) seems to be a bit confusing even for experienced Python programmers.

Understanding Python's "with" statement

As most other things in Python, the with statement is actually very simple, once you understand the problem it’s trying to solve. Consider this piece of code: set things up try: do something finally: tear things down Here, “set things up” could be opening a file, or acquiring some sort of external resource, and “tear things down” would then be closing the file, or releasing or removing the resource. If you do this a lot, it would be quite convenient if you could put the “set things up” and “tear things down” code in a library function, to make it easy to reuse.

Def controlled_execution(callback): set things up try: callback(thing) finally: tear things down def my_function(thing): do something controlled_execution(my_function) This wasn’t very difficult, was it? The Python “with” Statement by Example. Python’s with statement was first introduced five years ago, in Python 2.5.

The Python “with” Statement by Example

It’s handy when you have two related operations which you’d like to execute as a pair, with a block of code in between. The classic example is opening a file, manipulating the file, then closing it: with open('output.txt', 'w') as f: f.write('Hi there! ') The above with statement will automatically close the file after the nested block of code. (Continue reading to see exactly how the close occurs.) Decorators I: Introduction to Python Decorators. Computing ThoughtsDecorators I: Introduction to Python Decoratorsby Bruce EckelOctober 18, 2008 Summary This amazing feature appeared in the language almost apologetically and with concern that it might not be that useful. I predict that in time it will be seen as one of the more powerful features in the language. The problem is that all the introductions to decorators that I have seen have been rather confusing, so I will try to rectify that here.

(This series of articles will be incorporated into the open-source book Python 3 Patterns & Idioms). First, you need to understand that the word "decorator" was used with some trepidation, because there was concern that it would be completely confused with the Decorator pattern from the Design Patterns book. Indeed, you can use Python decorators to implement the Decorator pattern, but that's an extremely limited use of it.

The macro has a long history, but most people will probably have had experience with C preprocessor macros. The output is: Python - Official Website. 18.1.11. email: Examples. Here are a few examples of how to use the email package to read, write, and send simple email messages, as well as more complex MIME messages.

18.1.11. email: Examples

First, let’s see how to create and send a simple text message: # Import smtplib for the actual sending function import smtplib # Import the email modules we'll need from email.mime.text import MIMEText # Open a plain text file for reading. For this example, assume that # the text file contains only ASCII characters. fp = open ( textfile , 'rb' ) # Create a text/plain message msg = MIMEText ( fp . read ()) fp . close () # me == the sender's email address # you == the recipient's email address msg [ 'Subject' ] = 'The contents of %s ' % textfile msg [ 'From' ] = me msg [ 'To' ] = you # Send the message via our own SMTP server, but don't include the # envelope header. s = smtplib .

Python v3.1.3 documentation. Documentation — feedparser 5.1.3 documentation. Descriptor HowTo Guide — Python v2.7.5 documentation. Abstract Defines descriptors, summarizes the protocol, and shows how descriptors are called.

Descriptor HowTo Guide — Python v2.7.5 documentation

Examines a custom descriptor and several built-in python descriptors including functions, properties, static methods, and class methods. Shows how each works by giving a pure Python equivalent and a sample application. Learning about descriptors not only provides access to a larger toolset, it creates a deeper understanding of how Python works and an appreciation for the elegance of its design. Definition and Introduction In general, a descriptor is an object attribute with “binding behavior”, one whose attribute access has been overridden by methods in the descriptor protocol. The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. Descriptors are a powerful, general purpose protocol. Python IF...ELIF...ELSE Statements. An else statement can be combined with an if statement.

Python IF...ELIF...ELSE Statements

An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a false value. The else statement is an optional statement and there could be at most only one else statement following if . Syntax: The syntax of the if...else statement is: if expression: statement(s)else: statement(s) Flow Diagram: Example: #! When the above code is executed, it produces the following result: 1 - Got a true expression value 100 2 - Got a false expression value 0 Good bye! The elif Statement The elif statement allows you to check multiple expressions for truth value and execute a block of code as soon as one of the conditions evaluates to true. Like the else, the elif statement is optional.

The syntax of the if...elif statement is: Online Python Tutor - Visualize program execution. Write your Python code here: x = [1, 2, 3] y = [4, 5, 6] z = y y = x x = z x = [1, 2, 3] # a different [1, 2, 3] list!

Online Python Tutor - Visualize program execution