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