CS 112: Intro to Comp Prog

Similar documents
CS 112: Intro to Comp Prog

CS 112: Intro to Comp Prog

CS 112: Intro to Comp Prog

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Ex: If you use a program to record sales, you will want to remember data:

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Python Programming Exercises 1

MIT AITI Python Software Development

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Dublin Unified School District Suggested Pacing Guide for Grade 2 Text: Scott Foresman-Addison Wesley envision Math

Lecture 2: Operations and Data Types

CEN 414 Java Programming

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

Values, Variables, Types & Arithmetic Expressions. Agenda

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Full file at

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Chapter 2: Data and Expressions

Variables, Expressions, and Statements

CSE 115. Introduction to Computer Science I

Variable and Data Type I

cs1114 REVIEW of details test closed laptop period

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

COMP Primitive and Class Types. Yi Hong May 14, 2015

Expressions and Variables

ENGR (Socolofsky) Week 02 Python scripts

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Chapter 7 Arithmetic

KNOWLEDGE OF NUMBER SENSE, CONCEPTS, AND OPERATIONS

Module 1: Types and Expressions

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

pset1: C Tommy MacWilliam Grading Getting Started pset1: C Style Pennies Greedy Chart September 11, 2011

PROGRAMMING FUNDAMENTALS

Introduction to: Computers & Programming: Review prior to 1 st Midterm

ROCHESTER COMMUNITY SCHOOL MATHEMATICS SCOPE AND SEQUENCE, K-5 STRAND: NUMERATION

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

1/11/2010 Topic 2: Introduction to Programming 1 1

CSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016

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

Decisions, Decisions. Testing, testing C H A P T E R 7

Gateway Regional School District VERTICAL ALIGNMENT OF MATHEMATICS STANDARDS Grades 3-6

Python Numbers. Learning Outcomes 9/19/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17

VARIABLES & ASSIGNMENTS

REVIEW. The C++ Programming Language. CS 151 Review #2

Full file at

MODULE 02: BASIC COMPUTATION IN JAVA

Variables, expressions and statements

Algorithms and Programming I. Lecture#12 Spring 2015

Variable and Data Type I

Diocese of Boise Math Curriculum 5 th grade

Python Input, output and variables

Introduction to Computer Programming for Non-Majors

Gateway Regional School District VERTICAL ARTICULATION OF MATHEMATICS STANDARDS Grades K-4

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

Introduction to Computer Programming for Non-Majors

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

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CS Kangmei Yang. Page 1

Topic 2: Decimals. Topic 1 Integers. Topic 2 Decimals. Topic 3 Fractions. Topic 4 Ratios. Topic 5 Percentages. Topic 6 Algebra

CS1 Lecture 5 Jan. 25, 2019

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CMSC 201 Computer Science I for Majors

Topic 2: Introduction to Programming

Instructions PLEASE READ (notice bold and underlined phrases)

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

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Lecture 1. Course Overview, Python Basics

Math 7 Notes Unit Three: Applying Rational Numbers

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Problem Solving for Intro to Computer Science

Lessons on Python Numbers

Chapter 2. Elementary Programming

DATA TYPES AND EXPRESSIONS

CMPT 120 Control Structures in Python. Summer 2012 Instructor: Hassan Khosravi

Lecture 1. Course Overview, Python Basics

Introduction to Computer Programming for Non-Majors

Introduction to Programming in Turing. Input, Output, and Variables

Outline. Data and Operations. Data Types. Integral Types

Introduction to Python Programming

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

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

Getting Started Values, Expressions, and Statements CS GMU

CS313D: ADVANCED PROGRAMMING LANGUAGE

The source code for this lab must be submitted in a file named lab4.py. The source code file must contain a file header formatted as in previous labs.

ENGR 102 Engineering Lab I - Computation

printf( Please enter another number: ); scanf( %d, &num2);

Information Science 1

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Objectives. In this chapter, you will:

Prerequisites: Read all chapters through Chapter 4 in the textbook before attempting this lab. Read through this entire assignment before you begin.

CS 2316 Individual Homework 1 Python Practice Due: Wednesday, August 28th, before 11:55 PM Out of 100 points

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Transcription:

CS 112: Intro to Comp Prog Lecture Review Data Types String Operations Arithmetic Operators Variables In-Class Exercises Lab Assignment #2 Upcoming

Lecture Topics Variables * Types Functions Purpose Parameters Return Design Process ** * Will see in more depth, on Slide 6 ** Will see in more depth, on Slide 8

Data Types <<Type>> <<conversion function>> Integer int () (aka Fixed-Point) { -1, 0, 7, 3, -5,...} Floating-Point float() (aka Decimal) {-0.25, 12.41, -0.1, 0, 5.0,...} String str() { Hello World, CS 112 Rocks!, Pineapple,...} Anything surrounded by quotes is considered a String

String Operations + and * are operators with Strings (both sides must be Strings)...they can be used to combine or repeat a string + concatenation operator * repetition operator print Hi There, + John >>> Hi There, John print Beetlejuice * 3 >>> Beetlejuice Beetlejuice Beetlejuice print spider- + man >>> spider-man print what is + happening + my + friend? >>> what is happening my friend?

