|
|
# Basic Python Programming
|
|
|
## Introduction
|
|
|
This document is designed to help users with no experience programming in Python. This document will explain some of the features that make Python unique, but is mainly intended to focus on the practicalities of working with Python.
|
|
|
|
|
|
## Data Types
|
|
|
As with many other programming languages, there are many data types.
|
|
|
|
|
|
### Integer
|
|
|
An integer is simply a whole number, i.e. a number with no fractional component. Unlike many other programming languages, there is no explicit limit to the value that be stored in a numeric value.
|
|
|
|
|
|
#### Math Operations
|
|
|
The standard math operators (`+`, `-`, `*`, and `/`) work as expected. Power operations can be performed using the notation `a**b`, which is the operation for a<sup>b</sup>. The modulus operator (`%`) is used to calculate the remainder of a division operation:
|
|
|
|
|
|
```python
|
|
|
>>> 9%5
|
|
|
4
|
|
|
```
|
|
|
|
|
|
When working with variables, operators can be applied to the variable when combined with `=`. For example:
|
|
|
```python
|
|
|
>>> a=9
|
|
|
>>> a-=5
|
|
|
>>> a
|
|
|
4
|
|
|
>>> a+=1
|
|
|
>>> a
|
|
|
5
|
|
|
>>> a*=5
|
|
|
>>> a
|
|
|
25
|
|
|
```
|
|
|
|
|
|
|
|
|
### Float
|
|
|
|
|
|
A float, is a number with a fractional component, i.e. any non-integer real number.
|
|
|
|
|
|
### String
|
|
|
|
|
|
A string is a sequence of characters. Strings are enclosed by either single (`'`) or double ( `"`) quotes.
|
|
|
|
|
|
#### String Operations
|
|
|
|
|
|
### List
|
|
|
#### List Operations
|
|
|
### Tuple
|
|
|
### Set
|
|
|
### Dictionary
|
|
|
#### Dictionary Operations
|
|
|
### Mutability
|
|
|
One concept that often proves troublesome to novice Python programmers is mutability.
|
|
|
|
|
|
## Conditional Code
|
|
|
|
|
|
### if else syntax
|
|
|
|
|
|
### Boolean Operations
|
|
|
|
|
|
## Loops
|
|
|
|
|
|
### For Loops
|
|
|
|
|
|
#### Iterable objects
|
|
|
|
|
|
### While Loops
|
|
|
|
|
|
## Functions
|
|
|
|
|
|
### Defining a Function
|
|
|
|
|
|
#### Default Arguments
|
|
|
|
|
|
### Using a Function
|
|
|
|
|
|
## Classes
|
|
|
Classes are a way of bundling data and functions together. ...
|
|
|
|
|
|
### Atributes
|
|
|
A class can have multiple attributes. For example a class that stores information about gene can have attributes for gene name, organism, and sequence.
|
|
|
|
|
|
```python
|
|
|
class Gene:
|
|
|
"""Gene
|
|
|
A model class that represents a gene
|
|
|
"""
|
|
|
def __init__(self,name='',organism='',seq=''):
|
|
|
self.name=name
|
|
|
self.organism=organism
|
|
|
self.sequence=seq
|
|
|
|
|
|
A=Gene('COX1', 'human', 'AAAAAAAA')
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### Methods
|
|
|
```python
|
|
|
class Gene:
|
|
|
"""Gene
|
|
|
A model class that represents a gene
|
|
|
"""
|
|
|
def __init__(self,name='',organism='',seq=''):
|
|
|
"""init
|
|
|
This constructor is called when a new instance
|
|
|
of the class is created.
|
|
|
"""
|
|
|
self.name=name
|
|
|
self.organism=organism
|
|
|
self.sequence=seq
|
|
|
|
|
|
def get_length(self)
|
|
|
return len(self.sequence)
|
|
|
|
|
|
A=Gene('COX1', 'human', 'AAAAAAAA')
|
|
|
a_length=A.get_length()
|
|
|
```
|
|
|
|
|
|
### Inheritence |
|
|
\ No newline at end of file |