CS190/295 Programming in Python for Life Sciences: Lecture 2 Instructor: Xiaohui Xie University of California, Irvine.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Adapted from John Zelle’s Book Slides
Python Programming: An Introduction to Computer Science
CSC 110 Writing simple programs [Reading: chapter 2] CSC 110 C 1.
Vahé Karamian Python Programming CS-110 CHAPTER 2 Writing Simple Programs.
Introduction to C Programming
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to Python
Chapter 2 Writing Simple Programs
JavaScript, Third Edition
Introduction to C Programming
Chapter 3 Computing with Numbers
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
Computer Science 101 Introduction to Programming.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers Python Programming, 2/e1.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Chapter Introduction to Computers and Programming 1.
Lecture 2 - Variables, program execution, calculations, print() COMPSCI 101 Principles of Programming.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs.
A First Book of ANSI C Fourth Edition
Vahé Karamian Python Programming CS-110 CHAPTER 3 Computing with Numbers.
Topics Introduction Hardware and Software How Computers Store Data
Introduction to Python
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Computer Science 101 Introduction to Programming.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Input, Output, and Processing
An Object-Oriented Approach to Programming Logic and Design Chapter 1 An Overview of Computers and Logic.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
CS 127 Introduction to Computer Science. What is a computer?  “A machine that stores and manipulates information under the control of a changeable program”
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.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Python Programming Module 1 Computers and Programs Python Programming, 2/e1.
Topics Designing a Program Input, Processing, and Output
Python: Experiencing IDLE, writing simple programs
CSC201: Computer Programming
Topics Introduction Hardware and Software How Computers Store Data
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
CS190/295 Programming in Python for Life Sciences: Lecture 1
INPUT & OUTPUT scanf & printf.
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Topics Introduction Hardware and Software How Computers Store Data
CS190/295 Programming in Python for Life Sciences: Lecture 2
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
CS190/295 Programming in Python for Life Sciences: Lecture 3
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chopin’s Nocturne.
Introduction to C Programming
Class code for pythonroom.com cchsp2cs
Presentation transcript:

CS190/295 Programming in Python for Life Sciences: Lecture 2 Instructor: Xiaohui Xie University of California, Irvine

Announcements Classroom for this course will be moved to DBH 1500 starting from Jan 17 (next Tuesday). Enrollment max cap has been increased to 35 for CS295. However, due to limited resource, the enrollment cap cannot be further increased. First homework assignment will be out before 5pm, Friday. Please check the course website.

Review of the last lecture

 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

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

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

Topic 2: Writing Simple Programs

Software development process Formulate Requirements Figure out exactly what the problem to be solved is Determine Specifications Describe exactly what your program will do. What will it accomplish? What the inputs and outputs of the program? Create a Design Formulate the overall structure of the program. How will the program achieve the desired goals? Implement the Design Translate the design into a computer language and put it into the computer. Test/Debug the Program Try out your program and see if it works as expected. If there are any errors (often called bugs), then you should go back and fix them. The process of locating and fixing errors is called debugging a program. Maintain the Program Continue developing the program in response to the needs of your users. Most programs are never really finished; they keep evolving over years of use.

An example: temperature converter Input the temperature in degrees Celsius (call it celsius) Calculate fahrenheit = 9/5 * celsius + 32 Output fahrenheit

Elements of Programs: Names Names (also called identifiers): we give names to –modules (e.g., convert, chaos) –functions within modules (e.g., main) –variables (e.g., celsius, fahrenheit) Python rules on identifiers –must begin with a letter or underscore (‘_’), which may be followed by any sequence of letters, digits, or underscores. –cannot contain any spaces –the names that are part of Python, called reserved words, cannot be used as ordinary identifiers

Elements of Programs: Expressions Programs manipulate data. The fragments of code that produce or calculate new data values are called expressions. Using a variable that has not been assigned a value will result in a NameError. More complex expressions can be constructed by combining simpler expressions with operators (e.g., +, -, *, /, **) Spaces are irrelevant within an expression. Usually it’s a good idea to place some spaces in expressions to make them easier to read Use parentheses to modify the order of evaluation.

