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

Size: px
Start display at page:

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

Transcription

1 C H A P T E R 7 In the first few chapters, we saw some of the basic building blocks of a program. We can now make a program with input, processing, and output. We can even make our input and output a little fancier by using a GUI. We can assign the input to a variable, so we can use it later, and we can use some math to process it. Now we re going to start looking at ways to control what the program does. If a program did the same thing every time, it would be a little boring and not very useful. Programs need to be able to make decisions on what to do. We re going to add some different decision-making techniques to our processing repertoire. Testing, testing Programs need to be able to do different things based on their input. Here are a few examples: If Tim got the right answer, add 1 point to his score. If Jane hit the alien, make an explosion sound. If the file isn t there, display an error message. To make decisions, programs check (do a test) to see if a certain condition is true or not. In the first example above, the condition is got the right answer. Python has only a few ways to test something, and there are only two possible answers for each test: true or false. 65

2 66 Hello World! Here are some questions Python can ask to test something: Are two things equal? Is one thing less than another? Is one thing greater than another? But wait a minute, got the right answer isn t one of the tests you can do, at least not directly. That means you need to describe the test in a way Python can understand. When we want to know if Tim got the right answer, we d probably know the correct answer, as well as Tim s answer. We could write something like this: If Tim had the correct answer, then the two variables would be equal, and the condition would be true. If he had the wrong answer, the two variables would not be equal, and the condition would be false. Doing tests and making decisions based on the results is called branching. The program decides which way to go, or which branch to follow, based on the result of the test. Python uses the keyword if to test conditions, like this: if timsanswer == correctanswer: print "You got it right!" score = score + 1 print "Thanks for playing." These lines form a block of code because they re indented from the lines above and below

3 CHAPTER 7 67 A block of code is one or more lines of code that are grouped together. They re all related to a particular part of the program (like an if statement). In Python, blocks of code are formed by indenting the lines of code in the block. The colon (:) at the end of the if line tells Python that a block of instructions is coming next. The block includes every line that is indented from the if line, up until the next line that is not indented. Indenting means that a line of code is pushed over to the right a bit. Instead of starting at the far left, it has some spaces at the beginning, so it starts a few characters away from the left side. If the condition is true, everything in the following block will be done. In the previous short example, the second and third lines make up the block of statements for the if in the first line. Now might be a good time to talk about indenting and blocks of code. Indenting In some languages, indenting is just a matter of style you can indent however you like (or not at all). But in Python, indenting is a necessary part of how you write the code. Indenting tells Python where blocks of code start and where they end.

4 68 Hello World! Some statements in Python, like the if statement, need a block of code to tell them what to do. In the case of the if statement, the block tells Python what to do if the condition is true. It doesn t matter how far you indent the block, as long as the whole block is indented the same amount. A convention in Python is to use four spaces to indent blocks of code. It would be a good idea to follow this style in your programs. A convention just means lots of people do it that way. Am I seeing double? Are there actually two equal signs in that if statement (if timsanswer == correctanswer)? Yes, there are, and here s why. Am I seeing double? People say, Five plus four is equal to nine, and they ask, Is five plus four equal to nine? One is a statement; the other is a question. In Python we have the same kinds of things statements and questions. A statement might assign a value to a variable. A question might check whether a variable is equal to a certain value. One means you re setting something (assigning it or making it equal). The other means you re checking or testing something (is it equal, yes or no?). So Python uses two different symbols. You already saw the equal sign (=) used for setting or assigning values to variables. Here are a few more examples: correctanswer = temperature = 35 name = "Bill" For testing whether two things are equal, Python uses a double equal sign (==), like this: if myanswer == correctanswer: if temperature == 40: if name == "Fred":

5 CHAPTER 7 69 Mixing up = and == is one of the most common mistakes in programming. Several languages use these symbols (not just Python), and lots of programmers use the wrong one in the wrong place every day. Testing or checking is also called comparing. The double equal sign is called a comparison operator. Remember, we talked about operators in chapter 3. An operator is a special symbol that operates on the values around it. In this case, the operation is to test whether the values are equal. Other kinds of tests Lucky for us, the other comparison operators are easier to remember: less than (<), greater than (>), and not equal to (!=). (You can also use <> for not equal to, but most people use!=.) You can also combine > or < with = to make greater than or equal to (>=) and less than or equal to (<=). You might have seen some of these in math class. Not equal In Python 3, the <> syntax for not equal to is no longer supported. In Python 3, you have to use!= for not equal to. You can also chain two greater-than or less-than operators together to make an in-between test, like this: if 8 < age < 12:

