10/9/07 ______ < Moo? > \ ^__^ \ (oo)\_______ (__)\ )\/\

Slides:



Advertisements
Similar presentations
2/7/2008. >>> Overview * boolean * while * random * tuples.
Advertisements

Nested for loops Cont’d. 2 Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? –Real graphics require a.
Expressions, variables, and for loops (oh my!) spam, spam, spam, spam
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.
Building Java Programs
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Course Lectures Available on line:
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Week 4. >>> Overview * if else * returns * input.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Chapter 2: Java Fundamentals
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
Unit 2 Expressions and variables; for loops Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
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.
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.
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.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
1/10/2008. >>> About Us Paul Beck * Third quarter TA * Computer Engineering * Ryan Tucker * Second quarter TA * Computer.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
10/9/07 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
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.
8/2/07. >>> About Me Scott Shawcroft * Junior * Computer Engineering * Third Quarter TA * Creative Commons Intern * Small-time Open Source Developer
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Intro to Programming STARS College of Communication and Information Florida State University Written by: Hannah Brock Alissa Ovalle Nicolaus Lopez Martin.
10/30/07. >>> Overview * boolean * randomness * while * tuples.
10/30/07.
10/23/07. >>> Overview * if else * returns * input.
10/16/07.
Lecture 5: Some more Java!
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
basic Python programs, defining functions
OUTPUT STATEMENTS GC 201.
basic Python programs, defining functions
An Introduction to Java – Part I, language basics
expressions, variables, for loops
expressions, variables, for loops
Building Java Programs
Chapter 2: Java Fundamentals
CSc 110, Spring 2018 Lecture 7: input and Constants
Building Java Programs
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
Variables, Types, Operations on Numbers
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
Topic 6 loops, figures, constants
Building Java Programs
Building Java Programs
Building Java Programs
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Building Java Programs
Suggested self-checks:
Building Java Programs
Drawing complex figures
Unit 3: Variables in Java
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-3: Loop Figures and Constants reading:
Presentation transcript:

10/9/07 ______ < Moo? > ------ \ ^__^ \ (oo)\_______ (__)\ )\/\ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||

>>> Reminders * Code feedback for Pythony things only. * NOT logic errors that will also be in Java code. _________________________________ / Could someone help me reboot my \ \ spaceship? / --------------------------------- \ ^__^ \ (==)\_______ (__)\ )\/\ ||----w | || ||

>>> Last Time * print * escape sequences * functions >>> print "a string" a string >>> print 'a string' >>> print #a string# >>> print $a string$ File "<stdin>", line 1 print $a string$ ^ SyntaxError: invalid syntax A whaa? function – a subroutine independent of other code. method – a subroutine associated with a class (think public class...) or object.

>>> Overview * types * variables * for loops * 1337 ASCII art _________ < Whoa... > --------- \ ^__^ \ (**)\_______ (__)\ )\/\ U ||----w | || ||

>>> Types Python cares very little about types. In Java, one must declare a variable with a particular type and maintain that type throughout the existance of that variable. In other words, ints can be only stored in places designated for ints, same for doubles etc. This is not the case in Python. Python does not care about types until the very last moment. This last moment is when values are used in certain ways, such as concatination. Java python 178 175.0 “wow” 'w' True int double String char boolean int float str bool >>> "Suppose " + 2 + " swallows carry it together." Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects >>> "Suppose " + str(2) + " swallows carry it together." 'Suppose 2 swallows carry it together.'

>>> Expressions Expressions.java expressions.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 4 * 3/8 + 2.5 * 2 (2.5 + 3.5)/2 26 % 10 % 4 * 3 9/4 * 2.0 – 5/4 (5 * 7.0/2 - 2.5)/5 * 2 3 * 4 + 2 * 3 12/7 * 4.4 * 2/4 177 % 100 % 10/2 "hello 34 " + 2 * 4 9/2.0 + 7/3 – 3.0/2 "2 + 2 " + 3 + 4 813 % 100/3 + 2.4 3 + 4 + " 2 + 2" 27/2/2.0 * (4.3 + 1.7) – 8/3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 4 * 3/8 + 2.5 * 2 (2.5 + 3.5)/2 26 % 10 % 4 * 3 9/4 * 2.0 - 5/4 (5 * 7.0/2 - 2.5)/5 * 2 3 * 4 + 2 * 3 12/7 * 4.4 * 2/4 177 % 100 % 10/2 "hello 34 " + str(2 * 4) 9/2.0 + 7/3 - 3.0/2 "2 + 2 " + str(3) + str(4) 813 % 100/3 + 2.4 str(3 + 4) + " 2 + 2" 27/2/2.0 * (4.3 + 1.7) – 8/3 “hello”*3+”wow” “hello”*2+” world”

