Introduction to Python Usman Roshan
Python Interpreter language: the Python interpreter executes each statement one at a time. No compilation or linking is required. Python programs are called scripts which are simple text files. To execute a script we pass it through the interpreter.
Data types Basic data types Collections Integer Float String Boolean (true or false) Collections Lists Dictionaries (hash tables) Sets And more…
Expressions Numeric operators such as +, -, *, /, ** Logical operators such as and and or String operators such as in, +, [], [:] String and numeric functions such as len, abs, max, and min.
Assignment name = value Examples: sequence = “ACCCATA” x = 5 y = x + 2
Lists Ordered collection of basic types Examples: Operations on lists Concatenate: list3 = list1 + list2 Access nth element: list1[n]
Dictionaries Dictionaries are like arrays, except that they are indexed by user defined keys instead of non-negative integers. Example h={‘A’:’T’, ‘C’:’G’} Operations on dictionaries Lookup: h[key]
Input and Output Input and output to files is done by creating a file object open(path, mode) Example: f = open(“myfile.txt”,”r”) f.readline()
Control structures Conditional statements if expression : statements1 else: statements2 Example max = 0 if(x > y): max = x max = y
Iteration for count in range(start,stop,step): for item in collection: statements for item in collection: do something with item
Exercises Write a Python program to Measure and print the average length of DNA sequences in a file Determine the number of matches and mismatches between two DNA sequences of equal length Read a matrix from a text file into a two dimensional array and determine the largest number in the matrix Select random rows in a two dimensional array and print them to a file