6 70 Hello World! This will check if the variable age has a value between, but not including, 8 and 12. This would be true if age was equal to 9, 10, or 11 (or 8.1 or 11.6, and so on). If we wanted to include the ages 8 and 12, we d do this instead: if 8 <= age <= 12: Comparison operators are also called relational operators (because they test the relation between the two sides: equal or not equal, greater than or less than). A comparison is also called a conditional test or logical test. In programming, logical refers to something where the answer is either true or false. Listing 7.1 shows an example program using comparisons. Start a new file in the IDLE editor, type this program in, and save it call it compare.py. Then Run it. Try running it several times, using different numbers. Try numbers where the first one is bigger, where the first one is smaller, and where the two numbers are equal, and see what you get. Listing 7.1 Using the comparison operators num1 = float(raw_input("enter the first number: ")) num2 = float(raw_input("enter the second number: ")) if num1 < num2: print num1, "is less than", num2 if num1 > num2: print num1, "is greater than", num2 if num1 == num2: print num1, "is equal to", num2 if num1!= num2: print num1, "is not equal to", num2 Remember that this is a double equal sign What happens if the test is false? You ve seen how to make Python do something if the result of a test is true. But what does it do if the test is false? In Python, there are three possibilities: Do another test. If the first test comes out false, you can get Python to test something else with the keyword (which is short for else if ), like this: if answer >= 10: print "You got at least 10!" answer >= 5: print "You got at least 5!" answer >= 3: print "You got at least 3!"

7 CHAPTER 7 71 if answer>=10 answer>=5 answer>=3 least 3! least 5! least 10! You can have as many statements as you want after the if. Do something else if all the other tests come out false. You do this with the else keyword. This always goes at the end, after you ve done the if and any statements: if answer >= 10: print "You got at least 10!" answer >= 5: print "You got at least 5!" answer >= 3: print "You got at least 3!" print "You got less than 3." if answer>=10 answer>=5 answer>=3 else Got less than 3! least 3! least 5! least 10!

8 72 Hello World! Move on. If you don t put anything else after the if block, the program will continue on to the next line of code (if there is one) or will end (if there is no more code). if answer>=10 answer>=5 answer>=3 least 3! least 5! least 10! Try making a program with the code above by adding a line at the beginning to input a number: answer = float(raw_input ("Enter a number from 1 to 15")) Remember to save the file (you pick the name this time), and then run it. Try it a few times with different inputs to see what you get. Testing for more than one condition What if you want to test for more than one thing? Let s say we made a game that was for eight-year-olds and up, and we want to make sure the player is in at least third grade. There are two conditions to meet. Here is one way we could test for both conditions: age = float(raw_input("enter your age: ")) grade = int(raw_input("enter your grade: ")) if age >= 8: if grade >= 3: print "You can play this game." print "Sorry, you can't play the game." Notice that the first print line is indented eight spaces, not just four spaces. That s because each if needs its own block, so each one has its own indenting.

9 CHAPTER 7 73 Reminder Remember, if you re using Python 3, you need to replace raw_input() with input(), and you need to use parentheses with print, like this: print("you can play this game.") Using and That last example will work fine. But there is a shorter way to do the same thing. You can combine conditions like this: age = float(raw_input("enter your age: ")) grade = int(raw_input("enter your grade: ")) if age >= 8 and grade >= 3: print "You can play this game." print "Sorry, you can't play the game." Combine conditions with and We combined the two conditions using the and keyword. The and means both of the conditions have to be true for the following block to execute. if age>=8 and grade>=3 You can play! (Only get here if both conditions are true) You can t play! You can t play!

