Python Lab: Control Statements Conditionals, Loops, Iterations Proteomics Informatics, Spring 2014 Week 4 18 th Feb, 2014

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

Chapter 4: Control Structures I (Selection)
Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Geography 465 Assignments, Conditionals, and Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Python Control of Flow.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Basic Control Structures
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Python Conditionals chapter 5
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
COMP Loop Statements Yi Hong May 21, 2015.
Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
G. Pullaiah College of Engineering and Technology
Control Flow (Python) Dr. José M. Reyes Álamo.
Making Choices with if Statements
Sequence, Selection, Iteration The IF Statement
Python: Control Structures
Arithmetic operations, decisions and looping
Pages:51-59 Section: Control1 : decisions
Introduction to Programming Using Python PART 2
Summary Two basic concepts: variables and assignments Basic types:
Computer Science Core Concepts
Nate Brunelle Today: Conditional Decision Statements
ICT Programming Lesson 3:
Understanding Conditions
Nate Brunelle Today: Conditional Decision Statements
Pages:51-59 Section: Control1 : decisions
Nate Brunelle Today: Conditional Decision Statements
REPETITION Why Repetition?
Control 9 / 25 / 19.
Presentation transcript:

Python Lab: Control Statements Conditionals, Loops, Iterations Proteomics Informatics, Spring 2014 Week 4 18 th Feb, 2014

Recap Data types (int, float, str etc.) and values (2, 4.6, “ATCG”) Variables (x = 10, y = “ATCG”) Data structures (lists, dictionaries) Operations Simple python statements and expressions

Today Control statements: – Control the flow of the program/code – Conditionals and Loops Allow building complex logic in programs

Eclipse/Pydev setup ??

‘if/else’ Conditionals Basic structure (Conditional Execution) If : (Alternative execution) if : else: Idea: – Test for condition & take appropriate action – conditional execution vs. branching Ex. x = 3 if x % 4 == 0:## (modulus operator) print “x is divisible by 4” if x < 5: print “low expression” else: print “high expression”

Expressions must evaluate to True or False Relational operators: – x == y (x equal y?) – x > y (x greater than y?) – x < y (x less than y?) – x >= y (x greater than or equal to y?) – x <= y (x less than or equal to y?) – x != y (x not equal to y?) Membership (x in y) – “PEP” in “PEPTIDE”, “PET” not in “PEPTIDE” – 2 in [1,2,3,4,5] Type-specific, built-in vs. user-defined: – “PEPTIDE”.isupper() Logical or boolean operators (and, or, not) – help build more complex logic – x == y and x != z – x > 5 or x < -5 – x > 5 and x < 10 – not (x > 5 and x < 10)

Other ‘if/else’ Structures Multiple tests (chaining) if : elif : else: Ex. x = 3 if x < 3: print “low” elif x < 7: print “medium” else: print “high” Nested conditionals if : else: Ex. disease = True x = 3 if disease == True: if x > 0 and x <= 4: print “give drug A” elif x > 7 and x <= 10: print “give drug B” else: print “no drug”

‘for’ Loops Idea: repeat action(s) for each item in a sequence or any other iterable object (Ex. str, list, dict, set etc.) Basic structure for in :

‘for’ loop: Access Pattern 1 Sequence scans Ex. numList = [1,2,3,4] sum = 0 prod = 1 for num in numList: sum = sum + num prod = prod*num print “sum: %d\tprod: %d”%(sum, prod)

‘for’ loop: Access Pattern 2 Counter/index: range function – Greater flexibility for more specialized operations Ex. numList = [1,2,3,4] sum = 0 prod = 1 for pos in range(len(numList)): sum = sum + numList[pos] prod = product*numList[pos] print “sum: %d\tprod: %d”%(sum, prod)

HW Assignment Given a peptide sequence and charge state, compute its m/z

Next Class More examples of loops Write simple functions