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

Similar documents
Python Programming: An Introduction to Computer Science

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Writing Python Programs, Numerical Data Types

C++ Programming Lecture 11 Functions Part I

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

Introduction to Programming

MYSQL NUMERIC FUNCTIONS

Introduction to Engineering gii

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Product Price Formula extension for Magento2. User Guide

Functions and Inverses ID1050 Quantitative & Qualitative Reasoning

Maths Functions User Manual

Goals for This Lecture:

Introduction to MATLAB

Part V Appendices c Copyright, Todd Young and Martin Mohlenkamp, Department of Mathematics, Ohio University, 2017

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

CITS 1401 Problem Solving & Programming

Python Programming: An Introduction to Computer Science

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

CT 229 Java Syntax Continued

Chapter 2. Outline. Simple C++ Programs

Introduction to Python, Cplex and Gurobi

TECH TIP VISION Calibration and Data Acquisition Software

Chapter 4: Basic C Operators

Variable and Data Type 2

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

Introduction to Matlab

Methods CSC 121 Fall 2014 Howard Rosenthal

C++ Overview. Chapter 1. Chapter 2

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Beginning C Programming for Engineers

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Built-in Types of Data

Sketchify Tutorial Properties and Variables. sketchify.sf.net Željko Obrenović

Chapter 3 Mathematical Functions, Strings, and Objects

4. Modules and Functions

The Number object. to set specific number types (like integer, short, In JavaScript all numbers are 64bit floating point

Intrinsic Functions Outline

Introduction to Computer Programming for Non-Majors

Standard Library Functions Outline

Introduction to Computer Programming for Non-Majors

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Fundamentals of Programming & Procedural Programming

Comp 151. More on Arithmetic and intro to Objects

Introduction to Computer Programming for Non-Majors

Arithmetic. 2.2.l Basic Arithmetic Operations. 2.2 Arithmetic 37

Summary of basic C++-commands

Introduction to MATLAB 7 for Engineers

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

3.1. Chapter 3: The cin Object. Expressions and Interactivity

ECET 264 C Programming Language with Applications

The Expressions plugin PRINTED MANUAL

Chapter 1 Introduction to MATLAB

9 Using Equation Networks

Consider this m file that creates a file that you can load data into called rain.txt

Custom Variables (Virtual Variables)

CHAPTER 3: CORE PROGRAMMING ELEMENTS

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Methods CSC 121 Fall 2016 Howard Rosenthal

Using Game Maker 8: GML Scripting

Basic types and definitions. Chapter 3 of Thompson

Welcome. Please Sign-In

Operators Functions Order of Operations Mixed Mode Arithmetic VOID Data. Syntax and type conventions Using the Script window interface

Julia Calculator ( Introduction)

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

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Data Types and Basic Calculation

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Engineering Problem Solving with C++, Etter/Ingber

The Expressions plugin PRINTED MANUAL

EP375 Computational Physics

USER-DEFINED ELEMENT IN ZMAN TM

Matlab Programming Introduction 1 2

Computer Science & Engineering 150A Problem Solving Using Computers

Javascript Bignum Extensions

Chapter 3. built in functions help feature elementary math functions data analysis functions random number functions computational limits

CS1010E Lecture 3 Simple C Programs Part 2

Programming in MATLAB

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Methods CSC 121 Spring 2017 Howard Rosenthal

What is Matlab? The command line Variables Operators Functions

MATHEMATICAL / NUMERICAL FUNCTIONS

ANSI C Programming Simple Programs

Hive Wavetables. Introduction 2. Concepts 3 Some Terminology 3. Commands 4

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

Operations. Making Things Happen

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

Downloaded from Chapter 2. Functions

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Structured programming

Introduction to MATLAB. Computational Probability and Statistics CIS 2033 Section 003

Comp 151. More on Arithmetic and number types

APPENDIX P. Derived Parameter Specification

Telemetry Standards, IRIG Standard (Part 1), Appendix P, June 2011 APPENDIX P DERIVED PARAMETER SPECIFICATION. Paragraph Title Page

10 Using the PCFL Editor In this chapter

>>> * *(25**0.16) *10*(25**0.16)

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

1001ICT Introduction To Programming Lecture Notes

MobileCoach ISRII Handout

Transcription:

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

3.1 Numberic Data Types When computers were first developed, they were seen primarily as number crunchers. data - is the information that is stored and manipulated by computer programs Python built-in operations on numbers + addition a+b - subtraction a-b * multiplication a*b / division a/b = a b 2 ** exponentiation 2**3 = 2 3 = 8 % remainder 12 % 5 = 2 abs() absolute value abs(-23.4) = 23.4 // integer remainder 12//5 = 2

3.1 Numberic Data Types Different kinds of data will be stored and manipulated in different ways. Integers :..., -3, -2, -1, 0, 1, 2, 3,... integer data type (int) decimals (numbers with fractional part): 0.123, -4.345 floating point data type (float) try to input the following commands in Python shell: >>> type(23) The function type returns us >>>type(1.23) the type of the value. >>>type(2.0) >>>my_number=23.1 >>>type(my_number) 3

3.1 Numberic Data Types Different kinds of data will be stored and manipulated in different ways. Integers :..., -3, -2, -1, 0, 1, 2, 3,... integer data type (int) decimals (numbers with fractional part): 0.123, -4.345 floating point data type (float) try to input the following commands in Python shell: >>> type(23) The function type returns us >>>type(1.23) the type of the value. >>>type(2.0) >>>my_number=23.1 >>>type(my_number) 4! float type stores only approximations to real numbers; there is a limit to the precision, or accuracy

3.2 Type conversions and rounding There are situations where a value may need to be converted from one data type to another. Try in the Python interpreter: >>> int(4.5) 4 >>> float(3) 3.0 >>> float(int(4.5)) 4.0 5

3.2 Type conversions and rounding There are situations where a value may need to be converted from one data type to another. Try in the Python interpreter: >>> int(4.5) 4 >>> float(3) 3.0 >>> float(int(4.5)) 4.0 note that the decimal part is simply cut off. 6

3.2 Type conversions and rounding There are situations where a value may need to be converted from one data type to another. Try in the Python interpreter: >>> int(4.5) 4 >>> float(3) 3.0 >>> float(int(4.5)) 4.0 >>> float("4.56") 4.56 >>> int("456") 456 note that the decimal part is simply cut off. 7

3.2 Type conversions and rounding There are situations where a value may need to be converted from one data type to another. Try in the Python interpreter: >>> int(4.5) 4 >>> float(3) 3.0 >>> float(int(4.5)) 4.0 >>> float("4.56") 4.56 >>> int("456") 456 note that the decimal part is simply cut off. this is what we can use instead of eval at input 8

3.2 Type conversions and rounding Consider the following code: def main(): x=float(input("enter a decimal number:")) print("you entered: ",x) y = int(input("enter an integer:")) print("you entered:",y) main() 9

3.2 Type conversions and rounding Consider the following code: def main(): x=float(input("enter a decimal number:")) print("you entered: ",x) y = int(input("enter an integer:")) print("you entered:",y) main() First run: Enter a decimal number:5.6 You entered: 5.6 Enter an integer:8 You entered: 8 10

3.2 Type conversions and rounding Second run: Enter a decimal number:4.5 You entered: 4.5 Enter an integer:4.5 Traceback (most recent call last): File "C:/Users/Natasha/Google Drive/Web- pages/mine/english/teaching/csi31- materials/lecture05/example.py", line 10, in <module> main() File "C:/Users/Natasha/Google Drive/Web- pages/mine/english/teaching/csi31- materials/lecture05/example.py", line 7, in main y = int(input("enter an integer:")) ValueError: invalid literal for int() with base 10: '4.5' 11

3.2 Type conversions and rounding In addition, numeric type conversion in place of eval does not accommodate simultaneous input. >>> x,y=float(input("enter two decimal values:")) Enter two decimal values:5.6,7.5 Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> x,y=float(input("enter two decimal values:")) ValueError: could not convert string to float: '5.6,7.5' This is a small price to pay for added security 12

3.2 Type conversions and rounding To round off use round method. round(number[,ndigits]) number Try the following in the Python interpreter: >>> round(4.456,2) 4.46 >>> round(4.456,1) 4.5 >>> round(123.78476) 124 13

3.3 Using the Math Library Python provides many other useful mathematical operations in a special math library A library - is a module that contains some useful definitions of functions. In order to use functions from the library we need to include it or import it to our program: import math 14

15 Python mathematics English pi p An approximation of pi e e An approximation of e sqrt(x) x The square root of x sin(x) sin x The sine of x cos(x) cos x The cosine of x tan(x) tan x The tangent of x asin(x) arcsin x The inverse of sine of x acos(x) arccos x The inverse of cosine of x atan(x) arctan x The inverse of tangent of x log(x) ln x The natural logarithm of x log10(x) log 10 x The common logarithm of x exp(x) e x The exponential of x ceil(x) éxù Ceiling function of x floor(x) ëxû Floor function of x

3.3 Using the Math Library see table 3.2 on page 68 for some math library functions. see also Python Documentation -> The Python Standard Library -> Numeric and Mathematical Modules -> math input the following statements in the Python Interpreter: >>> import math >>> math.sqrt(5) >>> math.sqrt(25) >>> math.ceil(234.345) 16

3.3 Accumulating Results: Factorial factorial function: n! n! = n(n-1)(n-2)(n-3)... 3 2 1 = 1 2 3... (n-3)(n-2)(n-1)n Examples: 2! = 2 1=2 4! = 4 3 2 1 = 24 Let's write a program that calculates the factorial of a number entered by the user: 17

3.3 Accumulating Results: Factorial factorial function: n! n! = n(n-1)(n-2)(n-3)... 3 2 1 = 1 2 3... (n-3)(n-2)(n-1)n Input: a positive integer (n) Output: a positive integer (factorial) Relationship: factorial = n(n-1)(n-2)*...*2*1 18

3.3 Accumulating Results: Factorial factorial function: n! n! = n(n-1)(n-2)(n-3)... 3 2 1 = 1 2 3... (n-3)(n-2)(n-1)n Input: a positive integer (n) Output: a positive integer (factorial) Relationship: factorial = n(n-1)(n-2)*...*2*1 Algorithm: input number to take the factorial of, n for loop that will iterate n times factorial = factorial * factor output factorial 19 see the program: factorial.py