10 74 Hello World! You can put more than two conditions together with and: age = float(raw_input("enter your age: ")) grade = int(raw_input("enter your grade: ")) color = raw_input("enter your favorite color: ") if age >= 8 and grade >= 3 and color == "green": print "You are allowed to play this game." print "Sorry, you can't play the game." If there are more than two conditions, all the conditions have to be true for the if statement to be true. There are other ways of combining conditions too. Using or The or keyword is also used to put conditions together. If you use or, the block is executed if any of the conditions are true: color = raw_input("enter your favorite color: ") if color == "red" or color == "blue" or color == "green": print "You are allowed to play this game." print "Sorry, you can't play the game." if color = red or color = blue or color = green else You can t play! You can play! Using not You can also flip around a comparison to mean the opposite, using not: (Get here if any condition is true) color = raw_input("enter your favorite color: ") if color == "red" or color == "blue" or color == "green": print "You are allowed to play this game." print "Sorry, you can't play the game."

11 CHAPTER 7 75 This line means the same as this one: if not (age < 8): if age >= 8: In both cases, the block executes if the age is 8 or higher, and it doesn t if the age is lower than 8. In chapter 4, you saw math operators like +, -, *, and /. In this chapter, you saw the comparison operators <, >, ==, and so on. The and, or, and not keywords are also operators. They re called logical operators. They re used to modify comparisons by combining two or more of them (and, or) or reversing them (not). Table 7.1 lists all the operators we ve talked about so far. Table 7.1 List of math and comparison operators Operator Name What it does Math operators = Assignment Assigns a value to a name (variable). + Addition Adds two numbers together. This can also be used to concatenate strings. - Subtraction Subtracts two numbers. += Increment Adds one to a number. -= Decrement Subtracts one from a number. * Multiplication Multiplies two numbers together. / Division Divides two numbers. If both numbers are integers, the result will be just the integer quotient, with no remainder. % Modulus Gets the remainder (or modulus) for integer division of two numbers. ** Exponentiation Raises a number to a power. Both the number and the power can be integers or floats. Comparison operators == Equality Checks whether two things are equal. < Less than Checks whether the first number is less than the second number. > Greater than Checks whether the first number is greater than the second number. <= Less than or equal to >= Greater than or equal to Checks whether the first number is less than or equal to the second number. Checks whether the first number is greater than or equal to the second number.!= <> Not equal to Checks whether two things are not equal. (Either operator can be used.)

Text Input and Conditionals

Text Input and Conditionals Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()

More information

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

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

More information

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications Hello World! Computer Programming for Kids and Other Beginners by Warren Sande and Carter Sande Chapter 1 Copyright 2009 Manning Publications brief contents Preface xiii Acknowledgments xix About this

More information

BB4W. KS3 Programming Workbook INTRODUCTION TO. BBC BASIC for Windows. Name: Class:

BB4W. KS3 Programming Workbook INTRODUCTION TO. BBC BASIC for Windows. Name: Class: KS3 Programming Workbook INTRODUCTION TO BB4W BBC BASIC for Windows Name: Class: Resource created by Lin White www.coinlea.co.uk This resource may be photocopied for educational purposes Introducing BBC

More information

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners Getting Started Excerpted from Hello World! Computer Programming for Kids and Other Beginners EARLY ACCESS EDITION Warren D. Sande and Carter Sande MEAP Release: May 2008 Softbound print: November 2008

More information

Flow Control: Branches and loops

Flow Control: Branches and loops Flow Control: Branches and loops In this context flow control refers to controlling the flow of the execution of your program that is, which instructions will get carried out and in what order. In the

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python CS95003 - Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / 2014 Subjects 1) Beginning with Python 2) Variables 3) Strings 4) Basic arithmetic operators 5) Flow control 6) Comparison

More information

Chapter 1 Operations With Numbers

Chapter 1 Operations With Numbers Chapter 1 Operations With Numbers Part I Negative Numbers You may already know what negative numbers are, but even if you don t, then you have probably seen them several times over the past few days. If

More information

Fraction Arithmetic. A proper fraction is a fraction with a smaller numerator than denominator.

Fraction Arithmetic. A proper fraction is a fraction with a smaller numerator than denominator. Fraction Arithmetic FRAX is a game that is designed to help you and your student/child master fractions, but it does not teach them the basics. I ve put together this document to help remind you about

