Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.

Slides:



Advertisements
Similar presentations
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Advertisements

Expressions, variables, and for loops (oh my!) spam, spam, spam, spam
Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
Week 4 Strings, if/else, return, user input Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Strings, if/else, return, user input
Copyright 2008 by Pearson Education 1 Class constants and scope reading: 2.4 self-check: 28 exercises: 11 videos: Ch. 2 #5.
Week 2 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
Week 7 Lists Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Building Java Programs
Ruby (on Rails) CSE 190M, Spring 2009 Week 1. The Players Kelly "Everyday I'm Hustlin' " Dunn Kim "Mouse" Todd Ryan "Papa T" Tucker.
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Ruby! Useful as a scripting language – script: A small program meant for one time use – Targeted towards small to medium size projects Use by: – Amazon,
Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Unit 3 parameters and graphics Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
Unit 2 Expressions and variables; for loops Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Unit 1 Basic Python programs, functions Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
10/9/07 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
If/else, return, user input, strings
Topic 6 loops, figures, constants Based on slides bu Marty Stepp and Stuart Reges from "Complexity has and will maintain.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
Input, Output and Variables GCSE Computer Science – Python.
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 8: Python Writing Scripts in GIS
10/9/07 ______ < Moo? > \ ^__^ \ (oo)\_______ (__)\ )\/\
Building Java Programs
basic Python programs, defining functions
CSc 110, Autumn 2017 Lecture 5: The for Loop and user input
basic Python programs, defining functions
CSc 110, Spring 2017 Lecture 5: Constants and Parameters
CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants
expressions, variables, for loops
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
expressions, variables, for loops
while loops; logic; random numbers; tuples
Building Java Programs
CSc 110, Spring 2018 Lecture 7: input and Constants
Building Java Programs
Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. Like preset stations on a car stereo, or cell.
Building Java Programs
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
Topic 6 loops, figures, constants
Building Java Programs
Building Java Programs
The for loop suggested reading:
Chapter 2 Programming Basics.
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Building Java Programs
Building Java Programs
Building Java Programs
Suggested self-checks:
Building Java Programs
Drawing complex figures
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty Stepp for their work on these slides. Except where otherwise noted, this work is licensed under:

2 Who uses Python? “ Python is fast enough for our site and allows us to produce maintainable features in record times, with a minimum of developers ” -Cuong Do, Software Architect, YouTube.com

3 Expressions Arithmetic is very similar to Java –Operators: + - * / % (plus ** for exponentiation) –Precedence: () before ** before * / % before + - –Integers vs. real numbers >>> >>> * >>> 7 / 2 3 >>> 7.0 / 2 3.5

4 Variables Declaring –no type is written; same syntax as assignment Operators –no ++ or -- operators (must manually adjust by 1) JavaPython int x = 2; x++; System.out.println(x); x = x * 8; System.out.println(x); double d = 3.2; d = d / 2; System.out.println(d); x = 2 x = x + 1 print x x = x * 8 print x d = 3.2 d = d / 2 print d

5 Types Python is looser about types than Java –Variables' types do not need to be declared –Variables can change types as a program is running ValueJava typePython type 42int 3.14doublefloat "ni!"Stringstr

6 String Multiplication Python strings can be multiplied by an integer. –The result is many copies of the string concatenated together. >>> "hello" * 3 "hellohellohello" >>> print 10 * "yo " yo yo yo yo yo >>> print 2 * 3 * "4"

7 String Concatenation Integers and strings cannot be concatenated in Python. –Workarounds: – str( value ) - converts a value into a string – print value, value - prints value twice, separated by a space >>> x = 4 >>> print "Thou shalt not count to " + x + "." TypeError: cannot concatenate 'str' and 'int' objects >>> print "Thou shalt not count to " + str(x) + "." Thou shalt not count to 4. >>> print x + 1, "is out of the question." 5 is out of the question.

8 The for Loop – for name in range( max ): – statements –Repeats for values 0 (inclusive) to max (exclusive) >>> for i in range(5):... print i

9 for Loop Variations – for name in range( min, max ): – statements – for name in range( min, max, step ): – statements –Can specify a minimum other than 0, and a step other than 1 >>> for i in range(2, 6):... print i >>> for i in range(15, 0, -5):... print i

10 Nested Loops Nested loops are often replaced by string * and + –....1 –...2 –..3 –.4 – 5 Java for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (5 - line); j++) { System.out.print("."); } System.out.println(line); } Python 1212 for line in range(1, 6): print (5 - line) * "." + str(line)

11 Constants Python doesn't really have constants. –Instead, declare a variable at the top of your code. –All methods will be able to use this "constant" value. constant.py MAX_VALUE = 3 def printTop(): for i in range( MAX_VALUE ): for j in range(i): print j print def printBottom(): for i in range( MAX_VALUE, 0, -1): for j in range(i, 0, -1): print MAX_VALUE print

12 Exercise Rewrite the Mirror lecture program in Python. Its output: – #================# – | <><> | – | <>....<> | – | <> <> | – |<> <>| – | <> <> | – | <>....<> | – | <><> | – #================# –Make the mirror resizable by using a "constant."

13 Exercise Solution SIZE = 4 def bar(): print "#" + 4 * SIZE * "=" + "#" def top(): for line in range(1, SIZE + 1): # split a long line by ending it with \ print "|" + (-2 * line + 2 * SIZE) * " " + \ "<>" + (4 * line - 4) * "." + "<>" + \ (-2 * line + 2 * SIZE) * " " + "|" def bottom(): for line in range(SIZE, 0, -1): print "|" + (-2 * line + 2 * SIZE) * " " + \ "<>" + (4 * line - 4) * "." + "<>" + \ (-2 * line + 2 * SIZE) * " " + "|" # main bar() top() bottom() bar()

14 Concatenating Ranges Ranges can be concatenated with + –Can be used to loop over a disjoint range of numbers >>> range(1, 5) + range(10, 15) [1, 2, 3, 4, 10, 11, 12, 13, 14] >>> for i in range(4) + range(10, 7, -1):... print i

15 Exercise Solution 2 SIZE = 4 def bar(): print "#" + 4 * SIZE * "=" + "#" def mirror (): for line in range(1, SIZE + 1) + range(SIZE, 0, -1) : print "|" + (-2 * line + 2 * SIZE) * " " + \ "<>" + (4 * line - 4) * "." + "<>" + \ (-2 * line + 2 * SIZE) * " " + "|" # main bar() mirror() bar()