CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine
Today’s Goals Course information A basic introduction to computers and programs
Course Information Lecture: TT 3:30-4:50pm in ELH 110 Office hours: TT after class Course Prerequisites: none TA: Jake Biesinger – –Office: BH 4082 Grading based on –Homework (5-8 assignments); no exams
Course Goals Introduction to computer programming using python Primary targets of audience: –Students in bio or med areas who are interested in learning a programming language –Or students in other areas who are interested in using computers to solve life science problems
References Recommended textbook: Python Programming: An Introduction to Computer Science, 2nd, John Zelle Lecture notes, references and assignments will be available from the course website (website link available from EEE)
Teaching format of the course We will alternate between regular lectures and lab sessions. Regular lectures (basic programming techniques, concepts) Lab sessions, led by Jake Biesinger. Attendance is required. This is the way you will practice and improve your programming skills!
Topics to be covered Basic programming concepts: –Intro to computers and programs –Write simple programs –Computing with numbers –Sequences: strings, lists and files –Functions –Loop structures –Classes and object-oriented design Applications in life sciences –Examples from biological and medical areas
Chapter 1: Computers and Programs
CPU (central processing unit) - the “brain” of the machine, where all the basic operations are carried out, such as adding two numbers or do logical operations Main Memory – stores programs & data. CPU can ONLY directly access info stored in the main memory, called RAM (Random Access Memory). Main memory is fast, but volatile. Secondary Memory – provides more permanent storage o Hard disk (magnetic) o Optical discs o Flash drives Input Devices – keyboard, mouse, etc Output Device – monitor, printer, etc Hardware Basics
How does CPU execute a program? The instructions that comprise the program are copied from the secondary memory to the main memory CPU start executing the program, following a process called the fetch execute cycle 1.Retrieve the first instruction from memory 2.Decode its presentation 3.Perform the appropriate action Fetch, decode, and execute the next instruction
Programming Languages A program is simply a sequence of instructions telling a computer what to do. Programming languages are special notations for expressing computations in an exact, and unambiguous way Every structure in a program language has a precise form (its syntax) and a precise meaning (its semantics) Python is one example of a programming language. Others include C++, Fortran, Java, Perl, Matlab, …
High-level vs. machine language Python, C++, Fortran, Java, and Perl are high- level computer languages, designed to be used and understood by humans. However, CPU can only understand very low-level language known as machine language
High-level vs. machine language Suppose we want the computer to add two numbers. The instructions that the CPU actually carries out might be something like this: 1.Load the number from memory location 2001 into the CPU 2.Load the number from memory location 2002 into the CPU 3.Add the two numbers in the CPU 4.Store the result into location 2003 With instructions and numbers represented in binary notations (as sequences of 0s and 1s) In a high-level language (eg. Python): c = a + b
Translate a high-level language to a machine language Programs written in a high-level language need to be translated into the machine language the computer can execute Two ways to do this: a high-level language can be either compiled or interpreted
Compiling a high-level language A compiler is a complex computer program that takes another program written in a high-level language and translates it into an equivalent program in the machine language of some computer
Interpreting a high-level language An interpreter is a program that simulates a computer that understands a high-level language. Rather than translating the source program into a machine language equivalent, the interpreter analyzes and executes the source code instruction by instruction as necessary. To run a program, both the interpreter and the source are required each time. Interpreted languages tend to have more flexible programming environment as programs can be developed and run interactively, but are generally slower than compiled languages.
Python is an interpreted language >>> is a Python prompt indicating that the interpreter is waiting for us to give a command. A complete command is called a statement Start the Python interpreter in an interactive mode
Defining a new function in the interactive mode Python allows you put a sequence of statements together to create a brand-new command called a function. o The first line tells Python that we are defining a new function called hello. o The following lines are indented to show that they are part of the hello function. o The blank line lets Python know that the definition is finished Notice that the definition did not cause anything to happen. A function is invoked by typing its name.
Defining a new function with parameters Commands can have changeable parts called parameters that are placed within the parentheses Now we can called the newly defined function with parameters:
Write Python programs Type definitions into a separate file, called module or script. Save the file on a disk Several ways to run the program: –python chaos.py –>>> import chaos # in the interactive mode As Python imports the module file, each line executes. It’s just as if we had typed them one-by-one at the interactive Python prompt. Once imported, main() can be re-invoked by typing >>> import chaos.main()
Inside a Python program comments: o any text from # through the end of a line o intended for humans, ignored by the Python defining a function called main x is variable, used to give a name to a value so that we can refer to later The statement starting with for is an example of a loop o A loop is a device that tells Python to do the same thing over and over again o The lines indented underneath the loop heading form the body of the loop x = 3.9 * x * (1-x) is an assignment statement: the value on the right-hand side is computed, and is then stored back (assigned) into the variable on the left-and side of =.