>>> Variables As said earlier, Python cares less about types. This is the case when creating a variable. Unlike Java, Python does not care what type is in what variable. As a result, in Python to creating a variable is like Java without the type. expressions.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 x = 2 x += 1 print x x = x * 8 d = 3.0 d /= 2 print d s = “wow” print s Variables.java 1 2 3 4 5 6 7 8 9 10 11 ... int x = 2; x++; System.out.println(x); x = x * 8; double d = 3.0; d /= 2; System.out.println(d);

>>> Constants Continuing Python's free spirited ways, it has much less restrictions than Java. Because of this, constants are possible but not in a commonly used manner. Instead, we'll designate constants in Python solely by the variable capitalization. constants.py 1 2 3 SIZE = 2 x = 10 * SIZE print x

>>> Python's For Unlike Java's for loop, Python's for loop loops over elements in a sequence. To loop over a certain sequence of integers use the range() function. for.py 1 2 3 4 5 6 7 8 9 10 11 12 for i in range(4): # (end) print i for i in range(1,4): # (start,end) for i in range(2,8,2): # (start,end,step_size) # for <var> in <sequence>: # <statements> >>> range(4) [0, 1, 2, 3] >>> range(1,4) [1, 2, 3] >>> range(2,8,2) [2, 4, 6] >>> range(8,0,-2) [8, 6, 4, 2] A whaa? sequence – a series of elements. Ex: [1,2,3,4,5] is a sequence produced by range(1,6) Ex: [“milk”,”juice”,”bread”] is my grocery list

>>> Mirror scott @ yossarian ~ $ python mirror.py // Marty Stepp, CSE 142, Autumn 2007 // This program prints an ASCII text figure that // looks like a mirror. // This version uses a class constant to make the figure resizable. public class Mirror2 { public static final int SIZE = 4; // constant to change the figure size public static void main(String[] args) { line(); topHalf(); bottomHalf(); } // Prints the top half of the rounded mirror. public static void topHalf() { for (int line = 1; line <= SIZE; line++) { // pipe System.out.print("|"); // spaces for (int j = 1; j <= -2 * line + (2 * SIZE); j++) { System.out.print(" "); // <> System.out.print("<>"); // dots . for (int j = 1; j <= 4 * line - 4; j++) { System.out.print("."); System.out.println("|"); // Prints the bottom half of the rounded mirror. public static void bottomHalf() { for (int line = SIZE; line >= 1; line--) { // Prints the #==# line at the top and bottom of the mirror. public static void line() { System.out.print("#"); for (int j = 1; j <= 4 * SIZE; j++) { System.out.print("="); System.out.println("#"); scott @ yossarian ~ $ python mirror.py #================# | <><> | | <>....<> | | <>........<> | |<>............<>|

>>> Castle Arggh scott @ yossarian ~ $ python arggh.py _ _ _ _ _ _ _ _ | |_| |_| |_| | | |_| |_| |_| | \ / \ / \___________/ \___________/ | | __ __ __ __ __ __ __ __ __ __ __ | | | |_| |_| |_| |_| |_| |_| |_| |_| |_| |_| |_| |_| | | | | | | | ______ | | | | /||||||\ | | | | /||||||||\ | | | | |||||||||||| | | |_________|______________________||||||||||||______________________|_________|

© 2007 Scott Shawcroft, Some Rights Reserved Except where otherwise noted, this work is licensed under http://creativecommons.org/licenses/by-nc-sa/3.0 Python® and the Python logo are either a registered trademark or trademark of the Python Software Foundation. Java™ is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries.