Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y
Programming in Visual Basic
16-May-15 Sudden Python Drinking from the Fire Hose.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Introducing Java.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
30/10/ Iteration Loops Do While (condition is true) … Loop.
Topics: IF If statements Else clauses. IF Statement For the conditional expression, evaluating to True or False, the simple IF statement is if : x = 7.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python Functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
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.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
Lesson 4 Mathematical Operators! October 6, 2009.
Python Let’s get started!.
Programming Seminar 2009 Night 0. Why we’re holding this seminar You’re probably not a computer science major – Or even anything remotely close (e.g.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
COSC 1223 Computer Science Concepts I Joe Bryan. What is a function? Like a mini-program that performs a specific task Two types: Built-in functions Functions.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Whatcha doin'? Aims: To start using Python. To understand loops.
Introduction to Programming
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Introduction to Python
Loops BIS1523 – Lecture 10.
Topics Introduction to Repetition Structures
CS 115 Lecture 8 Structured Programming; for loops
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
While Loops in Python.
Topics Introduction to Repetition Structures
Engineering Innovation Center
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
More Loops.
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Repetition Structures
Computing Fundamentals
Python programming exercise
CISC101 Reminders All assignments are now posted.
Matlab Basics Tutorial
Python While Loops.
While Loops in Python.
def-ining a function A function as an execution control structure
Presentation transcript:

Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and try out “while” loops. Most: Experiment with writing their own functions. Some: Write an nth root function.

If there's just one value, you can use empty curly braces. Formatted Printing The format() command makes it easier to produce attractive output. We are going to use this to improve out multiplication table. Here's the new version of the print command. The numbers in curly braces refer to the terms in the format() part of the statement. Starting at zero, each term is slotted in where we want it.

What happens if you don't enter a whole number? It seems a shame that these are not in line with the ones above! Also, what if I want more than one go?

Try these snippets of code and observe what happens. >>> while False: print("False is the new True.") >>> while 6: print("Which numbers are True?") while -1: print("Which numbers are True?") while 0: print("Which numbers are True?") We are going to need another type of loop to let the user have more than one go at the times tables program. Basically, we use a “while” loop when we don't know how many times a loop will need to run. Otherwise, it's usually best to use a “for” loop. Let's experiment with while loops. A “while” loop is basically a condition followed by a block of indented code. The indented block will keep running as long as the condition is True. while

>>> x = 1 >>> while x < 1025: print(x) x *= 2 What does this do? Task: Write a “while” loop which counts down from 10 to 1 in steps of 0.5. Try doing the same in a “for” loop. What happens?

Input Validation In our program, we want the user to enter a whole number. The program tries to turn what they typed into an integer. If this doesn't work, the program crashes. Which is bad. So we can use “try:” and “except:”. These work as you might expect, they try to do the thing we want and let us specify what should happen if it fails. That way the program will not crash (we hope). Enter this program and try it out. It will be a useful reminder of how to use “try:”.

The “while True:” command creates an infinite loop. “try:” / “except:” clauses deal with bad input. We even the output up by adding a space The end = '' bit means “don't add a newline”.

The program now seems to perform better.

Open a new window in IDLE. Save the program as tables2.py Type it in and then run and test it. Python ignores your comments, but they help to make your code readable.

Functions Functions are really important in programming. We have already used some built-in functions like print(). It's very easy to make your own functions in Python. Here's a built-in function: max(). This bit is called a docstring. Here's how to define a function that doubles the number we pass to it.

The docstring explains what the function does. You should always write a docstring for every function you write. A docstring comes right after the “def my_function():” line. A docstring sits within triple quotation marks. The body of the function is indented (Python's IDLE does this for you). Task: Write a function that subtracts one from its input. Include an appropriate docstring and test your function.

Another Simple Function We've seen that we can raise a number to a power using “**”. If we want the square root of a number, we'd have to type something like this : >>> 81** >>> 81**(1/2) 9.0 Task: Write a function that takes two arguments (n and x) and returns the nth root of x.