Lists, tuples, files - Borenstein Lab
Lists, tuples, files
Genome 373
Review
? Python is object oriented, with many types of objects ? string objects represent a sequence of characters ? characters in strings can be gotten by index, e.g. myStr[3] ? substrings can be extracted by slicing, e.g. myStr[3:7] ? string objects have specific methods, e.g. myStr.find("foo") ? numbers are either type int (2 or 7) or float (2.7 or 3.1415) ? math operations on numbers retain their type, e.g. int/int -> int ? operations on mixed types give floats, e.g. int*float -> float
s = "python" i = 1 f = 2.1
creates a string object creates an int object creates a float object
Lists
? A list is an object that represents an ordered set of objects
>>> myString = "Hillary" >>> myList = ["Hillary", "Barack", "John"]
? Lists are
? ordered left to right ? indexed like strings (from 0)
creates three string objects and a list object
? mutable
? possibly heterogeneous (including containing other lists)
>>> list1 = [0, 1, 2] >>> list2 = ['A', 'B', 'C'] >>> list3 = ['D', 'E', 3, 4] >>> list4 = [list1, list2, list3] # WHAT? >>> list4 [[0, 1, 2], ['A', 'B', 'C'], ['D', 'E', 3, 4]]
Lists and strings are similar
>>> s = 'A'+'T'+'C'+'G'
>>> s 'ATCG'
>>> print s[0] A >>> print s[-1] G >>> print s[2:] CG >>> s * 3 'ATCGATCGATCG'
concatenate
index slice multiply
>>> s[9]
Traceback (most recent call last): File "", line 1, in ? IndexError: string index out of range
>>> L = ["adenine", "thymine"] + ["cytosine", "guanine"] >>> L ['adenine', 'thymine', 'cytosine', 'guanine'] >>> print L[0] adenine >>> print L[-1] guanine >>> print L[2:] ['cytosine', 'guanine'] >>> L * 3 ['adenine', 'thymine', 'cytosine', 'guanine', 'adenine', 'thymine', 'cytosine', 'guanine', 'adenine', 'thymine', 'cytosine', 'guanine'] >>> L[9] Traceback (most recent call last):
File "", line 1, in ? IndexError: list index out of range
You can think of a string as an immutable list of characters.
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- python functions file reading and writing
- python 2 7 quick reference
- all i ever needed to know about python scripting
- programming principles in python csci 503 490
- python dicts and sets inspiring innovation
- file input and output and conditionals
- lecture 5 python princeton university
- learning the pythonic way
- file input and output
- python programming scripting