A Byte of Python

0
0
118
2 months ago
Preview
Full text

Python

Choose your Python version: If you want to learn the current Python 2.x, read If you want to learn the new Python 3.0, read here or [1] [2] [3]or download the PDF here download the PDF [4] You can also buy a printed hardcopy.

Introduction

  It serves as a tutorial or guide to the Python language for a beginner audience. If you are looking for a tutorial[5] on the current Python 2.x version, please download the previous revision of the book .

Who Reads 'A Byte of Python'?

Feedback From Readers This is the best beginner's tutorial I've ever seen! Thank you for your effort

  I will also start on learning java and if youcan tell me where to find a tutorial as good as yours for java that would help me a lot. [7] published on Linux.comYesterday I got through most of Byte of Python on my Nokia N800 and it's the easiest and most concise introduction to Python I have yet encountered.

Academic Courses

  This book is being used as instructional material in various educational institutions: [9] 1.  'Introduction to Programming' course at University of Leeds [13] 5.

Even NASA

  [17] The book is even used by NASA! It is being used in their Jet Propulsion Laboratory with their Deep Space Network project.

Official Recommendation

  [18] This book has been listed on the official website for Python in the Full Tutorials section, next to the official documentation.  Volunteer contributions to this original book must be under this same license and the copyright must be assigned to the main author of this book.

Read Now You can read the book online at Python_en:Table of Contents. Buy the Book

[21] A printed hardcopy of the book can be purchased for your offline reading pleasure, and to support the continued development and improvement of this book.

Download

[22] [23] If you wish to support the continued development of this book, please consider

Translations

  If you are interested in reading or contributing translations of this book to other human languages, please see Translations. com/ buybook Source: http:/ / www.

Python en:Table of Contents

   → Operators and Expressions8.  → Appendix: Revision History→ Previous → Next Source: http:/ / www.

Python en:Translations

Chinese

  If you want to help these translations, please see the list of volunteers and languages below and decide if you want to start a new translation or help in existing translation projects. It's clear and effective to lead you into a world of Python in the shortest time.

Chinese Traditional

  An exciting feature of this translation is that it also contains the executable chinese python sources side by side with the original python sources. The translation areoriginally based on simplified chinese version, and soon a lot of rewrite were made to fit the current wiki version and the quality of reading.

Italian

  Enrico Morelli (mr-dot-mlucci-at-gmail-dot-com) and Massimo Lucci (morelli-at-cerm-dot-unifi-dot-it) have volunteered to translate the book to Italian. We are impressed by the smart language used on your Book and we think this is essential for approaching the Python to new users (we are thinking abouthundred of students and researcher working on our labs).

German

  Ithink 'A Byte of Python' falls nicely between these, since it is not too long, written to the point, and at the same time verbose enough to teach a newbie. Besides this,I like the simple DocBook structure, which makes translating the text a generation the output in various formats a charm.

Norwegian (bokmål)

  Most tutorials and books are written in very technical English, so most high school graduates will not evenhave the vocabulary to understand what the tutorial is about. I hope the translation will help people who have found themself in the same situation as me (especially youngpeople), and maybe help spread interest for the language among people with less technical knowledge.

Indonesian

Polish

  Daniel (daniel-dot-mirror-at-gmail-dot-com) is translating the book to Indonesian at http:/ / python. Translation is in progress and it's main page is available here: Ukąś Pythona (http:/ / wiki.

Catalan

  Moisès Gómez - I am a developer and also a teacher of programming (normally for people without any previous experience). I selected the Catalan language assuming that others will translate it to the more widespread Spanish.

Portuguese

Romanian

  I'm more of a self-taught programmer and decided to learn a new language, Python. Although I could be the one with the first initiative, I'm just one volunteer so if you can help, please join me.

Brazilian Portuguese

French

  The translation is in progress, you can read the spanish (argentinian) translation starting by the table of contents (tabla de contenidos). Not so long after knowing Python I discovered this book and it helped me to learn the language.

Swedish

Russian and Ukranian

  Mikael Jacobsson (leochingkwake-at-gmail-dot-com) has volunteered to translate the book to Swedish. Averkiev Andrey (averkiyev-at-ukr-dot-net) has volunteered to translate the book to Russian, and perhaps Ukranian (time permitting).

Turkish

Mongolian

  Note Replace ' - a t - ' with ' @ ' , ' - d o t - ' with ' . ' and ' - u n d e r s c o r e - ' with ' _ ' in the email addresses mentioned on this page.

Python en:Preface

Who This Book Is For

  This book aims to help you learn this wonderful language and show how to get things done quickly and painlessly - in effect 'The Perfect Anti-venom to yourprogramming problems'. If you do have previous programming experience, you will be interested in the differences between Python and your favorite programming language - I have highlighted many suchdifferences.

History Lesson

  After a lot of rewrites, it has reached a stage where it has become a useful guide to learning thePython language. This book started out as my personal notes on Python and I still consider it in the same way, although I've taken a lot of effort to make it more palatable to others :)In the true spirit of open source, I have received lots of constructive suggestions, criticisms and feedback from enthusiastic readers which has helped me improve this book a lot.

Status Of The Book

  The book needs the help of its readers such as yourselves to point out any parts of the book which are not good, not comprehensible or are simply wrong. It's a constant tussle to balance this book between a beginner's needs and the tendency towards 'completeness' of information.

