A Very Brief Introduction to Python. In the interactive interpreter session we actually created a very simple Python program. We created a variable called 'x' and associated it with the value 3, the number. We then printed this value to the screen. In almost any programming language, the 'variable' is the most basic concept. Variables are the way that we can store values and work with them. We might process these values and save them to a file, or perhaps send them across the internet, but variables are the way we keep track of them. 3 Processing Raw Text. The most important source of texts is undoubtedly the Web. It's convenient to have existing text collections to explore, such as the corpora we saw in the previous chapters. However, you probably have. Useful Python Programs CodePython can be easy to pick up whether you're a first time programmer or you're experienced with other languages. The following pages are a useful first step to get on your way writing programs with Python. This blog post is those still new to programming and have probably heard about 'object-oriented programming', 'OOP', 'classes', 'inheritance/encapsulation/polymorphism', and other computer science terms but still don't get. The line of code x = 3 is a statement which means it does a job. The job it does is to assign the value 3 to the variable 'x'. Very simple 'programs' Introduction. We have python installed, now what? And it is that simple (at least for now). Python makes it easy to run single lines of code - one-liner programs. SolexaQA calculates sequence quality statistics and creates visual representations of data quality for second-generation sequencing data. Originally developed for the Illumina system (historically known as. Python Tutorial for Beginners - Learn Python in simple and easy steps starting from basic to advanced concepts with examples including Python Syntax Object Oriented Language, Methods, Tuples, Tools/Utilities, Exceptions. In Python terminology we say that it binds the name 'x' to the number three. This idea of variables being names bound to objects (or names which 'reference' objects) is important in Python. A variable name in Python can be almost anything, so long as it obeys certain rules : Just as variables are a basic concept in programming, so are the different types of values that a variable can have. We call these 'data- types', because they are different types of ways we can store data. In programming the word 'type' has a specific meaning. It is a precise way of categorising different sorts of objects. Every object has a 'type'. The most basic data types are listed below. It is often possible to convert between these different types (for example converting the text '1. Integers. An integer is a whole number. In theory any whole number from minus infinity to plus infinity. In practise the range is limited by the computer, but with Python the maximum size is limited only by the amount of memory your computer has !'Under the hood' Python actually uses two types of integer, the integer and the 'long integer' for integers bigger than a certain amount. Occasionally you may care whether a number is an integer or a long integer, but in practise it will rarely matter. Floating Point Numbers. Computers are very good at dealing with whole numbers. This is why the 'integer' is a basic data- type. Useful Python Programs TutorialA 'Floating point number' is any number that has digits after the decimal point. For example 6. 3 or - 1. You can also store your whole numbers as floating point numbers, just include a decimal point when you write it: 1. Because dealing with floating point numbers is much more complicated for computers they are much slower to work with. If you know your numbers will be integers then use the integer type. You can also write floating point numbers using scientific notation: 1. If you need to a lot of complicated maths with floating point numbers, then you may be interested in extensions like numpy or General Multiprecision Python project. These extensions provide very fast ways of doing mathematical operations. For normal use the capabilities built in to Python will be sufficient. Warning. In current versions of Python, dividing an integer by an integer will return an integer. You can get unexpected results like . To get the answer you expect, one of the numbers must be a floating point number . There is also a complex number basic datatype in Python. They aren't very common, but can be extremely useful for certain types of maths. They are written : 3i + 4j. A string is any piece of text. Python does not distinguish between a string that is a single character or a large block of text. Because of the many ways that Python provides for working with strings, it is an ideal language to do text processing with. The name 'string' probably comes from the fact that programming languages often treat text as a sequence of characters 'stringed' together. Python has two different string- types. The basic string type (which I have called 'normal strings' below) store the text as a sequence of bytes (numbers) with one byte per character. These should only really be used if you know your text will only have ASCII values in it. Unfortunately they are terribly convenient to use, so many programs just use this sort of string. They can also be used for storing binary data in. They are sometimes called byte- strings, which is quite a good description of what they do. Unicode strings store text internally using the unicode standard. They are slightly more complicated, because you must know the 'encoding' the text is stored in whenever you read the text in or save it out. In the long run this can save a great deal of confusion. Below is an overview of the two different string types, and how to include them in your programs. Normal Strings. You include a string in your program by surrounding it with single quotes or double quotes : x = 'This is a string'y = . The quotes can again be single quotes or double quotes : x=. For example : x='\n'y='Don\'t worry about quotes'z='\\'If you want to be able to include backslashes in your strings without having to escape them, you can use a 'raw string'. If you precede the string literal in your code with an r it is a raw string. This is particularly useful for regular expressions which tend to use a lot of backslashes. The backslash will be interpreted as escaping the quote that follows it, as well as being included in the string. Annoying and not very useful in my opinion, but Python can't be too perfect. For this reason basic strings are often referred to as 'byte- strings'. You can also use them to store binary data. Because they are stored as bytes, your string literals (in your program) should only really contain ASCII characters. You can include non- ASCII by declaring an encoding at the start of your program, but you should only do this if you know what you are doing. If you intend to use (or allow your users to use) non- ASCII characters in your program, then storing strings without specifying an encoding is asking for trouble. Unless you are very careful you will probably end up treating text incorrectly and garbling your data. It is far better to use unicode strings, which are explained briefly below. Unicode Strings. As mentioned above, 'normal strings' in Python are byte- strings and store data as a sequence of numbers. This can be very convenient for ASCII text, but as soon as you use non- ASCII characters is a recipe trouble. For a good 'how- to' on using Unicode with Python, read A Crash Course in Character Encodings. Unicode strings have an internal representation which keeps text as text. Effectively the computer knows which characters your text compares. You will still have to choose an encoding when you read or write your data. You can include unicode literals in your program by preceding the string with a u. Unless you specify the encoding at the start of your program then you should still only use ASCII characters, but you know your string will be stored as unicode rather than a byte string. The quoting and escaping rules for unicode strings are the same as for byte strings : x=u'A unicode string'y=u'''A multi- line unicodestring.'''y=ru. This happens when one of the strings that you are 'implicitly' converting, for example by adding a unicode string to a byte string, has characters which can't be represented in the system default encoding (which will usually be ASCII). The article mentioned above (A Crash Course in Character Encodings) explains when this can happen and how to avoid it. Python strings, like the other built- in types, have lots of useful methods for doing string- type- things with them. Refer to the string methods in the Python docs for a full list. There are a few other useful values which can be very handy. None: Often written null in other languages, it is usually used to represent 'nothing'. True: The 'boolean' value of true. False: The boolean value of false. These values turn up a great deal when programming. You can include them in your program by writing them literally : x=Noney=Truez=False. In order to work with information, you will often want to group data together and work on it in that group. Python contains lots of powerful concepts for doing this, including fancy things like iterators, generators and list comprehensions. The most fundamental way of grouping data together is to use 'data- structures'. These are objects that store your values together. With Python you are free to create your own custom data structures, but it has four very powerful ones built in. Objects that store data for you are called containers. All these four data- types (the list, the tuple, the set and the dictionary) have lots of different operations you can perform on them. Many of these are built- in to them and are called 'methods'. This introduction will not cover these, instead it will just introduce you to them and how you include them in your programs. A list stores a group of items one after the other. It keeps them in order for you. You can add or remove items as you wish, sort your list or search through it to see if it contains a specific value. You can start with an empty list and add things to it, or use a pre- populated list. Items in your list can be any object, including other lists. This means that you can have lists of lists, and use them to create complex nested structures. They are a very useful fundamental building block. A list is written using square brackets, with commas between the items : w=. The index must be an integer representing the position of item. The first item is in position zero. You can index a variable by placing square brackets after the variable with the index in. If you try to access a position that does not exist (for example the fifth item of a list with only four members), you will get an Index. Error. You can access from the end of the list, by using negative indexes. Position - 1 is the last item, - 2 is the one before the last etc.> > > x=. They also have special syntax for working on parts of a list. This is called slicing. See the page on sequence types for details. Tuples are a lot like lists in that they are used to store sequences of information. Unlike lists they can't be altered after they have been created.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |