Intro to Python - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Intro to Python

Description:

Tuples can be used as keys in a dictionary - lists cannot. Conditional Statements ... lookup for cases where the key doesn't exist. Getting Help help ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 20
Provided by: karch7
Category:
Tags: intro | keys | python

less

Transcript and Presenter's Notes

Title: Intro to Python


1
Intro to Python
  • Foundations in Computational Biology II
  • Spring 09
  • TA Hannah Carter

2
Getting Started
  • Type python at the commandline
  • Commands can be typed directly at the prompt
  • To quit type ctrl-d

3
Basic syntax
  • Python uses 0 indexing
  • Comments
  • Lists
  • help(list) for more info
  • Dictionaries
  • help(dict) for more info
  • Tuples ()
  • help(tuple) for more info

4
Data Types
  • Integers x 1
  • Floats y 1.0
  • x/y will return a float
  • Strings string1 text
  • You can add text (numbers, lists, etc. must be
    converted into strings before adding)
  • string1 \t string2
  • string1 str(x)
  • string1 .join(list_of_strings)
  • Type casting
  • int()
  • float()
  • str()

5
Lists
  • list_name
  • Similar to an array
  • Can contain strings, numbers or both
  • Separate elements with commas
  • Add new elements with list_name.append()
  • Indexing
  • list_name0 will return first item
  • list_name-1 will return last item
  • list_name1-1 will return everything except the
    first and last
  • Can create nested lists
  • nested a,b,c , 1,2,3 , text

6
Dictionaries
  • dictionary_name
  • Use keys to look up values
  • Keys can be text or numbers
  • Syntax
  • dict 1item1, 2item2
  • dict1 will return the text item1
  • dict.keys() will return the list 1,2
  • dict.vals() will return the list item1,
    item2

7
Tuples
  • A tuple is an immutable list
  • It cannot be changed. You cant add elements to
    it or remove them from it
  • Tuples are faster than lists
  • Tuples are secure (write protected)
  • Tuples can be used as keys in a dictionary -
    lists cannot

8
Conditional Statements
  • ! lt gt gt lt
  • if var1 test
  • command1
  • elif var2 ! test
  • command2
  • else
  • command3
  • Test 2 conditions simultaneously using and or
    or

9
Looping
  • for, while
  • nums 1,2,3
  • for j in range(len(nums))
  • print j
  • -will print 0,1,2
  • Can also specify a distinct range to loop over
  • for j in range(1,4)
  • print j
  • -will print 1,2,3

10
Math
  • x 2
  • y 2
  • x y
  • xy
  • import math
  • math.sqrt(xy)
  • help(math)

11
Random
  • import random
  • nums 1,2,3,4,5,6,7,8,9
  • random.choice(nums)
  • random.sample(nums,4)
  • random.shuffle(nums)
  • help(random)

12
File I/O
  • filehandle file(/path/filename.txt,r)
  • text filehandle.readlines()
  • filehandle.close()
  • Use file or open command
  • r - read
  • w - write
  • a - append
  • filehandle.readline() will read one line at a time

13
Parsing Text Files
  • f file(/tmp/textfile.txt,r)
  • text f.readlines()
  • for line in text
  • z line.split()
  • if z2 CA
  • print z0
  • f.close()

14
Scripts
  • Create a file with a .py extension
  • Type the following on the first line
  • ! /usr/bin/env python
  • Write code and save
  • Open a shell. At the commandline
  • python filename.py

15
Commandline Arguments
  • Use the sys module
  • In the code
  • import sys
  • program_name sys.argv0
  • arg1 sys.argv1
  • At the command line
  • python program_name.py arg1

16
Exceptions
  • If you dont want your script to crash on an
    error, you can use try and except
  • Syntax
  • try
  • command
  • except
  • action to take if command fails
  • This is useful for dealing with user input (e.g.
    wrong
  • number of command line arguments) and dictionary
  • lookup for cases where the key doesnt exist.

17
Getting Help
  • gtgtgt help()
  • Links on Course Website (Lab page)
  • Power Point with basics
  • Python tutorial
  • Dive Into Python - free online text book

18
Good Coding Practices
  • Use self explanatory variable names
  • Comment well
  • Keep it as simple as possible
  • Try to make your programs general (i.e. avoid
    hard coding paths, etc. - use command line
    arguments instead)

19
Basic exercises
  • Generate a list of odd numbers between 1 and 1000
    then sum a random subset
  • Create a dictionary to convert 3 letter amino
    acid codes to 1 letter amino acid codes
  • Parse the pdb file posted on the Lab web page.
    Get the amino acid for each alpha carbon then use
    the dictionary to convert to the 3 letter codes
    into 1 letter codes
  • Ex ATOM 2 CA HIS A
  • Attempt the exercise posted on the lab website
Write a Comment
User Comments (0)
About PowerShow.com