Python programs
This presentation is the property of its rightful owner.
Sponsored Links
1 / 36

Python programs PowerPoint PPT Presentation


  • 68 Views
  • Uploaded on
  • Presentation posted in: General

Python programs. How can I run a program? Input and output. In this module you can learn. How to read, process, and output text How to read from the keyboard How to write to the screen How to repeat things How to create your own modules. Counting amino acids. What is a program?.

Download Presentation

Python programs

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Python programs

Python programs

How can I run a program?

Input and output


In this module you can learn

In this module you can learn

  • How to read, process, and output text

  • How to read from the keyboard

  • How to write to the screen

  • How to repeat things

  • How to create your own modules


Python programs

Counting amino acids


What is a program

What is a program?

It is a text file that contains Python commands or, in other words, lines of code


Python programs

Exercise 1

1) Open a text file, write:

print "This is the output of my first program"

save the file with the name my_print.py and exit.

Open a terminal, go to the directory where you saved my_print.py and type at the cursor:

python my_print.py


Python programs

Input

in the program

from the keyboard

program

from a file

from a Python module


Input from the program itself

Input from the program itself

a = 3

print a


Input from the keyboard

Input from the keyboard

>>> a = raw_input("Type a number: ")

Type a number: 3

>>> print a

3


Python programs

Exercise 2

2) Write a program that reads something from the keyboard and print it to the screen.


Python programs

a = raw_input("Type something: ")

print a


Input from a text file

Input from a text file

  • We need to “access” an existing input file

  • And read its content

Infile = open("insulin.txt")

content = Infile.read()

print content


From a python module

From a Python module

  • A Python module is a text file (with the .pyextension) that contains (Python) definitions/assignments

  • Python modules can be accessed from programs using the import statement

Python module insulin.py

insulin = "GIVEQCCTSICSLYQLENYCNFVNQHLCGSHL\

VEALYLVCGERGFFYTPKT"

Python program my_first_import.py

from insulin import insulin

print insulin


Python programs

Exercise 3

3) Write a program that reads a sequence from a file and print it to the screen. Run it.


Python programs

Output

to the computer screen

program

to a text file


To the computer screen

To the computer screen

?


To a text file

To a text file

  • We need to “open” a text file in the “writing” mode

  • We have to write to it.

from insulin import insulin

outfile = open("my_output.txt", "w")

outfile.write(insulin)

outfile.close()


Python programs

counts the number

of letters

seq.count("A")


Python programs

Exercise 4

  • 4) Calculate DNA base occurrences. Write a program that counts how many times the four bases occur in a DNA sequence. The program should:

  • Store the DNA sequence in a variable.

  • Count how often each base occurs.

  • Write all four numbers to the screen.

  • Test it with a DNA sequence for which you know the result, for instance “AAAACCCGGT”. This approach makes it much easier to discover small program errors.


Python programs

dna = "AGCTTCGA"

print dna.count("A")

print dna.count("C")

print dna.count("T")

print dna.count("G")


Loops with for

Loops with for

The for command repeats other commands:

dna = "AGCTTCGA”

for base in "ACTG":

print dna.count(base)

The commands that are repeated must be indented

(shifted right by four spaces).


Python programs

Compare

dna = "AGCTTCGA”

for base in "ACTG":

print dna.count(base)

Would you prefer this implementation?

dna = "AGCTTCGA"

print dna.count("A")

print dna.count("C")

print dna.count("T")

print dna.count("G")

Why or why not?


Python programs

Exercise 5

  • 5) Retrieve the 1132-residue sequence of human telomerase reverse transcriptase isoform 1 from the NCBI protein database. Choose the FASTA format. Copy the sequence to a text file (telomerase.txt). Write a program that reads the telomerase.txt file and prints first the whole sequence and then the sequence residue by residue.


Python programs

telomerase = open("telomerase.txt")

seq = telomerase.read()

print seq

for aa in seq:

print aa


You can use a for loop to read a file line by line

You can use a for loop to read a file line by line

Input_file = open(“my_file.txt”)

for line in Input_file:

print line


Python programs

Exercise 6

  • 6) Write a program that reads the telomerase.txt file and prints its content line by line.


Python programs

telomerase = open("telomerase.txt")

for line in telomerase:

print line


Python programs

Exercise 7

  • 7) Which amino acid is the most frequent in the sequence of the telomerase reverse transcriptase isoform 1?


Python programs

telomerase = open("telomerase.txt")

seq = telomerase.read()

for aa in "ACDEFGHKILMNPQRSTVYW":

aa_count = seq.count(aa)

aa_freq = aa_count/float(len(seq))

print aa, round(aa_freq, 3)


Recap

Recap


Recap i

Recap I

  • string variables contain text

  • print writes to the screen

  • you can use functions to do things

  • you can enter text with raw_input()

  • write() writes to an open file

  • for loops repeat commands

  • comments starts with # or '''


Recap ii

Recap II


  • Login