Output statements The syntax of print : These are templates for using print, using notations called meta-languages A print statement consists of the keyword print followed by zero or more expressions, which are separated by commas. The angle bracket notation (<>) is used to indicate “slots” that are filled in by other fragments of Python code. The name inside the brackets indicate what is missing; expr stands for an expression. The ellipses (“...”) indicate an indefinite series (of expressions, in this case). You don’t actually type the dots. The fourth version shows that a print statement may be optionally ended with a comma.

Output statements The semantics of print : Displays information in textual form, with expression evaluated left to right The resulting values are displayed on a single line in a left to right fashion A single blank space character is placed between the displayed values

Assignment statements: simple assignment The template for the basic assignment statement: = where variable is an identifier and expr is an expression A variable can be assigned many times. It always retain the value of the most recent assignment.

Assignment statements: assigning input The template for the assigning input: = input( ) where prompt is an expression that serves to prompt the user for input; this is almost always a string literal (i.e., some text inside of quotation marks).

Assignment statements: Simultaneous Assignment The template for simultaneous assignment:,, …, =,, …, Python evaluate all the expressions on the right-hand side and then assign these values to the corresponding variables named on the left-hand side.

Definite Loops Programmers use loops to execute a sequence of statements several times in succession. The simplest kind of loop is called a definite loop. This is a loop that will execute a definite number of times A Python for loop has this general form: –The body of the loop can be any sequence of Python statements. The start and end of the body is indicated by its indentation under the loop heading

Topic 3: Computing with Numbers

Numeric Data Types Example output:

Numeric Data Types Whole numbers are represented using the integer data type (int for short).Values of type int can be positive or negative whole numbers. Numbers that can have fractional parts are represented as floating point (or float) values. The data type of an object determines what values it can have and what operations can be performed on it. The float type only stores approximations. There is a limit to the precision, or accuracy, of the stored values. By contrast, the int type is exact.

Numeric Data Types Notice how operations on floats produce floats, and operations on ints produce ints.

Using the Math Library Python provides many other useful mathematical functions in a special math library. A library is just a module that contains some useful definitions. Example: find the roots of ax 2 +bx+c =0

Using the Math Library Python provides many other useful mathematical functions in a special math library. A library is just a module that contains some useful definitions.

Accumulating Results: Factorial In mathematics, factorial is often denoted with an exclamation (“!”). The factorial of a whole number is defined as n!=n(n-1)(n-2)…(1). This happens to be the number of distinct arrangements for n items. Given six items, we compute 6! =720 possible arrangements. Write a program that will compute the factorial of a number entered by the user. The basic outline of our program follows an Input-Process-Output pattern. Basic strategy: do repeated multiplications, use an accumulator variable + a loop structure Input number to take factorial of, n Compute factorial of n, fact Output fact Initialize the accumulator variable Loop until final result is reached update the value of accumulator variable

Accumulating Results: Factorial Initialize the accumulator variable Loop until final result is reached update the value of accumulator variable For example, suppose we want to calculate 5!=5*4*3*2*1 We define a variable and initialize it to be 1 fact = 1 for factor in [2,3,4,5] fact = fact * factor

Python range() function range(n) : produce a sequence of numbers starting with 0 and continuing up to, but not including n range(start, n) : produce a sequence of numbers starting with start and continuing up to, but not including n range(start, n, step) : produce a sequence of numbers starting with start and continuing up to, but not including n, and using step as the increment between numbers Examples: >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(5,10) [5, 6, 7, 8, 9] >>> range(5,10,3) [5, 8]

Accumulating Results: Factorial n!=n(n-1)(n-2)…(1). Write a program that will compute the factorial of a number entered by the user.

The limits of int

Handling Large Numbers: Long Ints Python provides a better solution for large, exact values in the form of a third numeric type long int. A long int is not a fixed size, but expands to accommodate whatever value it holds. To get a long int, you put an “L” suffix on a numeric literal.

Accumulating Results: Factorial n!=n(n-1)(n-2)…(1). Write a program that will compute the factorial of a number entered by the user.

Type Conversions Note that the value is truncated, not rounded when using int() or long()