Computing with Numbers

Similar documents
Python Programming: An Introduction to Computer Science

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

CITS 1401 Problem Solving & Programming

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors

Python Programming: An Introduction to Computer Science

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

CEN 414 Java Programming

Binary Search. Roland Backhouse February 5th, 2001

Introduction to Computer Programming for Non-Majors

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Introduction to Python. Data Structures

CS102: Variables and Expressions

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

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Arithmetic and IO. 25 August 2017

CS 115 Lecture 13. Strings. Neil Moore. Department of Computer Science University of Kentucky Lexington, Kentucky

Full file at

CS 102 Lab 3 Fall 2012

Long (or LONGMATH ) floating-point (or integer) variables (length up to 1 million, limited by machine memory, range: approx. ±10 1,000,000.

Basic data types. Building blocks of computation

Introduction to: Computers & Programming: Strings and Other Sequences

Getting started with Java

Chapter 10: Strings and Hashtables

cs1114 REVIEW of details test closed laptop period

Control structure: Repetition - Part 1

Python Class-Lesson1 Instructor: Yao

Types and Expressions. Chapter 3

Last Time. integer/float/string. import math math.sqrt() math.pi. Python Programming, 2/e 2

CIS133J. Working with Numbers in Java

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Comp 151. More on Arithmetic and intro to Objects

Department of Mathematical Sciences, FAU. Instructions:

Chapter Two: Fundamental Data Types

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Strings, Lists, and Sequences

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Expressions and Casting

ECE 122 Engineering Problem Solving with Java

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

UNIT 5. String Functions and Random Numbers

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

Natural Numbers and Integers. Big Ideas in Numerical Methods. Overflow. Real Numbers 29/07/2011. Taking some ideas from NM course a little further

Chapter 2. Data Representation in Computer Systems

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

Math Day 2 Programming: How to make computers do math for you

Programming with Java

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Lists How lists are like strings

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Program Fundamentals

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Chapter 2 Elementary Programming

CS-201 Introduction to Programming with Java

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

The Practice of Computing Using PYTHON. Chapter 4. Working with Strings. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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

Operators and Expressions:

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Part I Basic Concepts 1

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

Python Input, output and variables

B.V. Patel Institute of BMC & IT, UTU 2014

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

Programming in C++ 5. Integral data types

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


COS 140: Foundations of Computer Science

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Introduction to Computer Science (I1100) With 1 coin 2 possibilities: Head / Tail or 0/1

COMP6700/2140 Data and Types

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Intro to Strings. CSE 231 Rich Enbody. String: a sequence of characters. Indicated with quotes: or " " 9/11/13

Introduction to: Computers & Programming: Strings and Other Sequences

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSI33 Data Structures

ISE 101 Introduction to Information Systems. Lecture 3 Objectives: While loops Strings

بسم اهلل الرمحن الرحيم

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

Python: common syntax

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

Introduction to Computer Programming for Non-Majors

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Comparing Data. Comparing Floating Point Values. Comparing Float Values. CS257 Computer Science I Kevin Sahr, PhD

COMP1730/COMP6730 Programming for Scientists. Strings

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

Advanced Algorithms and Computational Models (module A)

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

Expressions and Variables

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Python Programming: An Introduction to Computer Science

Transcription:

Computing with Numbers

Example output: Numeric Data Types

Numeric Data Types Whole numbers are represented using the integer data type (int for short).values of type int can be positive or negative whole numbers. Numbers that can have fractional parts are represented as floating point (or float) values. The data type of an object determines what values it can have and what operations can be performed on it. The float type only stores approximations. There is a limit to the precision, or accuracy, of the stored values. By contrast, the int type is exact.

Numeric Data Types Notice how operations on floats produce floats, and operations on ints produce ints.

Using the Math Library Python provides many other useful mathematical functions in a special math library. A library is just a module that contains some useful definitions. Example: find the roots of ax 2 +bx+c =0

Using the Math Library Python provides many other useful mathematical functions in a special math library. A library is just a module that contains some useful definitions.

Accumulating Results: Factorial In mathematics, factorial is often denoted with an exclamation (! ). The factorial of a whole number is defined as n!=n(n-1)(n-2) (1). This happens to be the number of distinct arrangements for n items. Given six items, we compute 6! =720 possible arrangements. Write a program that will compute the factorial of a number entered by the user. The basic outline of our program follows an Input-Process-Output pattern. Input number to take factorial of, n Compute factorial of n, fact Output fact Basic strategy: do repeated multiplications, use an accumulator variable + a loop structure Initialize the accumulator variable Loop until final result is reached update the value of accumulator variable

Accumulating Results: Factorial Initialize the accumulator variable Loop until final result is reached update the value of accumulator variable For example, suppose we want to calculate 5!=5*4*3*2*1 We define a variable and initialize it to be 1 fact = 1 for factor in [2,3,4,5] fact = fact * factor

Python range() function range(n): produce a sequence of numbers starting with 0 and continuing up to, but not including n range(start, n): produce a sequence of numbers starting with start and continuing up to, but not including n range(start, n, step): produce a sequence of numbers starting with start and continuing up to, but not including n, and using step as the increment between numbers Examples: >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(5,10) [5, 6, 7, 8, 9] >>> range(5,10,3) [5, 8]

Accumulating Results: Factorial n!=n(n-1)(n-2) (1). Write a program that will compute the factorial of a number entered by the user.

The limits of int (Note that in python 2.7 or later can handle this error by automatically changing the data type.)

Handling Large Numbers: Long Ints Python provides a better solution for large, exact values in the form of a third numeric type long int. A long int is not a fixed size, but expands to accommodate whatever value it holds. To get a long int, you put an L suffix on a numeric literal.

Accumulating Results: Factorial n!=n(n-1)(n-2) (1). Write a program that will compute the factorial of a number entered by the user.

Type Conversions Note that the value is truncated, not rounded when using int() or long()

Computing with Strings

The String Data Type A string is a sequence of characters

The String Data Type A string is a sequence of characters Remember that the input statement treats whatever the user types as an expression to be evaluated

Indexing of the String A string is a sequence of characters Individual characters can be accessed through the operation of indexing. The general form for indexing is <string>[<expr>].

A string is a sequence of characters Slicing of the String Access a contiguous sequence of characters or substring from a string is called slicing. The general form for slicing is <string>[<start>:<end>]. Both start and end should be int-valued expressions. A slice produces the substring starting at the position given by start and running up to, but not including, position end.

The string data type is immutable

Operations for putting strings together + concatenation * repetition

Summary of basic string operations

Example: single string processing

String Representation How does a computer represent strings? Each character is translated into a number, and the entire string is stored as a sequence of (binary) numbers in computer memory. It doesn t really matter what number is used to represent any given character as long as the computer is consistent about the encoding/decoding process. One important standard, called ASCII, uses the numbers 0 through 127 to represent the characters typically found on a computer keyboard. For example, the capital letters A Z are represented by the values 65 90, and the lowercase versions have codes 97 122. UniCode is an extended standard to include characters of other written languages

The String Library split - This function is used to split a string into a sequence of substrings. By default, it will split the string wherever a space occurs

The String Library