Python 2 Conditionals and loops Matthew Egbert CS111

Similar documents
Python 2 Conditionals and loops

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Python Input, output and variables

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

Lecture 3. Input, Output and Data Types

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

ENGR 101 Engineering Design Workshop

CS 115 Lecture 4. More Python; testing software. Neil Moore

ENGR (Socolofsky) Week 02 Python scripts

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

CPSC 217 Midterm (Python 3 version)

Variable and Data Type I

Expressions and Variables

Fundamentals of Programming (Python) Getting Started with Programming

Variable and Data Type I

CS 141, Lecture 3. Please login to the Math/Programming profile, and look for IDLE (3.4 or the unnumbered. <-- fine <-- fine <-- broken

Getting Started with Python

Types, lists & functions

Mathematical Data Operators

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Fundamentals of Programming. Week 1 - Lecture 3: Loops

Module 3: Strings and Input/Output

>>> * *(25**0.16) *10*(25**0.16)

OCR Pseudocode to Python

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Name: Partner: Python Activity 9: Looping Structures: FOR Loops

CMSC201 Computer Science I for Majors

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Lecture 1. Types, Expressions, & Variables

Topic 2: Introduction to Programming

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

CSc 110, Spring 2017 Lecture 3: Expressions, Variables and Loops. Adapted from slides by Marty Stepp and Stuart Reges

from scratch A primer for scientists working with Next-Generation- Sequencing data Chapter 1 Text output and manipulation

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

CSSE 120 Introduction to Software Development Practice for Test 1 paper-and-pencil part Page 1 of 6

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Lecture 2: Variables & Assignments

Operators & Expressions

CMSC201 Computer Science I for Majors

Getting Started Values, Expressions, and Statements CS GMU

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Python as a First Programming Language Justin Stevens Giselle Serate Davidson Academy of Nevada. March 6th, 2016

CMSC201 Computer Science I for Majors

Python Programming. Introduction Part II

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

CSE 115. Introduction to Computer Science I

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

COMP Primitive and Class Types. Yi Hong May 14, 2015

Language Reference Manual

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Fundamentals: Expressions and Assignment

Lecture 1. Course Overview, Python Basics

CS313D: ADVANCED PROGRAMMING LANGUAGE

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

1/11/2010 Topic 2: Introduction to Programming 1 1

cs1114 REVIEW of details test closed laptop period

Lecture 1. Course Overview, Python Basics

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

DaMPL. Language Reference Manual. Henrique Grando

Python for Non-programmers

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

2.4 Multiplication and Division of Integers

Building Java Programs

Data Handing in Python

Algorithms and Programming I. Lecture#12 Spring 2015

Scripting Languages. Diana Trandabăț

Full file at C How to Program, 6/e Multiple Choice Test Bank

CS 105 Lab As a review of what we did last week a. What are two ways in which the Python shell is useful to us?

Not-So-Mini-Lecture 6. Modules & Scripts

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Decisions, Decisions. Testing, testing C H A P T E R 7

CMSC201 Computer Science I for Majors

Building Java Programs

Datatypes, Variables, and Operations

Introduction to Mathematical and Scientific Programming TUTORIAL WEEK 1 (MATH 1MP3) Winter 2019

CSEN 102 Introduction to Computer Science

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

To begin this textbook, we need to start with a refresher of the topics of numbers and numbering systems.

ENGR 102 Engineering Lab I - Computation

Programming in Python 3

CMSC201 Computer Science I for Majors

Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

COMPSCI 105 S Principles of Computer Science. Classes 3

JME Language Reference Manual

Monty Python and the Holy Grail (1975) BBM 101. Introduction to Programming I. Lecture #03 Introduction to Python and Programming, Control Flow

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

Introduction to Python

Information Science 1

CSCI 121: Anatomy of a Python Script

Oliver BONHAM-CARTER. Fall 2018 Week 2

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Spring Semester 11 Exam #1 Dr. Dillon. (02/15)

Fundamentals of Programming CS-110. Lecture 3

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

CMSC 201 Computer Science I for Majors

Transcription:

Python 2 Conditionals and loops Matthew Egbert CS111 Yesterday's Lecture Input and Output user_input_as_str = input('prompt: ') print('hello. You entered: '+ user_input_as_str) Comments # everything to the right of the # is ignored print('hi') # this is ignored Data types (and conversion functions) Strings, integers and floats str(), int(), and float() Assigning values to variables and (Overloaded) Operators -- *, **, %, +, /, // x = x * 2 # <-- this puts the number 4 in x y = 'y' * 2 # <-- this puts the string 'yy' in y # get input from user feet = int(input("enter feet: ")) # convert to metres feet_to_metres = 0.3048 metres = feet * feet_to_metres # print output print(str(feet)+" ft. equals "+ str(metres) +" m.") Printing multiple things out Concatenate print(str(feet)+" feet is equal to "+str(metres) +" metres.") Or use commas print(feet," feet is equal to ",metres, " metres.") Today s lecture conditional statements conditional execution. "If something is the case, then run these statements, otherwise, run those statements." loops repeated execution. "Run this sequence of statements again and again until XXX. Reminder: Please get a pencil / pen and paper (or a laptop) out, so you can work through some exercises.

IF statement Conditional Statements If... Conditional activity (ie. if this then do that ) is an important part of many programs The if statement lets you introduce conditional activity into your program Syntax if [logical condition]: Statements that are executed when if is true must be bed underneath the if statement. In python, white space is important! Incorrect whitespace can cause syntax errors. Comparison operators The following will evaluate to true or false: if [logical condition]: Logical operators You can combine comparisons using the following operators Meaning Operator Example Less than < a < b Less than or equal to <= a <= b Greater than > a > b Greater than or equal to >= a >= b Equal to == a == b Not equal to!= a!= b and If the test in a and b evaluate to true, then the logical condition will be true, eg: if x > 1 and y < 2: or If either a or b evaluate to true, then the overall logical condition will be true, eg: if x > 1 or y < 2:

Logical operators not Inverts True into False and False into True. eg: if not(x): Exercise: Logical and Boolean Operators [Program A] print( 5 > 4 ) [Program B] print( 5 == 4 ) [Program C] print( 5 > 4 and False ) [Program D] print( x <= 5 and x >= 0 ) [Program E] x = 5 print( x!= 3 ) [Program F] x = 4 print( x==0 or x==1 ) print( True or False ) [Program G] print( not(x) or x ) print( not(x) and x ) IF statement example Write a program that asks the user to enter a number between 1 and 10 (inclusive). The program will print out "Correct" if the number is in the range and "Incorrect" if the number is outside the range. Answer # get input from user, and put it in the variable called "number" users_number = int( input("enter a number (1-10): ") ) Enter a number (1-10): 34 Incorrect Enter a number (1-10): 6 Correct WORK THROUGH LIVE # print output depending on input if users_number >= 1 and number <= 10: print("correct") print("incorrect")

Exercise Write a program that asks the user to enter a number. The program should determine if the number is odd or even, and print out an appropriate message. Hint How do you check if a number is even? What is an even number? Enter a number: 56 You entered 56 which is even Enter a number: 33 You entered 33 which is odd Hint How do you check if a number is even? What is an even number? - It is a number that is divisible by 2. - When you divide it by 2, there is nothing extra left over. There is no "remainder". Operators Operation Symbol Applied to integers Applied to floating point numbers Applied to strings Exponent ** 2 ** 3 = 8 2.0 ** 3.0 = 8.0 N/A (ERROR) Multiply * 2 * 2 = 4 2.0 * 2.0 = 4.0 a * 3 = aaa Divide / 10/3 = 3.333 10.0/3.0 = 3.333 N/A (ERROR) Divide (integer) // 10 // 3 = 3 10.0//3.0 = 3.0 N/A (ERROR) Remainder % 10 % 3 = 1 10.0 % 3.0 = 1.0 N/A (ERROR) Add + 8 + 9 = 17 8.0 + 9.0 = 17.0 a + b = ab Subtract - 9-7 = 2 9.0-7.0 = 2.0 N/A (ERROR)

Answer # get input from user, and put it in the variable called "number" users_number = int(input("enter a number: ")) # print output, reporting on whether or not number is even if (users_number % 2) == 0: print("you entered ", users_number, " which is even") print("you entered ", users_number, " which is odd") Loops While... Loops Used to perform a sequence of statements repeatedly. Statements that are executed when the "while s condition" is True must be bed underneath the while statement Syntax: while [logical condition]: [statements that will repeatedly run while the condition is true...] [statements that run once the condition is no longer true] Loops statements before the loop Is the condition true? Yes Execute the sequence of statements in the block. No statements after the loop

Example Goal Write a program to print the numbers from 1 to 5 WORK THROUGH LIVE Example Goal Write a program to print the numbers from 1 to 5 Program current = 1 while current <= 5: print(current) current = current + 1 print("finished!") Output 1 2 3 4 5 Finished! Exercise What does these programs produce as output? # Program A while x == 2 : print(x) x = x + 1 # Program B while x >= 0 : print(x) x = x + 1 Exercise Write a program that repeatedly asks the user to enter a number between 1 and 10 (inclusive). When they do so, the program should print Well done on screen and end (i.e. stop asking the user to enter a number). Enter a number between 1 and 10: 99 Enter a number between 1 and 10: -1 Enter a number between 1 and 10: 10 Well done

Answer # get input from user, and put it in the variable called "number" number = int(input("enter a number between 1 and 10: ")) # keep asking for a number until it is in range while number < 1 or number > 10: number = int(input("enter a number between 1 and 10: ")) print("well done") Exercise Write a program that repeatedly asks the user to enter a number. If the number is even, then x is even (where x is the number) should be printed on screen. If the number is odd, then x is odd should be printed on screen. The program must print Thanks and end when the user types 0 Please enter a number: 45 45 is odd Please enter a number: 12 12 is even Please enter a number: 0 Thanks Answer user_input = int(input("please enter a number: ")) # until the user input is 0. while user_input!= 0: # report on evenness of input if user_input % 2 == 0: print(user_input, "is even") print(user_input, "is odd") user_input = int(input("please enter a number: ")) # the user's input was 0 print("thanks") Summary if statements allow you to introduce conditional activities into your program x = 10 if x == 5 : print("x is equal to five.") if x % 2 == 0: print("x is even") while loops allow you to repeat certain statements for as long as the logical condition evaluates to true while x < 10: print(x) x = x + 1