More information

Python Games. Session 1 By Declan Fox

Python Games. Session 1 By Declan Fox Python Games Session 1 By Declan Fox Rules General Information Wi-Fi Name: CoderDojo Password: coderdojowireless Website: http://cdathenry.wordpress.com/ Plans for this year Command line interface at first

More information

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CMSC 201 Fall 2016 Lab 09 Advanced Debugging CMSC 201 Fall 2016 Lab 09 Advanced Debugging Assignment: Lab 09 Advanced Debugging Due Date: During discussion Value: 10 points Part 1: Introduction to Errors Throughout this semester, we have been working

More information

Visualize ComplexCities

Visualize ComplexCities Introduction to Python Chair of Information Architecture ETH Zürich February 22, 2013 First Steps Python Basics Conditionals Statements Loops User Input Functions Programming? Programming is the interaction

More information

CS 115 Lecture 8. Selection: the if statement. Neil Moore

CS 115 Lecture 8. Selection: the if statement. Neil Moore CS 115 Lecture 8 Selection: the if statement Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 24 September 2015 Selection Sometime we want to execute

More information

Programming with Python

Programming with Python Programming with Python Dr Ben Dudson Department of Physics, University of York 21st January 2011 http://www-users.york.ac.uk/ bd512/teaching.shtml Dr Ben Dudson Introduction to Programming - Lecture 2

More information

Boolean Expressions. Is Equal and Is Not Equal

Boolean Expressions. Is Equal and Is Not Equal 3 MAKING CHOICES Now that we ve covered how to create constants and variables, you re ready to learn how to tell your computer to make choices. This chapter is about controlling the flow of a computer

More information

Python for Informatics

Python for Informatics Python for Informatics Exploring Information Version 0.0.6 Charles Severance Chapter 3 Conditional execution 3.1 Boolean expressions A boolean expression is an expression that is either true or false.

More information

T H E I N T E R A C T I V E S H E L L

T H E I N T E R A C T I V E S H E L L 3 T H E I N T E R A C T I V E S H E L L The Analytical Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform. Ada Lovelace, October 1842 Before

More information

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

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

(Refer Slide Time: 00:26)

(Refer Slide Time: 00:26) Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute Technology, Madras Module 07 Lecture 07 Contents Repetitive statements

More information

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials Fundamentals We build up instructions from three types of materials Constants Expressions Fundamentals Constants are just that, they are values that don t change as our macros are executing Fundamentals

More information

Invent Your Own Computer Games with Python

Invent Your Own Computer Games with Python Hello Wor ld! Invent Your Own Computer Games with Python Taesoo Kwon Heejin Park Hanyang University Introduction to Python Python Easier to learn than C. Serious programming language. Many expert programmers

More information

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Topics 1. Expressions 2. Operator precedence 3. Shorthand operators 4. Data/Type Conversion 1/15/19 CSE 1321 MODULE 2 2 Expressions

More information

Introduction to Python (All the Basic Stuff)

Introduction to Python (All the Basic Stuff) Introduction to Python (All the Basic Stuff) 1 Learning Objectives Python program development Command line, IDEs, file editing Language fundamentals Types & variables Expressions I/O Control flow Functions

More information

Add Subtract Multiply Divide

Add Subtract Multiply Divide ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient

More information

Boolean Expressions. Is Equal and Is Not Equal

Boolean Expressions. Is Equal and Is Not Equal 3 MAKING CHOICES ow that we ve covered how to create constants and variables, you re ready to learn how to tell your computer to make choices. This chapter is about controlling the flow of a computer program

More information

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block THE IF STATEMENT The if statement is used to check a condition: if the condition is true, we run a block of statements (called the if-block), elsewe process another block of statements (called the else-block).

More information

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python CONDITION CONTROLLED LOOPS Introduction to Programming - Python Generating Random Numbers Generating a random integer Sometimes you need your program to generate information that isn t available when you

More information

ENGR 101 Engineering Design Workshop

ENGR 101 Engineering Design Workshop ENGR 101 Engineering Design Workshop Lecture 2: Variables, Statements/Expressions, if-else Edgardo Molina City College of New York Literals, Variables, Data Types, Statements and Expressions Python as