Arithmetic Operators Mathematical operators exists for both floats and ints + addition - subtraction * multiplication / division (for version less than 3: 1/2 = 0, v3+: 1/2 = 0.5) % modulus (returns the remainder) (Version <3.0) When both dividend and divisor are integers then the result must also be an integer (one way to think of this is you do division normally and just hack off the decimals...no rounding) 8/3 = 2.66666667 cut off the decimal part and it is just 2 3/4 = 0.75 cut off the decimal part and it is just 0 Modulus is what is left over (Lets say I want groups of 4 out of 23 students, how many full groups do I have, and what is the size of the group that is not 4)...if we do 23 % 4 this will result in 3.

Variables Variables denote blocks in memory that allow us (the programmers) to store and recall information at any point in the program. In order to create a variable we will need to specify a name and assign a value into it (whether it be text, numbers, etc.) this data specifies the variable's type. x = 5 - variable x is being created and assigned the integer value: 5 text = hello - variable text is being created and assigned the string value: hello data = 8.1 - variable data is being created and assigned the floating-point value: 8.1

Naming Variables Variable Names can only contain numbers, letters, and underscores Variable Name cannot start with a number Use meaningful names. Good Names first_name fifteenper bill LastName amt meters Bad Names (invalid & poorly named) 1stName fhdjsghg name#1 feet&inches purplepeopleeater

A Top-Down Approach We can use a Top-Down approach to designing our program, these approach helps us keep the goal of the program in mind and not lose the forest for the trees Top-Down means looking from the big picture and breaking it down Example: An iron crowbar -> iron elements -> atoms -> protons, neutron, electrons ->... Thus we can identify a broad set of steps needed to accomplish our goal. These steps can be broken down into smaller stages and eventually into code

Program Planning Below is a series of steps you should go through either in your head or on paper to help you code your program. 1) Identify the Main Steps (Parts)...later make these into separate functions 2) Identify the Variables (Information) needed for each part or the entire program 3) Begin writing pseudo-code for each of steps 4) Revision (may take several revisions) 5) Convert pseudo-code into Python 6) Debug your code

Programming Examples Write a program that will: 1. Ask the user for two numbers 2. Add the two numbers and output the result Write a program that will: 1. Ask the user for a dollar amount (for now assume <$1.00) 2. Calculate the optimal number of quarters, dimes, nickels, and pennies to make the change amount. Hint: Convert the dollar amount into number of pennies first and think of everything in terms of pennies...e.g. A quarter is 25 pennies. Think how modulus will work here 3. Output the results ** Take a moment to think how to solve these, we will go over them in a minute. **

Summation Program def main(): # the raw_input reads in user input as a string, so it must be # converted to an int value using the conversion function # it is then stored in a new variable x x = int(raw_input("enter the first number : ")) # same as above except that we read in a second number storing # the value into a new variable y y = int(raw_input("enter the second number : ")) # using the addition operator add the two values stored, and # store the result into a new variable z z = x + y # in order to combine the string with the value in z, # z must be converted to a string using the conversion function print "The sum of two numbers is " + str(z) # nice little pause to end raw_input("\npress Enter to Exit") main() # tell your program to run the function we just defined

Change Program def main(): change = float(raw_input("enter the dollar amount: $")) pennies = int(change * 100) #need to cast to int as you can't have part of a penny quarters = pennies / 25 #find the number of full quarters (again why we needed #as pennies as int) pennies = pennies % 25 #set pennies to be the left over pennies, that do not #make up part of the quarters # repeat the above process for each coin #... dimes = pennies / 10 pennies = pennies % 10 nickels = pennies / 5 pennies = pennies % 5 #We do not need to divide by one since it will just be itself, so we are done with #the calculation steps #Outputting in a pleasant way (my decision...could do it any way) print "$"+str(change)+" is " print "\t",quarters, "quarters," print "\t",dimes, "dimes," print "\t",nickels, "nickels," print "\t",pennies, "pennies" main() #Execute the defined function

Assignment #2 Due next week before lab You need to turn in one file Lab2.py Contains the proper comment header Reads input from the user Does the proper conversion as outlined in the pdf Outputs the result as shown in the example output (must cut-off decimal points...using either the conversion functions or the round function see last slide) Again make sure to verify that everything was submitted

Tipper Program FOR YOU: Write a Tipper program where the user enters a restaurant bill total. The program should then display two amounts: a 15-percent tip and a 20-percent tip: Example Execution (Bolded and Italicized is user input) >>> tip.py Enter the restaurant bill total: 32.78 15-Percent: 4.917, 20-Percent: 6.556

Upcoming Branching, WhileLoops, and Program Planning Finish Lab Assignment #2 and be sure to complete it before next week's lab Read Chapter 3 in the textbook

Rounding Functions to truncate numeric data: math.ceil(<<number>>) math.floor(<<number>>) round(<<number>> [, <<places>>]) int() import math def main(): x = 3.2 y = 4.6 print "Original:\tx=",x,"\ty=",y print "ceil():\t x=",math.ceil(x),"\ty=",math.ceil(y) print "floor():\t x=",math.floor(x),"\ty=",math.floor(y) print "round():\t x=",round(x),"\ty=",round(y) print "int():\t x=",int(x)," \ty=",int(y) main() >>> Original: x= 3.2 y= 4.6 ceil(): x= 4.0 y= 5.0 floor(): x= 3.0 y= 4.0 round(): x= 3.0 y= 5.0 int(): x= 3 y= 4 >>>