Week 2. expressions, variables, for loops

Similar documents
Unit 3. parameters and graphics

Python! Created in 1991 by Guido van Rossum (now at Google)

10/9/07. < Moo? > \ ^ ^ \ (oo)\ ( )\ )\/\ ----w

Week 4. Strings, if/else, return, user input

Building Java Programs

Topic 6 loops, figures, constants

Topic 6 Nested for Loops

Repetition with for loops

CSc 110, Spring 2017 Lecture 3: Expressions, Variables and Loops. Adapted from slides by Marty Stepp and Stuart Reges

Exceptions. raise type(message) raise Exception(message)

Building Java Programs Chapter 2

Week 2. Classes and Objects

Recap: Assignment as an Operator CS 112 Introduction to Programming

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Week 3. parameters, return, math, graphics

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

CS 112 Introduction to Programming

Primitive data, expressions, and variables

Topic 4 Expressions and variables

ENGR 101 Engineering Design Workshop

Lecture 1. basic Python programs, defining functions

Python for C programmers

Building Java Programs

CSE 115. Introduction to Computer Science I

Unit 6. File processing

Building Java Programs

Getting Started Values, Expressions, and Statements CS GMU

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Exceptions. raise type(message) raise Exception(message)

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

Lecture 2: Variables & Assignments

Python memento TI-Smart Grids

ENGR (Socolofsky) Week 02 Python scripts

Python Input, output and variables

Computing with Strings. Learning Outcomes. Python s String Type 9/23/2012

CSc 110, Autumn Lecture 30: Methods. Adapted from slides by Marty Stepp and Stuart Reges

Lessons on Python Numbers

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Lecture 1. Types, Expressions, & Variables

Computer Science II (20082) Week 1: Review and Inheritance

Object Oriented Programming. Java-Lecture 1

Topic 5 for loops and nested loops

Variable and Data Type I

Building Java Programs

CSCI 121: Anatomy of a Python Script

cs1114 REVIEW of details test closed laptop period

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Building Java Programs

Getting started with Java

QUIZ: What value is stored in a after this

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Lecture 16: Static Semantics Overview 1

Building Java Programs

Building Java Programs

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Python language: Basics

Python Programming Exercises 1

CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random. Adapted from slides by Marty Stepp and Stuart Reges

Getting Started with Python

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Introduction to Programming Using Java (98-388)

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

Datatypes, Variables, and Operations

A simple interpreted language

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Lists, loops and decisions

Types, lists & functions

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Operators in java Operator operands.

CSCA08 Winter 2018 Week 2: Variables & Functions. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

Tutorial 06. Conditional statement: if then, if else, switch

Lecture 13: Two- Dimensional Arrays

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

JAVA OPERATORS GENERAL

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

CSI33 Data Structures

Elementary Programming

Lecture 2: Operations and Data Types

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Basics of Java Programming variables, assignment, and input

Fundamentals of Programming (Python) Getting Started with Programming

Here n is a variable name. The value of that variable is 176.

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Introduction to Python (All the Basic Stuff)

Data Structure and Programming Languages

Fall 2017 CISC124 9/16/2017

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Motivation was to facilitate development of systems software, especially OS development.

CPS122 Lecture: From Python to Java

CSc 110, Autumn Lecture 11: Strings. Adapted from slides by Marty Stepp and Stuart Reges

Chapter Legal intliterals: 22, 1, and d Results of intexpressions:

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Algorithms and Programming I. Lecture#12 Spring 2015

Transcription:

Week 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: http://creativecommons.org/licenses/by-nc-sa/3.0

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

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

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

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 Value Java type Python type 4 int int 3.14 double float "ni!" String str 5

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 yo yo yo yo yo >>> print * 3 * "4" 444444 6

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. 7

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

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(, 6):... print i 3 4 5 >>> for i in range(15, 0, -5):... print i 15 10 5 9

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

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 1 3 4 5 6 7 8 9 10 11 1 13 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 11

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

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 " " + (- * line + * SIZE) * " " + \ "<>" + (4 * line - 4) * "." + "<>" + \ (- * line + * SIZE) * " " + " " def bottom(): for line in range(size, 0, -1): print " " + (- * line + * SIZE) * " " + \ "<>" + (4 * line - 4) * "." + "<>" + \ (- * line + * SIZE) * " " + " " # main bar() top() bottom() bar() 13

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,, 3, 4, 10, 11, 1, 13, 14] >>> for i in range(4) + range(10, 7, -1):... print i 0 1 3 10 9 8 14

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