PROGRAMMING

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

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

Some common programming problems


- Syntactic errors
- Static semantic errors
- Different meaning than what the programmer intended (program crashes, stops running,
program runs forever, program gives an answer but it is different than expected)

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

o Non-scalar (have internal structure that can be assessed)

Type conversions (cast)


- Can convert object of one type to another
o Float (3) converts integer 3 to float 3.0
o Int (3.9) truncates float 3.9 to integer 3

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>

Operators on ints and float


- I+j the sum
- I-I the difference. If both are ints, result is int. If either or both are floats, result is float
- I*j the product
- i/j division result is float
- i%j the remainder when I is divided by j
- i**j I to the power of j

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

Binding variables and values


pi = 3.1416
pi_approx = 22/7
- Equal sing is an assignment of a value to a variable name
- The value is then stored in the computer memory
- Retrieve value associated with name or variable by invoking the name, by typing pi

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

- Variables are storage containers for numbers, strings, etc


- The equal sign (=) is used to assign a value to a variable:
o Codon = ‘atg’
o Dna_sequence= “gtcgcctaaccgtatatttttccccgt’

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

Strings can be viewed as objects. Object-oriented programming is programming language model


organized around objects rather than actions. An object has many attributs.

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

There are two ways to sort lists:


1. Uses the sorted ( ) buit in function: It does not modify the elements in the list
- myList=[3,31,123,1,5]
sorted(myList)
2. myList.sort( ) Modifies the elements in the list
myList
[1,3,5,31,123]
Tuples
A tuple consists of a number of values separated by commas, and is another standard sequence
data type, like strings and lists. Tuples are used with heterogeneous elements.
- t=1,2,3

Sets
Is an unordered collection with no duplicate elements. Set objects support mathematical
operations like union, intersection, and difference

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy