Python programs How can I run a program? Input and output.

Slides:



Advertisements
Similar presentations
ARDUINO CLUB Session 1: C & An Introduction to Linux.
Advertisements

Computing Science Software Design and Development SOFTWARE DESIGN AND DEVELOPMENT USING PYTHON.
Why python? Automate processes Batch programming Faster Open source Easy recognition of errors Good for data management What is python? Scripting programming.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
An Introduction to Python – Part II Dr. Nancy Warter-Perez April 21, 2005.
5.1 Previously on... PERL course (let ’ s practice some more loops)
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
An Introduction to Textual Programming
Introduction to Python
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
Python – Part 1 Python Programming Language 1. What is Python? High-level language Interpreted – easy to test and use interactively Object-oriented Open-source.
Working on exercises (a few notes first). Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Why? – Examples Speaking Computer-ise – How – What – Environment (windows) Basic Instructions – Declare – Conditional – Loop – Input Write a quiz game.
Intro Python: Variables, Indexing, Numbers, Strings.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
Dictionaries.   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing.
Chapter Three The UNIX Editors.
End of unit assessment Challenge 1 & 2. Course summary So far in this course you have learnt about and used: Syntax Output to screen (PRINT) Variables.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 4: Writing programs.
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
Working on exercises (a few notes first)‏. Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
GE3M25: Computer Programming for Biologists Python, Class 5
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Python – May 12 Recap lab Chapter 2 –operators –Strings –Lists –Control structures.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Introduction to Programming Python Lab 8: Loops 26 February PythonLab8 lecture slides.ppt Ping Brennan
Getting Started With Python Brendan Routledge
DAY 3. ADVANCED PYTHON PRACTICE SANGREA SHIM TAEYOUNG LEE.
Introduction to Python
Introduction to Programming
Chapter 2 Introduction to C++ Programming
Lesson 4 - Challenges.
(optional - but then again, all of these are optional)‏
Introduction to Python
Introduction to Python
Introduction to Programming
Think What will be the output?
Variables, Expressions, and IO
Basic operations in Matlab
Python Lesson 6 Mr. Kalmes.
Python I/O.
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Introduction to Programming
Python programming exercise
Introduction to Python
Introduction to Python
Topics Introduction to File Input and Output
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
12th Computer Science – Unit 5
Chopin’s Nocturne.
Input and Output Python3 Beginner #3.
Introduction to Programming
Text / Serial / Sequential Files
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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? It is a text file that contains Python commands or, in other words, lines of code

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

Input program in the program from the keyboard from a file from a Python module

Input from the program itself a = 3 print a

Input from the keyboard >>> a = raw_input("Type a number: ") Type a number: 3 >>> print a 3

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

a = raw_input("Type something: ") print a

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 A Python module is a text file (with the.py extension) that contains (Python) definitions/assignments Python modules can be accessed from programs using the import statement from insulin import insulin print insulin insulin = "GIVEQCCTSICSLYQLENYCNFVNQHLCGSHL\ VEALYLVCGERGFFYTPKT" Python module insulin.py Python program my_first_import.py

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

Output program to the computer screen to a text file

To the computer screen ?

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()

seq.count("A") counts the number of letters

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.

dna = "AGCTTCGA" print dna.count("A") print dna.count("C") print dna.count("T") print dna.count("G")

Loops with for The for command repeats other commands: The commands that are repeated must be indented (shifted right by four spaces). dna = "AGCTTCGA” for base in "ACTG": print dna.count(base)

dna = "AGCTTCGA” for base in "ACTG": print dna.count(base) Compare 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?

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.

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 Input_file = open(“my_file.txt”) for line in Input_file: print line

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

telomerase = open("telomerase.txt") for line in telomerase: print line

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

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 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