PROGRAMMING
PROGRAMMING
PROGRAMMING
Types of knowledge
- Declarative knowledge (statements of fact)
- Imperative knowledge (instruction on how to)
Semantics is the meaning associated with a syntactically correct strung of symbols with no static
semantic errors
Phyton programs
- A program is a sequence of definitions and commands
o Definitions evaluated
o Commands executed by python interpreter in a shell
- Commands (statements) instruct interpreter to do something
o Can be typed directly in a shell or stored in a file that is read into the shell and
evaluated
Objects
- Programs manipulate data objects
- Objects have a type that defines the kinds of things programs can do to them
o Scalar (cannot be subdivided)
Int – represent integers, ex. 5
Float – represent real numbers, ex 3.27
Bool – represent Boolean values true and false
Nonetype – special and has one value, none
Use “type ()” to see the type of an object
Printing to console
- To show output from code to a user, use print command
o In (11): 3+2
Out (11): 5
o In (12): print (3+2)
5
Expressions
- Combine objects and operators to form expression
- An expression has a value, which has a type
- Syntax for a simple expression
<object> <operator> <objejct>
Simple operations
- Parentheses are use to tell python to do these operations first
- Operator precedence without parentheses
o **
o *
o /
o + and – executed left to right, as appear in expression
Abstracting expressions
- Giving names to value of expressing allow to reuse those names instead of the values. It
ease coding
Pi = 3.1416
radius = 2.2
Area = pi*(radius**2)
Escape Characters
\n Newline
\t Tab
\\ Backslash
\” Double quote
Variables
String operators
- in membership: true if first string exists inside second given string
- not in: non-membership: true if first string does not exist in second string
- (x) indexing: gives the character at position x in a string
- (x:y) slicing: gives a substring between positions x and y in a string
- Variable(0) the position in the string is called its index and the first character has index is
0 (fun thing is not 1)
- Variable(-1) indices may also be negative numbers, which start counting from the right,
beginning at -1
- Variable(0:3) the starts is always included, and the end always excluded
- Variable(:3) an omitted first index defaults to zero
- Variable(2:) an omitted second index defaults to size of the whole string being sliced
- Print: show answer to the user
- Len ( ) returns the length of a string
- Type ( ) will tell me what type of number is in the line
- Help( ) find more information about any built-in function by using the pydoc command
in a shell or the help function of the interpreter
Variables can also perform specific actions (functions), called methods that only they can
perform!
- dna.count (‘c’): the . (dot) operator can be interpreted as “ask object dna to do
something” such as: count how many ‘c’ characters it contains.
- Dna.upper ( ) will convert all the string to capital letters. It also work for lower case
dna.lower ( )
- Dna.find (‘ag’) will tell me what position is ag at
- Dna.find (‘ag’,17) we need to tell the method where to start looking
- Dna.rfind(‘ag’) same as find ( ), but search backwards in string
- Dna.is () will retrieve a true or false answer. Ex. Dnaisupper ( ), dnaislower ( )
- Dna.replace(‘a’,’A’)
- Dna=input(“please enter a sequence”) ask the user to type something
Some conversion functions
- Int(x(,base)) converts x to an integer
- Float(x) converts x to a floating-point (real) number
- Complex(real(,imag)) create a complex number
- Str(x) converts x to a string
- Chr(x) converts an integer to a character
LISTS
A list is an ordered set of values. To create a list the values need to be enclosed by square
brackets!
Values don’t need to be the same type
- Gene_expression=(‘gene’,5.16e-0.8, 0.000128511, 733e-08)
To access individual list elements:
- Print(gene_expression[2])
- Print(gene_expression[-1])
- Don’t change an elelment in a string: In Phyton strings are immutable data types, while
lists are mutable data types
- Delgene_expression[1]: can be used to remove elements and slices from a list
destructively
- Gene_expression.reverse( ): will return the list of elements in reverse order
Lists as stacks
The lists methods append and pop make it very easy as a stack, where the last element added is
the first element retrieved (“last-in, first-out”)
To make the list in the stack use [ ]
- Stack=[‘a’, ‘b’, ‘c’, ‘d’]
To add a new element to the stack use ( )
- Stack.append(‘e’)
- Elem=stack.pop( ) retrieve an item from the top of the stack
Sorting lists
Sets
Is an unordered collection with no duplicate elements. Set objects support mathematical
operations like union, intersection, and difference