More information

Algorithms and Programming I. Lecture#12 Spring 2015

Algorithms and Programming I. Lecture#12 Spring 2015 Algorithms and Programming I Lecture#12 Spring 2015 Think Python How to Think Like a Computer Scientist By :Allen Downey Installing Python Follow the instructions on installing Python and IDLE on your

More information

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Building Java Programs. Chapter 2: Primitive Data and Definite Loops Building Java Programs Chapter 2: Primitive Data and Definite Loops Copyright 2008 2006 by Pearson Education 1 Lecture outline data concepts Primitive types: int, double, char (for now) Expressions: operators,

More information

An Introduction to Python

An Introduction to Python An Introduction to Python Day 2 Renaud Dessalles dessalles@ucla.edu Python s Data Structures - Lists * Lists can store lots of information. * The data doesn t have to all be the same type! (unlike many

More information

Ex: If you use a program to record sales, you will want to remember data:

Ex: If you use a program to record sales, you will want to remember data: Data Variables Programs need to remember values. Ex: If you use a program to record sales, you will want to remember data: A loaf of bread was sold to Sione Latu on 14/02/19 for T$1.00. Customer Name:

More information

Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few

Most of the class will focus on if/else statements and the logical statements (conditionals) that are used to build them. Then I'll go over a few With notes! 1 Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few useful functions (some built into standard

More information

Variables, expressions and statements

Variables, expressions and statements Variables, expressions and statements 2.1. Values and data types A value is one of the fundamental things like a letter or a number that a program manipulates. The values we have seen so far are 2 (the

More information

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Excerpt from Art of Problem Solving Volume 1: the Basics 2014 AoPS Inc. Chapter 5 Using the Integers In spite of their being a rather restricted class of numbers, the integers have a lot of interesting properties and uses. Math which involves the properties of integers is

More information

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors First Program Compilation and Execution Simplifying Fractions Loops If Statements Variables Operations Using Functions Errors C++ programs consist of a series of instructions written in using the C++ syntax

More information

Week - 01 Lecture - 03 Euclid's Algorithm for gcd. Let us continue with our running example of gcd to explore more issues involved with program.

Week - 01 Lecture - 03 Euclid's Algorithm for gcd. Let us continue with our running example of gcd to explore more issues involved with program. Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 01 Lecture - 03 Euclid's Algorithm

More information

CMPT 120 Control Structures in Python. Summer 2012 Instructor: Hassan Khosravi

CMPT 120 Control Structures in Python. Summer 2012 Instructor: Hassan Khosravi CMPT 120 Control Structures in Python Summer 2012 Instructor: Hassan Khosravi The If statement The most common way to make decisions in Python is by using the if statement. The if statement allows you

More information

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression Outline ECS 10 10/8 Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit( ) Example: Coin flipping (if time permits) Announcements Professor Amenta

More information

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA 1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

Python for Non-programmers

Python for Non-programmers Python for Non-programmers A Gentle Introduction 1 Yann Tambouret Scientific Computing and Visualization Information Services & Technology Boston University 111 Cummington St. yannpaul@bu.edu Winter 2013

More information

cs1114 REVIEW of details test closed laptop period

cs1114 REVIEW of details test closed laptop period python details DOES NOT COVER FUNCTIONS!!! This is a sample of some of the things that you are responsible for do not believe that if you know only the things on this test that they will get an A on any

More information

Chapter 4: Control Structures I (Selection)

Chapter 4: Control Structures I (Selection) Chapter 4: Control Structures I (Selection) 1 Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean) expressions Discover

More information

CS 1301 Exam 1 Answers Fall 2009

CS 1301 Exam 1 Answers Fall 2009 Page 1/6 CS 1301 Fall 2009 Exam 1 Your Name: I commit to uphold the ideals of honor and integrity by refusing to betray the trust bestowed upon me as a member of the Georgia Tech community. CS 1301 Exam

More information

Object-Oriented Programming

Object-Oriented Programming Object-Oriented Programming Java Syntax Program Structure Variables and basic data types. Industry standard naming conventions. Java syntax and coding conventions If Then Else Case statements Looping (for,

More information

Quiz. Introduction: Python. In this project, you ll make a quiz game to challenge your friends. Activity Checklist.

Quiz. Introduction: Python. In this project, you ll make a quiz game to challenge your friends. Activity Checklist. Python 1 Quiz All Code Clubs must be registered. Registered clubs appear on the map at codeclub.org.uk - if your club is not on the map then visit jumpto.cc/18cplpy to find out what to do. Introduction:

More information

Adding and Subtracting Integers

Adding and Subtracting Integers Quarterly 1 Review Sheet (NOTE: This may not include everything you need to know for tomorrow about every topic. It is student created and I am just sharing it in case you find it helpful) Page 1: Adding

More information

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. Lesson 2 VARIABLES Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. WHAT ARE VARIABLES? When you input data (i.e. information) into a computer

More information

Try typing the following in the Python shell and press return after each calculation. Write the answer the program displays next to the sums below.

Try typing the following in the Python shell and press return after each calculation. Write the answer the program displays next to the sums below. Name: Date: Instructions: PYTHON - INTRODUCTORY TASKS Open Idle (the program we will be using to write our Python codes). We can use the following code in Python to work out numeracy calculations. Try

More information

Python Unit

Python Unit Python Unit 1 1.1 1.3 1.1: OPERATORS, EXPRESSIONS, AND VARIABLES 1.2: STRINGS, FUNCTIONS, CASE SENSITIVITY, ETC. 1.3: OUR FIRST TEXT- BASED GAME Python Section 1 Text Book for Python Module Invent Your

More information

The Practice of Computing Using PYTHON. Chapter 2. Control. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The Practice of Computing Using PYTHON. Chapter 2. Control. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Practice of Computing Using PYTHON William Punch Richard Enbody Chapter 2 Control 1 Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Control: A Quick Overview 2 Selection

More information

Learning the Language - V

Learning the Language - V Learning the Language - V Fundamentals We now have locations to store things so we need a way to get things into those storage locations To do that, we use assignment statements Deja Moo: The feeling that

More information

Conditional Expressions and Decision Statements

Conditional Expressions and Decision Statements Conditional Expressions and Decision Statements June 1, 2015 Brian A. Malloy Slide 1 of 23 1. We have introduced 5 operators for addition, subtraction, multiplication, division, and exponentiation: +,

More information

If Statements, For Loops, Functions

If Statements, For Loops, Functions Fundamentals of Programming If Statements, For Loops, Functions Table of Contents Hello World Types of Variables Integers and Floats String Boolean Relational Operators Lists Conditionals If and Else Statements

More information

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Spring 2011 Python Python was developed

More information

Programming for Engineers in Python. Recitation 1

Programming for Engineers in Python. Recitation 1 Programming for Engineers in Python Recitation 1 Plan Administration: Course site Homework submission guidelines Working environment Python: Variables Editor vs. shell Homework 0 Python Cont. Conditional

More information

Basics of Java Programming

Basics of Java Programming Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

More information

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python ENGG1811 UNSW, CRICOS Provider No: 00098G W4 Computers have changed engineering http://www.noendexport.com/en/contents/48/410.html

More information

MA 1128: Lecture 02 1/22/2018

MA 1128: Lecture 02 1/22/2018 MA 1128: Lecture 02 1/22/2018 Exponents Scientific Notation 1 Exponents Exponents are used to indicate how many copies of a number are to be multiplied together. For example, I like to deal with the signs

More information

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT LESSON VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT PROF. JOHN P. BAUGH PROFJPBAUGH@GMAIL.COM PROFJPBAUGH.COM CONTENTS INTRODUCTION... Assumptions.... Variables and Data Types..... Numeric Data Types:

More information

PYTHON YEAR 10 RESOURCE. Practical 01: Printing to the Shell KS3. Integrated Development Environment

PYTHON YEAR 10 RESOURCE. Practical 01: Printing to the Shell KS3. Integrated Development Environment Practical 01: Printing to the Shell To program in Python you need the latest version of Python, which is freely available at www.python.org. Your school will have this installed on the computers for you,

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

More information

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors CMSC201 Computer Science I for Majors Lecture 03 Operators All materials copyright UMBC and Dr. Katherine Gibson unless otherwise noted Variables Last Class We Covered Rules for naming Different types

More information

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch Purpose: We will take a look at programming this week using a language called Scratch. Scratch is a programming language that was developed

More information

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

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an  . Michigan State University CSE 231, Fall 2013 CSE 231, Rich Enbody Office Hours After class By appointment send an email 2 1 Project 1 Python arithmetic Do with pencil, paper and calculator first Idle Handin Help room 3 What is a Computer Program?

More information

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet. Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that

More information

MITOCW watch?v=0jljzrnhwoi

MITOCW watch?v=0jljzrnhwoi MITOCW watch?v=0jljzrnhwoi The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To

More information

Making Decisions In Python

Making Decisions In Python Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action. Decision Making Is All About Choices My next vacation? Images:

More information

CS Summer 2013

CS Summer 2013 CS 1110 - Summer 2013 intro to programming -- how to think like a robot :) we use the Python* language (www.python.org) programming environments (many choices): Eclipse (free from www.eclipse.org), or

More information

4 + 4 = = 1 5 x 2 = 10

4 + 4 = = 1 5 x 2 = 10 Beginning Multiplication Ask your child... "Have you ever seen a multiplication problem?" Explain: "Instead of a plus ( + ) sign or a minus ( - ) sign a multiplication sign ( x ) is used." Have child to

More information

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms Faculty of Mathematics Waterloo, Ontario N2L 3G1 Centre for Education in Mathematics and Computing Relations Let s talk about relations! Grade 6 Math Circles November 6 & 7 2018 Relations, Functions, and

More information

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A DEBUGGING TIPS COMPUTER SCIENCE 61A 1 Introduction Every time a function is called, Python creates what is called a stack frame for that specific function to hold local variables and other information.

More information

CMSC 201 Fall 2018 Lab 04 While Loops

CMSC 201 Fall 2018 Lab 04 While Loops CMSC 201 Fall 2018 Lab 04 While Loops Assignment: Lab 04 While Loops Due Date: During discussion, September 24 th through September 27 th Value: 10 points (8 points during lab, 2 points for Pre Lab quiz)

More information

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Java Programming Fundamentals - Day Instructor: Jason Yoon Website: Java Programming Fundamentals - Day 1 07.09.2016 Instructor: Jason Yoon Website: http://mryoon.weebly.com Quick Advice Before We Get Started Java is not the same as javascript! Don t get them confused

More information

Introduction to Programming with JES

Introduction to Programming with JES Introduction to Programming with JES Titus Winters & Josef Spjut October 6, 2005 1 Introduction First off, welcome to UCR, and congratulations on becoming a Computer Engineering major. Excellent choice.

More information

AP Computer Science A

AP Computer Science A AP Computer Science A 1st Quarter Notes Table of Contents - section links Click on the date or topic below to jump to that section Date : 9/8/2017 Aim : Java Basics Objects and Classes Data types: Primitive

More information

Condition Controlled Loops. Introduction to Programming - Python

Condition Controlled Loops. Introduction to Programming - Python + Condition Controlled Loops Introduction to Programming - Python + Repetition Structures n Programmers commonly find that they need to write code that performs the same task over and over again + Example:

More information

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

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s Intro to Python Python Getting increasingly more common Designed to have intuitive and lightweight syntax In this class, we will be using Python 3.x Python 2.x is still very popular, and the differences

More information

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

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1 The Three Rules Chapter 1 Beginnings Rule 1: Think before you program Rule 2: A program is a human-readable essay on problem solving that also executes on a computer Rule 3: The best way to improve your

More information

Downloaded from Chapter 2. Functions

Downloaded from   Chapter 2. Functions Chapter 2 Functions After studying this lesson, students will be able to: Understand and apply the concept of module programming Write functions Identify and invoke appropriate predefined functions Create

More information

GAP CLOSING. Grade 9. Facilitator s Guide

GAP CLOSING. Grade 9. Facilitator s Guide GAP CLOSING Grade 9 Facilitator s Guide Topic 3 Integers Diagnostic...5 Administer the diagnostic...5 Using diagnostic results to personalize interventions solutions... 5 Using Intervention Materials...8

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

QUIZ: What value is stored in a after this

QUIZ: What value is stored in a after this QUIZ: What value is stored in a after this statement is executed? Why? a = 23/7; QUIZ evaluates to 16. Lesson 4 Statements, Expressions, Operators Statement = complete instruction that directs the computer

More information

CSC 220: Computer Organization Unit 10 Arithmetic-logic units

CSC 220: Computer Organization Unit 10 Arithmetic-logic units College of Computer and Information Sciences Department of Computer Science CSC 220: Computer Organization Unit 10 Arithmetic-logic units 1 Remember: 2 Arithmetic-logic units An arithmetic-logic unit,

More information

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal An Easy Intro to Python & Programming Don t just buy a new video game, make one. Don t just download the latest app, help design it. Don t just play on your phone, program it. No one is born a computer

More information

Languages. Solve problems using a computer, give the computer instructions. Remember our diaper-changing exercise?

Languages. Solve problems using a computer, give the computer instructions. Remember our diaper-changing exercise? Languages Solve problems using a computer, give the computer instructions. Remember our diaper-changing exercise? Talk the talk Speak its language High-level: Python, C++, Java Low-level: machine language,

More information

Hello, World and Variables

Hello, World and Variables Hello, World and Variables Hello, World! The most basic program in any language (Python included) is often considered to be the Hello, world! statement. As it s name would suggest, the program simply returns

More information

Topic 4 Expressions and variables

Topic 4 Expressions and variables Topic 4 Expressions and variables "Once a person has understood the way variables are used in programming, he has understood the quintessence of programming." -Professor Edsger W. Dijkstra Based on slides

More information

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration STATS 507 Data Analysis in Python Lecture 2: Functions, Conditionals, Recursion and Iteration Functions in Python We ve already seen examples of functions: e.g., type()and print() Function calls take the

More information

Math Day 2 Programming: How to make computers do math for you

Math Day 2 Programming: How to make computers do math for you Math Day 2 Programming: How to make computers do math for you Matt Coles February 10, 2015 1 Intro to Python (15min) Python is an example of a programming language. There are many programming languages.

More information

Python lab session 1

Python lab session 1 Python lab session 1 Dr Ben Dudson, Department of Physics, University of York 28th January 2011 Python labs Before we can start using Python, first make sure: ˆ You can log into a computer using your username

More information

Iron County Schools. Yes! Less than 90 No! 90 No! More than 90. angle: an angle is made where two straight lines cross or meet each other at a point.

Iron County Schools. Yes! Less than 90 No! 90 No! More than 90. angle: an angle is made where two straight lines cross or meet each other at a point. Iron County Schools 1 acute angle: any angle that is less than 90. Yes! Less than 90 No! 90 No! More than 90 acute triangle: a triangle where all the angles are less than 90 angle: an angle is made where

More information

Post Experiment Interview Questions

Post Experiment Interview Questions Post Experiment Interview Questions Questions about the Maximum Problem 1. What is this problem statement asking? 2. What is meant by positive integers? 3. What does it mean by the user entering valid

More information

CSCE 110 Programming I

CSCE 110 Programming I CSCE 110 Programming I Basics of Python (Part 1): Variables, Expressions, and Input/Output Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Spring 2013 Tiffani

More information

Programming: Part I. In this section of notes you will learn about how to write simple programs using JES. Translators

Programming: Part I. In this section of notes you will learn about how to write simple programs using JES. Translators Programming: Part I In this section of notes you will learn about how to write simple programs using JES. Translators The (greatly simplified) process of writing a computer program. Step 3: The (binary)

More information

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style CS125 : Introduction to Computer Science Lecture Notes #4 Type Checking, Input/Output, and Programming Style c 2005, 2004, 2002, 2001, 2000 Jason Zych 1 Lecture 4 : Type Checking, Input/Output, and Programming

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 2 Data and expressions reading: 2.1 3 The computer s view Internally, computers store everything as 1 s and 0

More information

CIS133J. Working with Numbers in Java

CIS133J. Working with Numbers in Java CIS133J Working with Numbers in Java Contents: Using variables with integral numbers Using variables with floating point numbers How to declare integral variables How to declare floating point variables

More information