More elements of Python programs

Slides:



Advertisements
Similar presentations
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved Floating-point Numbers.
Advertisements

Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Chapter 2 Writing Simple Programs
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Data Types. Every program must deal with data The data is usually described as a certain type This type determines what you can do with the data and how.
Chapter 3 Computing with Numbers
Computing with Numbers CSC 161: The Art of Programming Prof. Henry Kautz 9/14/2009.
Munster Programming Training
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Homework: p odd, odd, odd, odd, and odd.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Round-off Errors and Computer Arithmetic. The arithmetic performed by a calculator or computer is different from the arithmetic in algebra and calculus.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
CHAPTER 5: Representing Numerical Data
CSCI206 - Computer Organization & Programming
Introduction to Numerical Analysis I
Chapter 2 Writing Simple Programs
Computer Science 210 Computer Organization
Topics Designing a Program Input, Processing, and Output
Python: Experiencing IDLE, writing simple programs
Introduction To Computer Science
Chapter 6: Data Types Lectures # 10.
Thinking about programming
William Stallings Computer Organization and Architecture 7th Edition
Data Structures Mohammed Thajeel To the second year students
Topic 3d Representation of Real Numbers
CSCI206 - Computer Organization & Programming
Arithmetic operations, decisions and looping
Introduction to Abstract Data Types
Bryan Burlingame 03 October 2018
More about Numerical Computation
Lecture 2 Python Programming & Data Types
Computer Science 210 Computer Organization
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Approximations and Round-Off Errors Chapter 3
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
C++ Data Types Data Type
Decision Structures and Indefinite Loops
Introduction to Dictionaries
Variables and Expressions
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
Differences between Java and C
Homework #5 — Monte Carlo Simulation
Lecture 2 Python Programming & Data Types
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Elements of a Python Program
More About Functions Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Variables, Lists, and Objects
Debuggers and Debugging
Simple Graphics Package
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Notes on pyplot Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
CSV files Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Note on Program Design Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Notes on Homework #6 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Representation of real numbers
Topic 3d Representation of Real Numbers
Numpy, pylab, matplotlib (follow-up)
Class code for pythonroom.com cchsp2cs
Assignment 1.2 Irrational and Rational Numbers
Presentation transcript:

More elements of Python programs CS-1004, A-Term 2014 More elements of Python programs

More Elements of Python Programs Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute) CS-1004, A-Term 2014 More elements of Python programs

Questions on Homework #1? Lab #1? What if you and someone else get different answers with the same input data? How do you find the problem? If you don’t have someone to compare it with, how do you know that your program is producing a correct or useful answer? CS-1004, A-Term 2014 More elements of Python programs

More elements of Python programs Reading Assignment All of Chapter 2 of textbook “Steps in Program Design” Very important No useful examples in course, yet Important habit to get into A little bit like washing hands before a meal! Names and keywords (i.e., reserved words) More about print() function Optional parameter at end Specified with “keyword parameter” More about input statements Need to use eval() for string to numerical data Interesting extension to assignment A, B = expression1, expression2 CS-1004, A-Term 2014 More elements of Python programs

More elements of Python programs Definite Loop What does a definite loop look like? (Lab #1) How would you explain it to a friend not yet in this course? Explain range(10) What is range? An identifier? A keyword? What? Answer:– a built-in Python function for generating a sequence of integers on the fly Can we see a “range”? Yes, use the “list” function Another form of range? range(start, stop) range(start, stop, step) Includes start but not stop! CS-1004, A-Term 2014 More elements of Python programs

More elements of Python programs Back to the for-loop Describe the behavior of a for-loop Can we make a for-loop with a list? Give example Note: need to make a loop with a list in HW #2 CS-1004, A-Term 2014 More elements of Python programs

More elements of Python programs Questions? CS-1004, A-Term 2014 More elements of Python programs

More elements of Python programs What’s in a number? Three types of numbers in real life:– Integers Rational numbers Irrational numbers What are they used for? How many? Can you count them? How defined? Incorrectly called “real numbers”! CS-1004, A-Term 2014 More elements of Python programs

Disgression — Counting the Rationals CS-1004, A-Term 2014 More elements of Python programs

What’s in a number? (continued) Rational numbers (continued) … What about decimal numbers? What about scientific notation? Irrational numbers? How to describe them? Can you give examples? How many are there? Can you count them? CS-1004, A-Term 2014 More elements of Python programs

Numbers in Computers — Integers Finite precision Usually 32- or 64 bits (these days) “Clock” arithmetic Wraps around Nasty examples:– 30,000 × 30,000 = 900,000,000 40,000 × 40,000 = 1,600,000,000 50,000 × 50,000 = -352,516,352 (!!) One week of CS-2011 devoted to this topic! Nearly every computer language uses internal representation of integers Subject to this problem Except Python! CS-1004, A-Term 2014 More elements of Python programs

More elements of Python programs Questions? CS-1004, A-Term 2014 More elements of Python programs

Numbers in Computers — Floating Point Used to approximate rationals and irrationals A little bit like scientific notation, but in binary Fraction part f:– 1.0 <= f < 2.0 Exponent part: 2e, where e can be positive or negative Different levels of precision 32-bit, 64-bit, 80-bit, 128-bit, etc. A lot of mathematical theory behind Mathematical calculations Rounding off results IEEE standard So that we get same answers on different computers! Python uses the internal floating point arithmetic hardware and representation CS-1004, A-Term 2014 More elements of Python programs

Data types in Python Programs Integers Floats Strings Lists <other stuff deferred to later> CS-1004, A-Term 2014 More elements of Python programs

More elements of Python programs Integers Integer operations +, -, *, // Take integers, yield integer results / Take integers, yield float Combination of integer and float? Integers get “promoted” to floats float() function Forces value to float Note: range() generates sequences of integers CS-1004, A-Term 2014 More elements of Python programs

More elements of Python programs Floats Same operations as integers Yield floats round(f) Rounds a float to a desired precision  float Rounds xx.5 to even direction! int(f) Forces a float to an integer Truncates fraction part type(variable | expression) Reports the type of the variable or value CS-1004, A-Term 2014 More elements of Python programs

More elements of Python programs Variables Every variable in Python “knows” its own type May change as a result of subsequent assignments Examples Unlike other programming languages Type of a variable is fixed at time program is written! CS-1004, A-Term 2014 More elements of Python programs

More elements of Python programs Questions? CS-1004, A-Term 2014 More elements of Python programs

More elements of Python programs Lists A collection of values enclosed in square brackets, separated by commas [1, 2, 3, 5, 7, 11, 13, 17, 19, 23] May be all the same type Examples May be used as control of for-loop for i in [1, 2, 3, 5, 7, 11, 13, 17, 19, 23]: May be assigned to variables May be passed as arguments to functions And results! Needed for HW #2 CS-1004, A-Term 2014 More elements of Python programs

More elements of Python programs Questions? CS-1004, A-Term 2014 More elements of Python programs