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.

Slides:



Advertisements
Similar presentations
While loops.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to Computers and Programming Lecture 7:
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
String Escape Sequences
Adapted from John Zelle’s Book Slides1 CS177 Python Programming Chapter 1 Computers and Programs.
Announcements Class / Recitation slides available on the syllabus page Check morning of for latest version We will start grading clickers next lecture.
Data Structures More List Methods Our first encoding Matrix.
An Introduction to Textual Programming
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
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.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Topics: Sequence Sequences Index into a sequence [] notation Slicing and other operations.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Announcements Additional office hours tomorrow: 3-6pm in HAAS 142 Midterm Bring a #2 pencil (test given via scantron sheet) Photo ID No Labs or Recitation.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Announcements Project1 Due Wednesday September 21 st 4:30pm We will post instructions on how to turnin from home Note: you can always turn in from the.
For loops in programming Assumes you have seen assignment statements and print statements.
Chapter 05 (Part III) Control Statements: Part II.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Announcements Survey link (can be found on piazza): OCJ6
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
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)
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
COMP Loop Statements Yi Hong May 21, 2015.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Announcements No Labs / Recitation this week On Friday we will talk about Project 3 Release late afternoon / evening tomorrow Cryptography.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 CS Review, iClicker -Questions Week 15. Announcements 2.
Announcements Reading Read Chapter 4 (functions).
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.
More about Iteration Victor Norman CS104. Reading Quiz.
Announcements Project 2 Is now posted Will be due on Oct. 1 Week 7 Midterm on Wed. Oct. 3 in EE 129 Come early, EE is a complicated building Review session.
Python: Experiencing IDLE, writing simple programs
Python Loops and Iteration
© 2016 Pearson Education, Ltd. All rights reserved.
CS 115 Lecture 8 Structured Programming; for loops
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CS190/295 Programming in Python for Life Sciences: Lecture 3
CISC101 Reminders All assignments are now posted.
CHAPTER 6: Control Flow Tools (for and while loops)
Chopin’s Nocturne.
Comparing Python and Java
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
LOOP Basics.
Introduction to Python
Presentation transcript:

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 Wednesday (by Dr. Martino) No Class Friday Week 7 Class Wed /Fri as normal Midterm Thursday Project due Sunday

Python Boot Camp Introduction to Ranges Sequences Definite Loops For Loops Comparison of For and While

Our First For Loop # File: chaos.py # A simple program illustrating chaotic behavior def main(): print("This program illustrates a chaotic function") x = eval(input("Enter a number between 0 and 1:")) for i in range(10): x = 3.9 * x * (1 - x) print(x) main()

Executing our First For Loop >>> This program illustrates a chaotic function Enter a number between 0 and 1: >>>

Definite Loops In chaos.py, what did range(10) do? >>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] range is a built-in Python function that generates a sequence of numbers, starting with 0. list is a built-in Python function that turns the sequence into an explicit list (sequence) The body of the loop executes 10 times.

Ranges Python allows us to specify a range of values range(n) 0, 1, …, n-1 Example: list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Ranges Continued But what if we don’t want to start from 0? range(n) is short hand for range(0, n) Example: list(range(-4, 4)) [-4, -3, -2, -1, 0, 1, 2, 3]

Ranges Continued But what if I don’t want to count up by 1 Python allows us to “step” by a given integer range(start, end, step) Example: list(range(0, 10, 2)) [0, 2, 4, 6, 8]

Ranges … again But what if I want to count down? Python allows us to “step” by a given integer range(start, end, step) Lets try: list(range(0,10, -1)) [] Example that works: list(range(10, 0, -1)) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Definite Loops A definite loop executes a definite number of times, i.e., at the time Python starts the loop it knows exactly how many iterations to do. for in : The beginning and end of the body are indicated by indentation. Sequences: ranges, lists, strings

Definite Loops for in : The variable after the for is called the loop index. It takes on each successive value in sequence.

For (Definite) Loops These loops have a more controlled structure and cannot run infinitely This type of loop is commonly used with the range(x,y) function for x in range(0,10): print (x) for x in list(range(0,10)): print (x)

Definite Loops >>> for i in [0,1,2,3]: print (i)

Definite Loops >>> for i in range(0,4): print (i)

Definite Loops >>> for i in list(range(0,4)): print (i)

Definite Loops >>> for odd in [1, 3, 5, 7]: print(odd*odd)

CQ:Are these programs equivalent? for a in range(0, 10, 1): print(a) for a in range(10): print(a) 21 A: yes B: no

Definite Loops for loops alter the flow of program execution, so they are referred to as control structures. more items in = new item yes no

More Complex Examples x = 0 for a in range(10): x = x + a print(x) x = 1 for a in range(1,10): x = x * a print(x)

How to “visualize” a loop You can use print statements to figure out what is happening inside a loop This works for For loops and While loops What should we print? The loop index This will give us information of how the loop index changes as the loop executes Any calculations we may perform

More Complex Examples x = 0 for a in range(10): print(“a is: ”, a) x = x + a print(“x is: ”, x) print(x) a is: 0 x is: 0 a is: 1 x is: 1 a is: 2 x is: 3 a is: 3 x is: 6 a is: 4 x is: 10 a is: 5 x is: 15 a is: 6 x is: 21 a is: 7 x is: 28 a is: 8 x is: 36 a is: 9 x is: 45

More Complex Examples x = 1 for a in range(1,10): print(“a is: ”, a) x = x * a print(“x is: ”, x) print(x) a is: 1 x ix: 1 a is: 2 x ix: 2 a is: 3 x ix: 6 a is: 4 x ix: 24 a is: 5 x ix: 120 a is: 6 x ix: 720 a is: 7 x ix: 5040 a is: 8 x ix: a is: 9 x ix:

CQ:Are these programs equivalent? A: Yes B: No x = 0 y = 0 for k in range(5): x = x + k y = x + k print (y) x = 0 y = 0 for k in range(5): x = x + k y = x + k print (y)

Loops can be nested def complex(a, b): for x in range(0, a): for y in range (0, b): print(x*y)

Visualizing a Nested Loop def complex(a, b): for x in range(0, a): print(“x is: ”, x) for y in range (0, b): print(“y is:”, y) print(x*y) x is: 0 y is: 0 0 y is: 1 0 y is: 2 0 x is: 1 y is: 0 0 y is: 1 1 y is: 2 2

CQ:Are these functions equivalent? def complex(a,b): for x in range(0, a): for y in range (0, b): print(x*y) def complex2(a,b): for y in range(0,b): for x in range (0, a): print(x*y) A: yes B: no

When might they be equivalent? What about when a=b? Ie we call the functions and provide the same values for a and b complex(2,2) = complex2(2,2)

Differences between While and For For loops provide a a finite and enumerated “range” of values This determines the “length” of the loop For loops explicitly rebind the loop index for X in … While loops express an execution condition It executes until that condition no longer holds

Chaos with a While # File: chaos.py # A simple program illustrating chaotic behavior def main(): print("This program illustrates a chaotic function") x = eval(input("Enter a number between 0 and 1:")) a = 0 while a < 10: x = 3.9 * x * (1 - x) print(x) a = a+1 main()

CQ:Are these programs equivalent? a = 0 while(a < 10): print(a) a = a+1 for a in range(10): print(a) 21 A: yes B: no

Homework Read “Loops” in the python wiki book

Announcements

Python Boot Camp How to specify strings Special Characters ASCII Unicode

Strings In addition to basic data types such as Integers most languages provide Strings Strings are sequences of characters. We have seen a few examples already. “This is a string” Strings are designated using “”

Strings and how we specify them Strings are defined with quotations “hello” There are four ways we can define a string ‘hello’ “hello” “””hello””” ‘’’hello’’’

Why do we have three versions? We can use ‘ ‘ to print strings that contain “ Example: print ’ “ ‘ Will print “ Example: print “ ‘ “ Will print ‘ We can use “”” “”” to print strings that span multiple lines

What about special characters? There are more characters than we have on our keyboards Other languages (Greek, Chinese, etc) Characters with accent marks Recall from recitation that we can encode characters We presented ASCII

Unicode Unicode is another way to encode characters Just like there is a limit to the largest integer we can encode in 32 bits there is a limit to the amount of characters we can encode in ASCII Full Unicode encoding is available at:

Unicode Examples

How do we use Unicode in Strings? “\uXXXX” is a Unicode character, where XXXX is a code and each X can be 0-9 or A-F We can use such characters in strings “This is a unicode character: \uFFFF” One Catch (only in Python 2.x): We need to specify a ‘u’ before the string to tell python it contains unicode characters print u”This is a unicode character: \uFFFF”

Try this: print (”This is a unicode character: \uFFFF”)

There are other special Characters “\b” is backspace Example: print (“hello\bworld”) prints hellworld “\n” is a newline (like pressing the Enter key” Example: print (“hello\nworld”) prints hello world

There are other special Characters “\t” is a tab Example: print (“hello\tworld”) prints hello world

String Operations We already know how to concatenate strings Example: print “Hello”+”World” Will print HelloWorld We also know how to get the length of a string Example: print len(“HelloWorld”) Will print 10

String Operations We also know how to print the ASCII encoding of a character Example: print ord(‘b’) Will print 98 We can leverage this to print the ASCII encoding of a string: str = “hello” for char in str: print ord(char)

Advanced String Operations But first we need to introduce some new syntax Object.Method(arguments) We will study Object Oriented programming more formally later in the course

Objects and Methods Methods are similar to functions We can think of a Method as a function or an action performed on an Object An Object can be anything More concretely, any python value We will revisit this definition For now we will consider strings as Objects Different types support different methods

Advanced String Operations print (“Hello World”.capitalize()) Hello world print (“hello world”.capitalize()) Hello world print (“Hello World”.endswith(“World”)) True print (“Hello World”.endswith(“Hello”)) False

CQ:Are these programs equivalent? 1.capitalize()“1”.capitalize() 21 A: yes B: no

Advanced String Operations print (“HeLlO WoRlD”.lower()) hello world print (“hello world”.lower()) hello world print (“Hello World”.upper()) HELLO WORLD print (“hello world”.upper()) HELLO WORLD

Advanced String Operations print (“Hello World”.find(“World”)) 6 print (“Hello World”.find(“or”)) 7 print (“Hello World”.find(“Hello”)) 0 print (“Hello World”.find(“o”)) 4

Example b = “HELLO” a = b.lower() print(b) print(a) >>> b = "HELLO" >>> a = b.lower() >>> print(b) HELLO >>> print(a) hello

Variable Storage In week 2 we had this example. Note that b is a string. 10 a Sb a l l y a = 10 b = “Sally”

String Storage We can see that strings are really individual characters stored into different locations in memory, in order There is another way to store values in memory in this way. 10 a Sb a l l y

The Range Function Revisited Consider this example 10 a 0b a = 10 b = range(0,5)

String Storage Like a string, we have a single variable assigned to the start of multiple values in memory 10 a 0b Sc a a = 10 b = range(0,5) c = “Sally” ….

Homework Read 5.1, 5.2, 5.3, 5.4 Read 8.1 – 8.4 but skip 8.3.3