Official Website

  com/ notes/ Python where you can read the whole book online, download the latest versions of the book, buy a printedhard copy (http:/ / www.  Volunteer contributions to this original book must be under this same license and the copyright must be assigned to the main author of this book.

Buy the Book

Something To Think About

  If you wish to support the continued development of this book, please consider purchasing a printed copy (http:/ / www. There are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies; the other is to make it socomplicated that there are no obvious deficiencies.

Python en:Introduction Introduction Python is one of those rare languages which can claim to be both simple and powerful

  You will find that you will be pleasantly surprised on how easy it is to concentrate on the solution to the problem rather than the syntax and structure of the language you areprogramming in. Note Guido van Rossum, the creator of the Python language, named the language after theBBC show "Monty Python's Flying Circus".

Features of Python Simple

  This is one of the reasons whyPython is so good - it has been created and is constantly improved by a community who just want to see a better Python. Extensible If you need a critical piece of code to run very fast or want to have some piece of algorithm not to be open, you can code that part of your program in C or C++ andthen use it from your Python program.

Why not Perl?

Why not Ruby?

  As the name suggests, this is a humongous collection of Perl modules and it is simply mind-boggling because of its sheersize and depth - you can do virtually anything you can do with a computer using these modules. Ipersonally found it hard to grok the Ruby language, but for people who understand Ruby, they all praise the beauty of the language.

What Programmers Say

You may find it interesting to read what great hackers like ESR have to say about Python: He says that no language has made him more productive than Python. He says thatPython is perhaps the only language that focuses on making things easier for the

About Python 3

  The main reason for a major new version of Python is to remove all the small problems and nitpicks that have accumulated over the years and to make the language even more clean. If you already have a lot of Python 2.x code, then there is a utility to assist you to convert2.x to 3.x source (http:/ / docs.

For Linux and BSD users

  To test if you have Python already installed on your Linux box, open a shell program (like k o n s o l e or g n o m e - t e r m i n a l ) and enter the command p y t h o n - V as shown below. However, if you get a message like this one:$ p y t h o n - V b a s h : P y t h o n : c o m m a n d n o t f o u n dThen you don't have Python installed.

For Windows Users

  0/ and download the latest version from this website, which was 3.0 beta 1 (http:/ / www. Caution When you are given the option of unchecking any "optional" components, don't uncheck any!

DOS Prompt

For Mac OS X Users

  For Windows 2000, XP, 2003 , click on C o n t r o l P a n e l -> S y s t e m -> A d v a n c e d ->E n v i r o n m e n t V a r i a b l e s . Click on the variable named PATH in the 'System Variables' section, then select E d i t and add ; C : \ P y t h o n 3 0 to the end of what is already there.

Summary For a Linux system, you most probably already have Python installed on your system

  Otherwise, you can install it using the package management software that comes with your distribution. For a Windows system, installing Python is as easy as downloading theinstaller and double-clicking on it.

Python en:First Steps Introduction

  Now enter p r i n t ( ' H e l l o W o r l d ' ) followed by the E n t e r key. 1 5 0 0 3 2 b i t ( I n t e l ) ] o n w i n 3 2T y p e " h e l p " , " c o p y r i g h t " , " c r e d i t s " o r " l i c e n s e " f o r m o r e i n f o r m a t i o n .> > > p r i n t ( ' H e l l o W o r l d ' )H e l l o W o r l d> > >Notice that Python gives you the output of the line immediately!

Choosing An Editor

  A good editor will help you write Python programs easily, making your journey more comfortable and helps you reach your destination One of the very basic requirements is syntax highlighting where all the different parts of your Python program are colorized so that you can see your program and visualize itsrunning. In case you are willing to take the time to learn Vim or Emacs, then I highly recommend thatyou do learn to use either of them as it will be very useful for you in the long run.

Using A Source File

  / u s r / b i n / p y t h o n # F i l e n a m e : h e l l o w o r l d . p yp r i n t ( ' H e l l o W o r l d ' )Run this program by opening a shell (Linux terminal or DOS prompt) and entering the command p y t h o n h e l l o w o r l d .

How It Works

  These are called comments - anything to the right of the # symbol is a comment and is mainly useful as notes for the reader of theprogram. Note that you can always run the program on any platform by specifying the interpreter directly on the command line such as the command p y t h o n h e l l o w o r l d .

Executable Python Programs

  We can make this program available everywhere by simply copying this source file to one of the directories listed inP A T H .$ e c h o $ P A T H/ u s r / l o c a l / b i n : / u s r / b i n : / b i n : / u s r / X 1 1 R 6 / b i n : / h o m e / s w a r o o p / b i n$ c p h e l l o w o r l d . p y / h o m e / s w a r o o p / b i n / h e l l o w o r l d$ h e l l o w o r l dH e l l o W o r l dWe can display the P A T H variable using the e c h o command and prefixing the variable name by $ to indicate to the shell that we need the value of this variable.

Caution W.r.t. Python, a program or a script or software all mean the same thing. Getting Help

  For example, run h e l p ( p r i n t ) - this displays the help for the print function which is used to print things to the screen. In case you need to get help for operators like r e t u r n , then you need to put those inside quotes such as h e l p ( ' r e t u r n ' ) so that Python doesn't get confused on what we're tryingto do.

Python en:Basics

Literal Constants

  An example of a literal constant is a number like 5 , 1 . 2 5 e - 3 or a string like ' T h i s i s a s t r i n g ' or " I t ' s a s t r i n g !

Note for Experienced Programmers There is no separate 'long int' type. The default integer type can be any large value. Strings

  e n c o d e ( " a s c i i " ) . I can almost guarantee that you will be using strings in almost every Python program that you write, so pay attention to the following part on how to use strings in Python.

Single Quotes

Double Quotes

  You can specify strings using single quotes such as ' Q u o t e m e o n t h i s ' . An example is " W h a t ' s y o u r n a m e ?

Triple Quotes

Escape Sequences

  T h i s i s t h e s e c o n d l i n e ." W h a t ' s y o u r n a m e ? An example is T h i s i s t h e f i r s t l i n e \ n T h i s i s t h e s e c o n d l i n e .

Raw Strings

Strings Are Immutable

  An example is r " N e w l i n e s a r e i n d i c a t e d b y \ n " . ' is automatically converted in to " W h a t ' s y o u r n a m e ?

The format Method

  / u s r / b i n / p y t h o n # F i l e n a m e : s t r _ f o r m a t . p y a g e = 2 5 n a m e = ' S w a r o o p ' p r i n t ( ' { 0 } i s { 1 } y e a r s o l d ' . f o r m a t ( n a m e , a g e ) ) p r i n t ( ' W h y i s { 0 } p l a y i n g w i t h t h a t p y t h o n ?

W h y i s S w a r o o p p l a y i n g w i t h t h a t p y t h o n ?

  f o r m a t ( ' h e l l o ' ) # f i l l w i t h u n d e r s c o r e s ( _ ) w i t h t h e t e x t c e n t e r e d ( ^ ) t o 1 1 w i d t h ' _ _ _ h e l l o _ _ _ '> > > ' { n a m e } w r o t e { b o o k } ' . f o r m a t ( n a m e = ' S w a r o o p ' , b o o k = ' A B y t e o f P y t h o n ' )# k e y w o r d - b a s e d' S w a r o o p w r o t e A B y t e o f P y t h o n ' Details of this formatting specification is explained in the Python Enhancement Proposal No.

Variables

  Variables are exactly what the name implies - their value can vary, i.e., you can store anything using a variable. There are some rules you have to follow for naming identifiers: Note the lowercase n in the former and the uppercase N in the latter.

Data Types

Objects

  How to write Python programs Henceforth, the standard procedure to save and run a Python program is as follows: 1.  Run the interpreter with the command p y t h o n p r o g r a m .

Example: Using Variables And Literal Constants

  p y i = 5p r i n t ( i ) 1p r i n t ( i ) s = ' ' ' T h i s i s a m u l t i - l i n e s t r i n g . p y 5 T h i s i s t h e s e c o n d l i n e .

Logical And Physical Lines

  An example of a logical line is a statement like p r i n t ( ' H e l l o W o r l d ' ) - if this was on a line by itself (as you see it in an editor), then this also corresponds to a physical line. For example, i = 5p r i n t ( i ) is effectively same asi = 5 ; p r i n t ( i ) ;and the same can be written as i = 5 ; p r i n t ( i ) ;or even i = 5 ; p r i n t ( i )However, I strongly recommend that you stick to writing a single logical line in a single physical line only.

Indentation

  N o t i c e a s i n g l e s p a c e a t t h e s t a r t o f t h e l i n e p r i n t ( ' I r e p e a t , t h e v a l u e i s ' , i )When you run this, you get the following error:F i l e " w h i t e s p a c e . N o t i c e a s i n g l e s p a c e a t t h es t a r t o f t h e l i n e^I n d e n t a t i o n E r r o r : u n e x p e c t e d i n d e n tNotice that there is a single space at the beginning of the second line.

Python en:Operators and Expressions Introduction

Operators

  - number or gives thesubtraction of one number from the other F a l s e , else it returns evaluation of yx = F a l s e ; y = T r u e ; x a n d y returns F a l s e since x is False. Ifx is F a l s e , it returns T r u e .x = T r u e ; n o t x returns F a l s e .and Boolean AND x a n d y returns F a l s e if x is == Equal To Compares if the objects are equalx = 2 ; y = 2 ; x = = y returns T r u e .x = ' s t r ' ; y = ' s t R ' ; x = = y returns F a l s e .x = ' s t r ' ; y = ' s t r ' ; x = = y returns T r u e .

Shortcut for math operation and assignment

It is common to run a math operation on a variable and then assign the result of the operation back to the variable, hence there is a shortcut for such expressions:You can write: 2 ; a = a * a = 3 as: a = 2 ; a * =

Evaluation Order

  This means that in a given expression, Python will first evaluate the operators and expressions lower in the tablebefore the ones listed higher in the table. It is far better to use parentheses to group operators and operandsappropriately in order to explicitly specify the precedence.

Changing the Order Of Evaluation

  There is an additional advantage to using parentheses - it helps us to change the order of evaluation. For example, if you want addition to be evaluated before multiplication in anexpression, then you can write something like ( 2 + 3 ) * 4 .

Associativity

Expressions

  p y l e n g t h = 5 b r e a d t h = 2 a r e a = * l e n g t h b r e a d t h p r i n t ( ' A r e a i s ' , a r e a ) p r i n t ( + * ' P e r i m e t e r i s ' , 2 ( l e n g t h b r e a d t h ) ) Output: $ p y t h o n e x p r e s s i o n . Westore the result of the expression l e n g t h * b r e a d t h in the variable a r e a and then print it using the p r i n t function.

Python en:Control Flow Introduction

The if statement

  ) ' ) # N e w b l o c k e n d s h e r e e l i f g u e s s < n u m b e r : p r i n t ( ' N o , i t i s a l i t t l e h i g h e r t h a n t h a t ' ) # A n o t h e r b l o c k # Y o u c a n d o w h a t e v e r y o u w a n t i n a b l o c k . e l s e : p r i n t ( ' N o , i t i s a l i t t l e l o w e r t h a n t h a t ' ) # y o u m u s t h a v e g u e s s > n u m b e r t o r e a c h h e r ep r i n t ( ' D o n e ' ) # T h i s l a s t s t a t e m e n t i s a l w a y s e x e c u t e d , a f t e r t h e i f s t a t e m e n t i s e x e c u t e dOutput:$ p y t h o n i f .

After Python has finished executing the complete i f statement along with the associated e l i f and e l s e clauses, it moves on to the next statement in the block containing the i f

  In this case, it is the main block where execution of the program starts and the next statement is the p r i n t ( ' D o n e ' ) statement. Although this is a very simple program, I have been pointing out a lot of things that you should notice even in this simple program.

The while Statement

  p y n u m b e r = 2 3 r u n n i n g = T r u e w h i l e r u n n i n g : g u e s s = i n t ( i n p u t ( ' E n t e r a n i n t e g e r : ' ) ) i f g u e s s = = n u m b e r : p r i n t ( ' C o n g r a t u l a t i o n s , y o u g u e s s e d i t . ' ) r u n n i n g = F a l s e # t h i s c a u s e s t h e w h i l e l o o p t o s t o p e l i f g u e s s < n u m b e r : p r i n t ( ' N o , i t i s a l i t t l e h i g h e r t h a n t h a t .

We move the i n p u t and i f statements to inside the w h i l e loop and set the variable r u n n i n g to T r u e before the while loop. First, we check if the variable r u n n i n g is T r u e and

  The e l s e block is executed when the w h i l e loop condition becomes F a l s e - this may even be the first time that the condition is checked. If there is an e l s e clause for a w h i l e loop,it is always executed unless you break out of the loop with a b r e a k statement.

Note for C/C++ Programmers Remember that you can have an e l s e clause for the w h i l e loop. The for loop

  p yf o r i n i r a n g e ( 1 , 5 ) :p r i n t ( i ) e l s e : p r i n t ( ' T h e f o r l o o p i s o v e r ' )Output:$ p y t h o n f o r . What we do here is supply it two numbers and r a n g e returns a sequence of numbers The f o r loop then iterates over this range - f o r i i n r a n g e ( 1 , 5 ) is equivalent to f o r i i n [ 1 , 2 , 3 , 4 ] which is like assigning each number (or object) in the sequence to i, oneat a time, and then executing the block of statements for each value of i .

The break Statement

  p yw h i l e T r u e : s = ( i n p u t ( ' E n t e r s o m e t h i n g : ' ) ) i f s = = ' q u i t ' : b r e a k p r i n t ( ' L e n g t h o f t h e s t r i n g i s ' , l e n ( s ) ) p r i n t ( ' D o n e ' )Output:$ p y t h o n b r e a k . p yE n t e r s o m e t h i n g : P r o g r a m m i n g i s f u nL e n g t h o f t h e s t r i n g i s 1 8E n t e r s o m e t h i n g : W h e n t h e w o r k i s d o n e How It Works:In this program, we repeatedly take the user's input and print the length of each input each time.

Swaroop's Poetic Python

The continue Statement

  The input I have used here is a mini poem I have written called Swaroop's Poetic Python:P r o g r a m m i n g i s f u nW h e n t h e w o r k i s d o n e i f y o u w a n n a m a k e y o u r w o r k a l s o f u n :u s e P y t h o n ! p yw h i l e T r u e : s = i n p u t ( ' E n t e r s o m e t h i n g : ' ) i f s = = ' q u i t ' : b r e a k i fl e n ( s ) < 3 :p r i n t ( ' T o o s m a l l ' ) c o n t i n u e p r i n t ( ' I n p u t i s o f s u f f i c i e n t l e n g t h ' ) # D o o t h e r k i n d s o f p r o c e s s i n g h e r e .

E n t e r s o m e t h i n g : q u i t

  So, we use the built-in l e n function to get the length and if thelength is less than 3, we skip the rest of the statements in the block by using the c o n t i n u e statement. We have seen how to use the three control flow statements - i f , w h i l e and f o r along with their associated b r e a k and c o n t i n u e statements.

Python en:Functions Introduction

  / u s r / b i n / p y t h o n # F i l e n a m e : f u n c t i o n 1 . p yd e f s a y H e l l o ( ) : p r i n t ( ' H e l l o W o r l d !

Function Parameters

  / u s r / b i n / p y t h o n # F i l e n a m e : f u n c _ p a r a m . p yd e fp r i n t M a x ( a , b ) :i f a > b : p r i n t ( a , ' i s m a x i m u m ' ) e l i f a = = b : p r i n t ( a , ' i s e q u a l t o ' , b ) e l s e : p r i n t ( b , ' i s m a x i m u m ' ) p r i n t M a x ( 3 , 4 ) # d i r e c t l y g i v e l i t e r a l v a l u e s x = 5 y = 7 p r i n t M a x ( x , y ) # g i v e v a r i a b l e s a s a r g u m e n t sOutput: $ p y t h o n f u n c _ p a r a m .

Local Variables

  / u s r / b i n / p y t h o n # F i l e n a m e : f u n c _ l o c a l . p y x = 5 0 d e ff u n c ( x ) :p r i n t ( ' x i s ' , x ) x = 2p r i n t ( ' C h a n g e d l o c a l x t o ' , x ) f u n c ( x ) p r i n t ( ' x i s s t i l l ' , x )Output:$ p y t h o n f u n c _ l o c a l .

C h a n g e d l o c a l x t o 2 x i s s t i l l 5 0

  How It Works:In the function, the first time that we use the value of the name x , Python uses the value of the parameter declared in the function. In the last p r i n t function call, we display the value of x in the main block and confirm that it is actually unaffected.

Using The global Statement

  / u s r / b i n / p y t h o n # F i l e n a m e : f u n c _ g l o b a l . p y x = 5 0 d e ff u n c ( ) :g l o b a l x p r i n t ( ' x i s ' , x ) x = 2p r i n t ( ' C h a n g e d g l o b a l x t o ' , x ) f u n c ( ) p r i n t ( ' V a l u e o f x i s ' , x )Output:$ p y t h o n f u n c _ g l o b a l .

V a l u e o f x i s 2

Using nonlocal statement

  p yd e ff u n c _ o u t e r ( ) : x = 2p r i n t ( ' x i s ' , x ) d e ff u n c _ i n n e r ( ) : n o n l o c a l x x = 5 f u n c _ i n n e r ( ) p r i n t ( ' C h a n g e d l o c a l x t o ' , x ) f u n c _ o u t e r ( )Output:$ p y t h o n f u n c _ n o n l o c a l . p y x i s 2C h a n g e d l o c a l x t o 5How It Works:When we are inside f u n c _ i n n e r , the 'x' defined in the first line of f u n c _ o u t e r is relatively neither in local scope nor in global scope.

Default Argument Values

  / u s r / b i n / p y t h o n # F i l e n a m e : f u n c _ d e f a u l t . p yd e fs a y ( m e s s a g e , t i m e s = 1 ) : ( m e s s a g e t i m e s ) s a y ( ' H e l l o ' )s a y ( ' W o r l d ' , 5 ) Output:$ p y t h o n f u n c _ d e f a u l t .

Keyword Arguments

  If you have some functions with many parameters and you want to specify only some of them, then you can give values for such parameters by naming them - this is called keyword arguments - we use the name (keyword) instead of the position (which we have been using all along) to specify the arguments to the function. / u s r / b i n / p y t h o n # F i l e n a m e : f u n c _ k e y .

VarArgs parameters TODO

  / u s r / b i n / p y t h o n # F i l e n a m e : t o t a l . p y t o t a l ( i n i t i a l = 5 , n u m b e r s , * * k e y w o r d s ) : c o u n t = i n i t i a l f o r i n n u m b e r n u m b e r s : c o u n t + = n u m b e r f o r i n k e y k e y w o r d s : c o u n t + = k e y w o r d s [ k e y ] r e t u r n c o u n t p r i n t ( t o t a l ( 1 0 , 1 , 2 , 3 , v e g e t a b l e s = 5 0 , f r u i t s = 1 0 0 ) ) Output:$ p y t h o n t o t a l .

Keyword- only Parameters

  p y 6 6T r a c e b a c k ( m o s t r e c e n t c a l l l a s t ) :F i l e " t e s t . p y " , l i n e 1 2 , i n < m o d u l e > p r i n t ( t o t a l ( 1 0 , 1 , 2 , 3 ) )T y p e E r r o r : t o t a l ( ) n e e d s k e y w o r d - o n l y a r g u m e n t v e g e t a b l e sHow It Works:Declaring parameters after a starred parameter results in keyword-only arguments.

The return Statement

  p yd e fm a x i m u m ( x , y ) :i f x > y : r e t u r n x e l s e : r e t u r n y p r i n t ( m a x i m u m ( 2 , 3 ) ) Output:$ p y t h o n f u n c _ r e t u r n . You can see this by running p r i n t ( s o m e F u n c t i o n ( ) )where the function s o m e F u n c t i o n does not use the r e t u r n statement such as: d e fs o m e F u n c t i o n ( ) :p a s s The p a s s statement is used in Python to indicate an empty block of statements.

DocStrings

  p yd e fp r i n t M a x ( x , y ) : ' ' ' P r i n t s t h e m a x i m u m o f t w o n u m b e r s . ' ' ' x = i n t ( x ) # c o n v e r t t o i n t e g e r s , i f p o s s i b l e y = i n t ( y ) i f x > y : p r i n t ( x , ' i s m a x i m u m ' ) e l s e : p r i n t ( y , ' i s m a x i m u m ' ) p r i n t M a x ( 3 , 5 )p r i n t ( p r i n t M a x .

T h e t w o v a l u e s m u s t b e i n t e g e r s

  The convention followed for a docstring is a multi-line string where the first line starts with a capital letter and ends with a dot. We can access the docstring of the p r i n t M a x function using the _ _ d o c _ _ (notice the double underscores) attribute (name belonging to) of the function.

Annotations

  Functions have another advanced feature called annotations which are a nifty way of attaching additional information for each of the parameters as well as the return value. Since the Python language itself does not interpret these annotations in any way (that functionality is left to third-party libraries to interpret in any way they want), we will skipthis feature in our discussion.

Python en:Modules Introduction

There are various methods of writing modules, but the simplest way is to create a file with a . p y extension that contains functions and variables

  p yi m p o r t s y s p r i n t ( ' T h e c o m m a n d l i n e a r g u m e n t s a r e : ' ) f o r i n i s y s . z i p ' ,' C : \ \ P y t h o n 3 0 \ \ D L L s ' , ' C : \ \ P y t h o n 3 0 \ \ l i b ' ,' C : \ \ P y t h o n 3 0 \ \ l i b \ \ p l a t - w i n ' , ' C : \ \ P y t h o n 3 0 ' ,' C : \ \ P y t h o n 3 0 \ \ l i b \ \ s i t e - p a c k a g e s ' ]How It Works:First, we import the s y s module using the i m p o r t statement.

The from . . . import . . . statement

  p yi f _ _ n a m e _ _ = = ' _ _ m a i n _ _ ' : p r i n t ( ' T h i s p r o g r a m i s b e i n g r u n b y i t s e l f ' ) e l s e : p r i n t ( ' I a m b e i n g i m p o r t e d f r o m a n o t h e r m o d u l e ' )Output:$ p y t h o n u s i n g _ n a m e . p yT h i s p r o g r a m i s b e i n g r u n b y i t s e l f$ p y t h o n> > > i m p o r t u s i n g _ n a m eI a m b e i n g i m p o r t e d f r o m a n o t h e r m o d u l e> > >How It Works:Every Python module has it's _ _ n a m e _ _ defined and if this is ' _ _ m a i n _ _ ' , it implies that the module is being run standalone by the user and we can take appropriate actions.

Making Your Own Modules

  p yd e fs a y h i ( ) :p r i n t ( ' H i , t h i s i s m y m o d u l e s p e a k i n g . p yf r o m m y m o d u l e i m p o r t s a y h i , _ _ v e r s i o n _ _ s a y h i ( ) p r i n t ( ' V e r s i o n ' , _ _ v e r s i o n _ _ ) You could also use: This will import all public names such as s a y h i but would not import _ _ v e r s i o n _ _ because it starts with double underscores.

The dir function

  In order to observe the d i r in action, we define a new variable a and assign it a value and then check d i r and we observe that there is an additional value in the list of the samename. For example, run d i r ( p r i n t ) to learn about the attributes of the print function, or d i r ( s t r ) for the attributes of the str class.

Packages By now, you must have started observing the hierarchy of organizing your programs

  Packages are just folders of modules with a special _ _ i n i t _ _ . The standard library that comes with Python is an example of such a set of packages and modules.

Python en:Data Structures Introduction

List

  A l i s t is a data structure that holds an ordered collection of items i.e. This is easy to imagine if you can think of a shopping list where you have a list of items to buy, except that you probably have each item on a separate line in your shopping list whereas in Python you put commas in between them.

Quick Introduction To Objects And Classes

  p y# T h i s i s m y s h o p p i n g l i s t s h o p l i s t = [ ' a p p l e ' , ' m a n g o ' , ' c a r r o t ' , ' b a n a n a ' ] p r i n t ( ' I h a v e ' , l e n ( s h o p l i s t ) , ' i t e m s t o p u r c h a s e . s o r t ( ) p r i n t ( ' S o r t e d s h o p p i n g l i s t i s ' , s h o p l i s t ) p r i n t ( ' T h e f i r s t i t e m I w i l l b u y i s ' , s h o p l i s t [ ] ) o l d i t e m = s h o p l i s t [ ] d e l s h o p l i s t [ ] p r i n t ( ' I b o u g h t t h e ' , o l d i t e m ) p r i n t ( ' M y s h o p p i n g l i s t i s n o w ' , s h o p l i s t )Output:$ p y t h o n u s i n g _ l i s t .

Tuple

  Tuples are usually used in cases where a statement or a user-defined function can safely assume that the collection of values i.e. / u s r / b i n / p y t h o n # F i l e n a m e : u s i n g _ t u p l e .

N u m b e r o f a n i m a l s i n t h e n e w z o o i s 5

  We access thethird item in n e w _ z o o by specifying n e w _ z o o [ 2 ] and we access the third item within the third item in the n e w _ z o o tuple by specifying n e w _ z o o [ 2 ] [ 2 ] . For example, p r i n t ( 1 , 2 , 3 )and p r i n t ( ( 1 , 2 , 3 ) ) mean two different things - the former prints three numbers whereas the latter prints a tuple (which contains three numbers).

Dictionary

  c o m ' } p r i n t ( " S w a r o o p ' s a d d r e s s i s " , a b [ ' S w a r o o p ' ] ) # D e l e t i n g a k e y - v a l u e p a i rd e l a b [ ' S p a m m e r ' ] p r i n t \ n\ n ( ' T h e r e a r e { 0 } c o n t a c t s i n t h e a d d r e s s - b o o k ' . c o mT h e r e a r e 3 c o n t a c t s i n t h e a d d r e s s - b o o kC o n t a c t S w a r o o p a t s w a r o o p @ s w a r o o p c h .

G u i d o ' s a d d r e s s i s g u i d o @ p y t h o n . o r g

  Next, we access each key-value pair of the dictionary using the i t e m s method of the dictionary which returns a list of tuples where each tuple contains a pair of items - the keyfollowed by the value. We retrieve this pair and assign it to the variables n a m e and a d d r e s s We can check if a key-value pair exists using the i n operator or even the h a s _ k e y method of the d i c t class.

Sequences

  Hence,s h o p l i s t [ 0 ] fetches the first item and s h o p l i s t [ 3 ] fetches the fourth item in the s h o p l i s t sequence. Therefore, s h o p l i s t [ - 1 ] refers to the last item in the sequence ands h o p l i s t [ - 2 ] fetches the second last item in the sequence.

Set

  > > > b r i = s e t ( [ ' b r a z i l ' , ' r u s s i a ' , ' i n d i a ' ] )i n > > > ' i n d i a ' b r iT r u ei n > > > ' u s a ' b r iF a l s e> > > b r i c = b r i . p yS i m p l e A s s i g n m e n t s h o p l i s t i s [ ' m a n g o ' , ' c a r r o t ' , ' b a n a n a ' ]m y l i s t i s [ ' m a n g o ' , ' c a r r o t ' , ' b a n a n a ' ]C o p y b y m a k i n g a f u l l s l i c e s h o p l i s t i s [ ' m a n g o ' , ' c a r r o t ' , ' b a n a n a ' ]m y l i s t i s [ ' c a r r o t ' , ' b a n a n a ' ]How It Works: Most of the explanation is available in the comments.

More About Strings

  s t a r t s w i t h ( ' S w a ' ) : p r i n t ( ' Y e s , t h e s t r i n g s t a r t s w i t h " S w a " ' ) i f i n' a ' n a m e :p r i n t ( ' Y e s , i t c o n t a i n s t h e s t r i n g " a " ' ) i f n a m e . = - 1 : p r i n t ( ' Y e s , i t c o n t a i n s t h e s t r i n g " w a r " ' ) d e l i m i t e r = ' _ * _ 'm y l i s t = [ ' B r a z i l ' , ' R u s s i a ' , ' I n d i a ' , ' C h i n a ' ] p r i n t ( d e l i m i t e r .

B r a z i l _ * _ R u s s i a _ * _ I n d i a _ * _ C h i n a

  The s t a r t s w i t h method is used to find out whether the string starts with the given string. The f i n d method is used to do find the position of the given string in the string or returns We have explored the various built-in data structures of Python in detail.

Python en:Problem Solving

  We have explored various parts of the Python language and now we will take a look at how all these parts fit together, by designing and writing a program which does somethinguseful. htm) and add C : \ P r o g r a m F i l e s \ G n u W i n 3 2 \ b i n to your system PATH environmentvariable, similar to what we did for recognizing the python command itself.

The Solution

  \ \ \ \ s o u r c e = [ ' " C : M y D o c u m e n t s " ' , ' C : C o d e ' ] # N o t i c e w e h a d t o u s e d o u b l e q u o t e s i n s i d e t h e s t r i n g f o r n a m e s w i t hi n s p a c e s i t . T h e b a c k u p m u s t b e s t o r e d i n a m a i n b a c k u p d i r e c t o r y\ \ t a r g e t _ d i r = ' E : B a c k u p ' # R e m e m b e r t o c h a n g e t h i s t o w h a t y o u w i l l b e u s i n g # 3 .

If the above program does not work for you, put a p r i n t ( z i p _ c o m m a n d ) just before the o s . s y s t e m call and run the program. Now copy/paste the printed zip_command to the shell

  The target directory is where storeall the backup files and this is specified in the t a r g e t _ d i r variable. For example, use ' C : \ \ D o c u m e n t s ' or r ' C : \ D o c u m e n t s ' .

Second Version

  \ \ \ \ s o u r c e = [ ' " C : M y D o c u m e n t s " ' , ' C : C o d e ' ] # N o t i c e w e h a d t o u s e d o u b l e q u o t e s i n s i d e t h e s t r i n g f o r n a m e s w i t hi n s p a c e s i t . T h e b a c k u p m u s t b e s t o r e d i n a m a i n b a c k u p d i r e c t o r y\ \ t a r g e t _ d i r = ' E : B a c k u p ' # R e m e m b e r t o c h a n g e t h i s t o w h a t y o u w i l l b e u s i n g # 3 .

Third Version

  T h e b a c k u p m u s t b e s t o r e d i n a m a i n b a c k u p d i r e c t o r y\ \ t a r g e t _ d i r = ' E : B a c k u p ' # R e m e m b e r t o c h a n g e t h i s t o w h a t y o u w i l l b e u s i n g # 3 . s t r f t i m e ( ' % Y % m ' ) # T h e c u r r e n t t i m e i s t h e n a m e o f t h e z i p a r c h i v e # T a k e a c o m m e n t f r o m t h e u s e r t o c r e a t e t h e n a m e o f t h e z i p f i l e c o m m e n t = i n p u t ( ' E n t e r a c o m m e n t - - > ' ) i fl e n ( c o m m e n t ) = = : # c h e c k i f a c o m m e n t w a s e n t e r e d e l s e : c o m m e n t .

Fourth Version

  T h e b a c k u p m u s t b e s t o r e d i n a m a i n b a c k u p d i r e c t o r y\ \ t a r g e t _ d i r = ' E : B a c k u p ' # R e m e m b e r t o c h a n g e t h i s t o w h a t y o u w i l l b e u s i n g # 3 . s t r f t i m e ( ' % H % M % S ' ) # T a k e a c o m m e n t f r o m t h e u s e r t o c r e a t e t h e n a m e o f t h e z i p f i l e c o m m e n t = i n p u t ( ' E n t e r a c o m m e n t - - > ' ) i fl e n ( c o m m e n t ) = = : # c h e c k i f a c o m m e n t w a s e n t e r e d e l s e : t a r g e t + + t o d a y o s .

S u c c e s s f u l b a c k u p t o E : \ B a c k u p \ 2 0 0 8 0 7 0 2 \ 2 0 2 8 3 9 . z i p

  We take in the user's comments using the i n p u t function and then check if theuser actually entered something by finding out the length of the input using the l e n function. However, if a comment was supplied, then this is attached to the name of the zip archive just before the .

More Refinements

  a r g v list and we canadd them to our s o u r c e list using the e x t e n d method provided by the l i s t class. s y s t e m way of creating archives and instead using the z i p f i l e or t a r f i l e built-in module to create these archives.

The Software Development Process

  Most of the time you can use procedural programming, but when writing large programs or have a problem that is better suited to this method, you can use object oriented programming techniques. An analogy is that you canhave variables of type i n t which translates to saying that variables that store integers are variables which are instances (objects) of the i n t class.

The self

  / u s r / b i n / p y t h o n # F i l e n a m e : s i m p l e s t c l a s s . p yc l a s s P e r s o n : p a s s# A n e m p t y b l o c k p = P e r s o n ( ) p r i n t ( p )Output:$ p y t h o n s i m p l e s t c l a s s .

Object Methods

  p yc l a s s P e r s o n : d e fs a y H i ( s e l f ) :p r i n t ( ' H e l l o , h o w a r e y o u ? s a y H i ( ) # T h i s s h o r t e x a m p l e c a n a l s o b e w r i t t e n a s P e r s o n ( ) .

The _ _ init_ _ method

  n a m e = n a m ed e fs a y H i ( s e l f ) :p r i n t ( ' H e l l o , m y n a m e i s ' , s e l f . s a y H i ( ) # T h i s s h o r t e x a m p l e c a n a l s o b e w r i t t e n a s P e r s o n ( ' S w a r o o p ' ) .

Class And Object Variables

  ' ' '# A c l a s s v a r i a b l e , c o u n t i n g t h e n u m b e r o f r o b o t s p o p u l a t i o n = d e f_ _ i n i t _ _ ( s e l f , n a m e ) :' ' ' I n i t i a l i z e s t h e d a t a . f o r m a t ( s e l f . n a m e ) ) # W h e n t h i s p e r s o n i s c r e a t e d , t h e r o b o t # a d d s t o t h e p o p u l a t i o n d e f_ _ d e l _ _ ( s e l f ) :' ' ' I a m d y i n g .

Inheritance

  One of the major benefits of object oriented programming is reuse of code and one of the ways this is achieved is through the inheritance mechanism. A better way would be to create a common class called S c h o o l M e m b e r and then have the teacher and student classes inherit from this class i.e.

S c h o o l M e m b e r , this is automatically reflected in the subtypes as well. For example, you can add a new ID card field for both teachers and students by simply adding it to the

  S h r i v i d y a ' , 4 0 , 3 0 0 0 0 )s = S t u d e n t ( ' S w a r o o p ' , 2 5 , 7 5 ) p r i n t ( ) # p r i n t s a b l a n k l i n e m e m b e r s = [ t , s ] f o r m e m b e r i n ( S c h o o l M e m b e r ) : ( ' S a l a r y : " { 0 : d } " ' . S h r i v i d y a )( I n i t i a l i z e d S c h o o l M e m b e r : S w a r o o p )( I n i t i a l i z e d S t u d e n t : S w a r o o p )N a m e : " M r s .

N a m e : " S w a r o o p " A g e : " 2 5 " M a r k s : " 7 5 "

  Notice that we can treat instances of T e a c h e r or S t u d e n t as just instances of the S c h o o l M e m b e r when we use the t e l l method of the S c h o o l M e m b e r class. Also, observe that the t e l l method of the subtype is called and not the t e l l method of theS c h o o l M e m b e r class.

Python en:Input Output Introduction

Input from user

  p yd e fr e v e r s e ( t e x t ) :r e t u r n t e x t [ : : - 1 ] d e fi s _ p a l i n d r o m e ( t e x t ) :r e t u r n t e x t = = r e v e r s e ( t e x t ) s o m e t h i n g = i n p u t ( ' E n t e r t e x t : ' ) i f ( i s _ p a l i n d r o m e ( s o m e t h i n g ) ) : p r i n t ( " Y e s , i t i s a p a l i n d r o m e " ) e l s e : p r i n t ( " N o , i t i s n o t a p a l i n d r o m e " )Output:$ p y t h o n u s e r _ i n p u t . p yE n t e r t e x t : m a d a mY e s , i t i s a p a l i n d r o m e$ p y t h o n u s e r _ i n p u t .

Y e s , i t i s a p a l i n d r o m e

Files

  p y\ p o e m = ' ' ' P r o g r a m m i n g i s f u n W h e n t h e w o r k i s d o n ei f y o u w a n n a m a k e y o u r w o r k a l s o f u n : u s e P y t h o n !' ' ' f = o p e n ( ' p o e m . t x t ' , ' w ' ) # o p e n f o r ' w ' r i t i n g f . t x t ' ) # i f n o m o d e i s s p e c i f i e d , ' r ' e a d m o d e i s a s s u m e db y d e f a u l t w h i l eT r u e : l i n e = f .

Pickle

  d a t a ' # t h e l i s t o f t h i n g s t o b u y s h o p l i s t = [ ' a p p l e ' , ' m a n g o ' , ' c a r r o t ' ] # W r i t e t o t h e f i l e f = o p e n ( s h o p l i s t f i l e , ' w b ' ) p i c k l e . c l o s e ( ) d e l s h o p l i s t # d e s t r o y t h e s h o p l i s t v a r i a b l e p r i n t ( s t o r e d l i s t )Output:$ p y t h o n p i c k l i n g .

Python en:Exceptions Introduction

Errors

  For example, what if you are going to read a file and the file does not exist? What if we misspelt p r i n t as P r i n t ?

Exceptions We will try to read input from the user. Press c t r l - d and see what happens

Handling Exceptions

  p yt r y : t e x t = i n p u t ( ' E n t e r s o m e t h i n g - - > ' ) e x c e p t E O F E r r o r : p r i n t ( ' W h y d i d y o u d o a n E O F o n m e ? p yE n t e r s o m e t h i n g - - > # P r e s s c t r l - dW h y d i d y o u d o a n E O F o n m e ?$ p y t h o n t r y _ e x c e p t .

Y o u e n t e r e d n o e x c e p t i o n s

How It Works:We put all the statements that might raise exceptions/errors inside the t r y block and then In the next example, we will also see how to get the exception object so that we can retrieve additional information.

Raising Exceptions

  a t l e a s t = a t l e a s tt r y : t e x t = i n p u t ( ' E n t e r s o m e t h i n g - - > ' ) i fl e n ( t e x t ) < 3 :r a i s e S h o r t I n p u t E x c e p t i o n ( l e n ( t e x t ) , 3 )# O t h e r w o r k c a n c o n t i n u e a s u s u a l h e r ee x c e p t E O F E r r o r : p r i n t ( ' W h y d i d y o u d o a n E O F o n m e ? ' ) e x c e p t a s S h o r t I n p u t E x c e p t i o n e x : p r i n t ( ' S h o r t I n p u t E x c e p t i o n : T h e i n p u t w a s { 0 } l o n g , e x p e c t e d a t \ l e a s t { 1 } '.

Try . . Finally

  p yi m p o r t t i m e t r y : f = o p e n ( ' p o e m . t x t ' ) w h i l eT r u e : # o u r u s u a l f i l e - r e a d i n g i d i o m l i n e = f . s l e e p ( 2 ) # T o m a k e s u r e i t r u n s f o r a w h i l ee x c e p t K e y b o a r d I n t e r r u p t : p r i n t ( ' !

The with statement

  p yw i t h a so p e n ( " p o e m . t x t " ) f :f o r i n l i n e f : p r i n t ( l i n e , e n d = ' ' )How It Works:The output should be same as the previous example. The difference here is that we are using the o p e n function with the w i t h statement - we leave the closing of the file to bedone automatically by w i t h o p e n .

Python en:Standard Library Introduction

  w a r n ( " N e e d P y t h o n 3 . 0 f o r t h i s p r o g r a m t o r u n " , R u n t i m e W a r n i n g ) e l s e : p r i n t ( ' P r o c e e d a s n o r m a l ' )Output: $ p y t h o n 2 . b a s i c C o n f i g ( l e v e l = l o g g i n g . D E B U G , % ( a s c t i m e ) s % ( l e v e l n a m e ) s % ( m e s s a g e ) s f o r m a t = ' : : ' , f i l e n a m e = l o g g i n g _ f i l e ,f i l e m o d e = ' w ' ,) l o g g i n g .

First, we check which operating system we are using by checking the string returned by p l a t f o r m . p l a t f o r m ( ) (for more information, see i m p o r t p l a t f o r m ; h e l p ( p l a t f o r m ) ). If it

  u p d a t e ( { ' a p p i d ' : Y A H O O _ A P P _ I D , ' q u e r y ' : q u e r y ,' r e s u l t s ' : r e s u l t s , ' s t a r t ' : s t a r t ,' o u t p u t ' : ' j s o n ' } ) r e s u l t = j s o n . u r l o p e n ( u r l ) ) i f i n' E r r o r ' r e s u l t :r a i s e Y a h o o S e a r c h E r r o r ( r e s u l t [ ' E r r o r ' ] ) r e t u r n r e s u l t [ ' R e s u l t S e t ' ] q u e r y = i n p u t ( ' W h a t d o y o u w a n t t o s e a r c h f o r ?

Python en:More Introduction

Passing tuples around

  r e t u r n (2 , ' s e c o n d e r r o r d e t a i l s ' ) . > > > e r r n u m , e r r s t r = g e t _ e r r o r _ d e t a i l s ( ) > > > e r r n u m2 > > > e r r s t r' s e c o n d e r r o r d e t a i l s ' Notice that the usage of a , b = < s o m e e x p r e s s i o n > interprets the result of the expression as a tuple with two values.

Special Methods

  For example, if you want to use the x [ k e y ] indexing operation for your class (just like you use it for lists andtuples), then all you have to do is implement the _ _ g e t i t e m _ _ ( ) method and your job is done. Name Explanation __init__(self, ...) This method is called just before the newly created object is returned for usage.__del__(self) Called just before the object is destroyed p r i n t function or when s t r ( ) is used.__str__(self) Called when we use the __lt__(self, other) Called when the less than operator (<) is used.

Single Statement Blocks

  The following example should make this clear: > > > f l a g = T r u ei f p r i n t > > > f l a g : ' Y e s ' . Y e sNotice that the single statement is used in-place and not as a separate block.

Lambda Forms

  / u s r / b i n / p y t h o n # F i l e n a m e : l a m b d a . p yd e fm a k e _ r e p e a t e r ( n ) : s : s n t w i c e = m a k e _ r e p e a t e r ( 2 )p r i n t ( t w i c e ( ' w o r d ' ) ) p r i n t ( t w i c e ( 5 ) ) Output:$ p y t h o n l a m b d a .

List Comprehension

  / u s r / b i n / p y t h o n # F i l e n a m e : l i s t _ c o m p r e h e n s i o n . p y l i s t o n e = [ 2 , 3 , 4 ]f o r i n i f l i s t t w o = [ 2 * i i l i s t o n e i > 2 ]p r i n t ( l i s t t w o )Output: $ p y t h o n l i s t _ c o m p r e h e n s i o n .

Receiving Tuples and Lists in Functions

  ' ' ' R e t u r n t h e s u m o f e a c h a r g u m e n t r a i s e d t o s p e c i f i e d p o w e r . For example, we can generate a stringcontaining Python code at runtime and then execute these statements using the e x e c statement: e x e c> > > ( ' p r i n t ( " H e l l o W o r l d " ) ' ) H e l l o W o r l dSimilarly, the e v a l function is used to evaluate valid Python expressions which are stored in a string.

The assert statement

  > > > m y l i s t = [ ' i t e m ' ]a s s e r t > > > l e n ( m y l i s t ) > = 1 > > > m y l i s t . p o p ( )' i t e m '> > > m y l i s t [ ] a s s e r t> > > l e n ( m y l i s t ) > = i n F i l e " < s t d i n > " , l i n e 1 , < m o d u l e >A s s e r t i o n E r r o r The a s s e r t statement should be used judiciously.

The repr function

  The interesting part is that you will have e v a l ( r e p r ( o b j e c t ) ) = = o b j e c t most of the time. a p p e n d ( ' i t e m ' )> > > r e p r ( i )" [ ' i t e m ' ] "> > > e v a l ( r e p r ( i ) ) [ ' i t e m ' ] > > > e v a l ( r e p r ( i ) ) = = iT r u e Basically, the r e p r function is used to obtain a printable representation of the object.

Python en:What Next

  If you have read this book thoroughly till now and practiced writing a lot of programs, then you must have become comfortable and familiar with Python. This is fairly easy if you think about it in terms of all the various stuff that we have come across till now.

Implement the replace command (http:/ / unixhelp. ed. ac. uk/ CGI/ man-cgi?replace). This command will replace one string with another in the list of

  files provided. The replace command can be as simple or as sophisticated as you wish, from simple string substitution to looking for patterns (regular expressions).

Example Code

Questions and Answers

The best way to learn a programming language is to write a lot of code and read a lot of code: html)

Tips and Tricks

Books, Papers, Tutorials, Videos

  The logical next step after this book is to read Mark Pilgrim's awesome Dive Into Python(http:/ / www. org) book which you can read fully online as well.

Discussion

News

  If you are stuck with a Python problem, and don't know whom to ask, then the comp.lang.python discussion group (http:/ / groups. If you want to learn what is the latest in the world of Python, then follow the Official Python Planet (http:/ / planet.

Installing libraries

Graphical Software

  There are lots of choices for GUI using Python: PyQt This is the Python binding for the Qt toolkit which is the foundation upon which theKDE is built. PyGTK This is the Python binding for the GTK+ toolkit which is the foundation upon whichGNOME is built.

Summary of GUI Tools

Various Implementations

  This is a research project to make it fast and easy to improve the interpreter since the interpreter itself is written in a dynamiclanguage (as opposed to static languages such as C, Java or C# in the above three implementations) Stackless Python (http:// www. We have now come to the end of this book but, as they say, this is the the beginning of the end!.

Python en:Appendix FLOSS Free/ Libre and Open Source Software (FLOSS)

  org/ wiki/ FLOSS) is based on the concept of a community, which itself is based on the concept of sharing, and particularly the sharing of knowledge. org/ vlc/ ) ] This list is just intended to give you a brief idea - there are many more excellent FLOSS out there, such as the Perl language, PHP language, Drupal content management system forwebsites, PostgreSQL database server, TORCS racing game, KDevelop IDE, Xine - the movie player, VIM editor, Quanta+ editor, Banshee audio player, GIMP image editingprogram, ...

Python en:Appendix About Colophon

Birth of the Book

  Almost all of the software that I have used in the creation of this book are free and open source software. In the first draft of this book, I had used Red Hat 9.0 Linux as the foundation of my setup and in the sixth draft, I used Fedora Core 3 Linux as the basis of my setup.

Teenage Years

  So, I switched toOpenOffice which was just excellent with the level of control it provided for formatting as well as the PDF generation, but it produced very sloppy HTML from the document. I had also written a crude lexicalanalyzer, in Python of course, which automatically provides syntax highlighting to all the program listings.

Now

About The Author

  Now I edit everything online and the readerscan directly read/edit/discuss within the wiki website. I still use Vim for editing thanks to the ViewSourceWith extension for Firefox (https:/ / addons.

Python en:Appendix Revision History

  → Previous → Back to Table of Contents Source: http:/ / www. com/ mediawiki/ index.

Python en:Appendix Changes for Python 3000

37de95ef0326293d 52 3. 0/

Dokumen baru

Aktifitas terbaru

Download (118 Halaman)
Gratis

Tags

A Whirlwind Tour Of Python Pdf Pdf Add Add A Rn Siklus Jumlah Byte Picking A Python Version A Manifesto Pdf Pdf Perbedaan Bit Dan Byte Docx Half Byte And Run Length Backpropagation Python Python Sp Fundamentals Of Python Data Structures Pdf Pdf
Show more