Skip to main content

Posts

Installation and documentation python

Installation and documentation python        If you use Mac OS X or Linux, then Python should already be installed on your computer by default. If not, you can download the latest version by visiting the Python home page, at http://www.python.org where you will also find loads of documentation and other useful information. Windows users can also download Python at this website. Don’t forget this website; it is your first point of reference for all things Python. You will find there, for example, reference [1], the excellent Python Tutorial by Guido van Rossum. You may find it useful to read along in the Tutorial as a supplement to this document.

Loading commands from the library in python

Loading commands from the library in python Python has a very extensive library of commands, documented in the Python Library Reference Manual [2]. These commands are organized into modules. One of the available modules is especially useful for us: the math module. Let’s see how it may be used. >>> from math import sqrt , exp >>> exp ( -1) 0.36787944117144233 >>> sqrt (2) 1.4142135623730951 We first import √ the sqrt and exp functions from the math module, then use them to compute −1 e = 1/e and 2. Once we have loaded a function from a module, it is available for the rest of that session. When we start a new session, we have to reload the function if we need it. Note that we could have loaded both functions sqrt and exp by using a wildcard *: >>> from math import * which tells Python to import all the functions in the math module. What would have happened if we forgot to import a needed function? After starting a new session, if we type >...

Operators in python

Operators in python The common binary operators for arithmetic are + for addition, - for subtraction, * for multi- plication, and / for division. As already mentioned, Python uses ** for exponentiation. Integer division is performed so that the result is always another integer (the integer quotient): 11>>> 25/3 8 >>> 5/2 2 This is a wrinkle that you will always have to keep in mind when working with Python. To get a more accurate answer, use the float type: >>> 25.0/3 8.3333333333333339 >>> 5/2.0 2.5 If just one of the operands is of type float, then the result will be of type float. Here is another example of this pitfall: >>> 2 * * ( 1 / 2 ) 1 where we wanted to compute the square root of 2 as the 12 power of 2, but the division in the exponent produced a result of 0 because of integer division. A correct way to do this computation is: >>> 2**0.5 1.4142135623730951 Another useful operator is %, which is read as ”mod”. ...

Strings in python

Strings in python A string in Python is a sequence of characters. In some sense strings are similar to lists, however, there are important differences. One major difference is that Python strings are immutable, mean- ing that we are not allowed to change individual parts of them as we could for a list. So if x is an existing string, then x[i] gets the character at position i, but we are not allowed to reassign that character, as in x[5] = ’s’. >>> x = ’ g o b b l e t y g o o k ’ >>> x [2] ’b ’ >>> x [5] ’e ’ >>> x [5] = ’s ’ T r a c e b a c k ( most recent call last ): File " < stdin > " , line 1 , in < module > T y p e E r r o r : ’ str ’ object does not s u p p o r t item a s s i g n m e n t Just as for lists, string items are indexed starting at 0. Slicing for strings works exactly the same as for lists. The length function len is the same as for lists, and concatenation is the same too. But 19the list methods appen...

Loops in python

Loops in python Python provides two looping commands: for and while. These are compound commands. for loop The syntax of a for loop is for i t e m in l i s t : action As usual, the action consists of one or more statements, all at the same indentation level. These statements are also known as the body of the loop. The item is a variable name, and list is a list. Execution of the for loop works by setting the variable successively to each item in the list, and then executing the body each time. Here is a simple example (the comma at the end of the print makes all printing occur on the same line): for i in [2 , 4 , 6 , 0]: print i , This produces the output 2 4 6 0 while loop The syntax of the while loop is while c o n d i t i o n : action Of course, the action may consist of one or more statements all at the same indentation level. The statements in the action are known as the body of the loop. Execution of the loop works as follows. First the condition is evaluated. If True, ...

Decisions in python

Decisions by python  The if–else is used to make choices in Python code. This is a compound statement. The simplest form is if c o n d i t i o n : a c t i o n −1 13else : a c t i o n −2 The indentation is required. Note that the else and its action are optional. The actions action-1 and action-2 may consist of many statements; they must all be indented the same amount. The condition is an expression which evaluates to True or False. Of course, if the condition evaluates to True then action-1 is executed, otherwise action-2 is exe- cuted. In either case execution continues with the statement after the if-else. For example, the code x = 1 if x > 0: print " Friday is w o n d e r f u l" else : print " Monday sucks " print " Have a good w e e k e n d" results in the output Friday is w o n d e r f u l Have a good w e e k e n d Note that the last print statement is not part of the if-else statement (because it isn’t indented), so if we change the first...

Variables and assignment in python

Variables and assignment in python An assignment statement in Python has the form variable = expression. This has the following effect. First the expression on the right hand side is evaluated, then the result is assigned to the variable. After the assignment, the variable becomes a name for the result. The variable retains the same value until another value is assigned, in which case the previous value is lost. Executing the assignment produces no output; its purpose it to make the association between the variable and its value. >>> x = 2+2 >>> print x 4 In the example above, the assignment statement sets x to 4, producing no output. If we want to see the value of x, we must print it. If we execute another assignment to x, then the previous value is lost. >>> x = 380.5 >>> print x 380.5 >>> y = 2* x >>> print y 761.0 Remember: A single = is used for assignment, the double == is used to test for equality. In mathematics t...