12.07.2015 Views

2. Built-in Data Types

2. Built-in Data Types

2. Built-in Data Types

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Department of Computer ScienceDr. John S. Mallozzi<strong>2.</strong> <strong>Built</strong>-<strong>in</strong> <strong>Data</strong> <strong>Types</strong>Basic data concepts, Python built-<strong>in</strong>types, Arithmetic <strong>in</strong> Python, VariablesCopyright © 2010 by John S. Mallozzi


• Bit str<strong>in</strong>gs• Represent<strong>in</strong>g text• Represent<strong>in</strong>g other data• Represent<strong>in</strong>g numbers• Conversion2


• Each bit is either 0 or 1, so it can represent twoitems• A bit str<strong>in</strong>g is a “str<strong>in</strong>g of bits,” i.e., a list of bits,such as 11010101• Two bits can represent four items• There are four possibilities 00, 01, 10, 11• Three bits can represent eight items• Four bits can represent 16 items• Each additional bit doubles the number of items3


• You have to be able to represent any character(letter, digit, punctuation, special “signal<strong>in</strong>g”character, such as “end of a l<strong>in</strong>e” or “tab”)• Different character sets have been designed• ASCII: an 8-bit character set, OK for English andsome graphic characters• Unicode: a 16-bit set for “all” languages. This set<strong>in</strong>cludes all the ASCII characters• Exact representation doesn't matter – done bysoftware beh<strong>in</strong>d the scenes4


• <strong>Data</strong> such as sound (e.g., music), still pictures,or video, usually require many bits of storage• Example: each s<strong>in</strong>gle pixel <strong>in</strong> a picture requiresstorage of brightness and color <strong>in</strong>formation• For data requir<strong>in</strong>g too much storage,compression is used• Examples: MP3 format for music JPEG for pictures• In compressed file formats (<strong>in</strong>clud<strong>in</strong>g MP3 andJPEG), some <strong>in</strong>formation is lost5


• Numerals• Positional notation – base 10• Other bases• Digits• Names of numbers• Hexadecimal• Arithmetic• Other numbers6


• The symbol used to write a number is called anumeral• 23 is an Arabic numeral and XXIII is a Roman numeralfor the number we call “twenty-three”• Our standard (Arabic) numerals are positional: thebase 10 digits are use• Roman numerals are not positional – they do not usea base• Us<strong>in</strong>g a base makes possible algorithms fordo<strong>in</strong>g computations such as multiplication anddivision• You learned algorithms for multiply<strong>in</strong>g and divid<strong>in</strong>gArabic numerals• Try them with Roman numerals!7


• Example: 25042 1000 = 20005 100 = 500+ 0 10 = 0+ 4 1 = 4• Each position has a multiplier times a power of 102 5 0 41000position100position10position1position8


• Base 10 probably arose because…• Other bases can be used – any positive <strong>in</strong>tegerwould work• But it would take some gett<strong>in</strong>g used to!• Each position will be a power of the base• Example: <strong>in</strong> base 13, positions from right to leftrepresent 1, 13, 13 2 = 169, 13 3 = 2197• Example• If the numeral 642 is <strong>in</strong>terpreted <strong>in</strong> base 13, it means6 13 2 + 4 13 + 2 1• This is the number whose base 10 numeral is 1068• Base 2, or b<strong>in</strong>ary numerals, are best for digitalcomputation9


• You need as many digits as there are 1’s <strong>in</strong> thebase• Ten for base ten: 0,1,2,3,4,5,6,7,8,9• Eight for base eight: 0,1,2,3,4,5,6,7• Thirteen for base thirteen: 0,1,2,3,4,5,6,7,8,9 and threemore Customary to use letters: for base thirteen, A, B, and C• Example: to write the number whose decimalnumeral is 642 <strong>in</strong> base 13, we write 3A5• 3 13 2 + A 13 + 5 1 = 642 (10 is written A)10


• We are used to writ<strong>in</strong>g numbers <strong>in</strong> base 10• Even the names we use for numbers are basedon 10• “six hundred forty two”• This makes it hard to talk about other bases• Sometimes we write 642 10 and 642 13 for “thenumeral 642 <strong>in</strong>terpreted <strong>in</strong> base ten” and “thenumeral 642 <strong>in</strong>terpreted <strong>in</strong> base thirteen”• So 642 10 = 3A5 13 and 642 13 = 1068 10• Not for humans!11


• Decimal and b<strong>in</strong>ary are most important – the firstfor us, the second for the computer• When we want to look at the actual representation<strong>in</strong>side a computer, hexadecimal (base 16) is moreconvenient than b<strong>in</strong>ary• Hexadecimal notation allows us to replace each 4-bit bitstr<strong>in</strong>g by a s<strong>in</strong>gle symbol, by writ<strong>in</strong>g thehexadecimal equivalent of each 4-bit str<strong>in</strong>g• Example: 001110110001 2 = 3B1 160011 1011 00013 B 112


• The algorithms you learned for arithmetic may beused no matter what the base• Example: addition algorithm Add column by column, from right to left. If answer has morethan one digit, carry to next column Base 10 example:1 12 6 5+ 4 7 7742▫ Base 2 example:1 1 11 1 1 1 0 1+ 1 1 0 1 1 01 11001113


• To represent a negative number, you can usethe first bit for a sign• This is almost what is done <strong>in</strong> most computers• Leftmost bit is for the sign (0 for +, 1 for -)• But two’s complement notation is used• (Details <strong>in</strong> homework)• For decimals, float<strong>in</strong>g-po<strong>in</strong>t format is used: twonumbers are stored• Base• Mantissa• (Details later)14


• From another base to decimal is easy• What about the other way?• Here is an algorithm:• Set the dividend equal to the orig<strong>in</strong>al numeral• Repeat as long as the dividend is not zero Divide the dividend by the new base Remember the rema<strong>in</strong>der Set the dividend equal to the quotient• Form the numeral <strong>in</strong> the new base by writ<strong>in</strong>g therema<strong>in</strong>ders <strong>in</strong> reverse order• …but we don't have to do this – or understandwhy it works• (except <strong>in</strong> homework!)15


• Convert 44 to base 2• Dividend = 44. Dividend not 0.• Divide by 2: quotient = 22, rema<strong>in</strong>der = 0• Set dividend = 2<strong>2.</strong> Dividend not 0, so repeat.• Divide by 2: quotient = 11, rema<strong>in</strong>der = 0• Set dividend = 11. Dividend not 0, so repeat.• Divide by 2: quotient = 5, rema<strong>in</strong>der = 1• Set dividend = 5. Dividend not 0, so repeat.• Divide by 2: quotient = 2, rema<strong>in</strong>der = 1• Set dividend = <strong>2.</strong> Dividend not 0, so repeat.• Divide by 2: quotient = 1, rema<strong>in</strong>der = 0• Set dividend = 1. Dividend not 0, so repeat.• Divide by 2: quotient = 0, rema<strong>in</strong>der = 1• Set dividend = 0. Dividend is 0, so stop.• Base 2 numeral is 10110016


• Different k<strong>in</strong>ds of numbers• Python <strong>in</strong>teger types• Range of decimal numbers• Float<strong>in</strong>g-po<strong>in</strong>t numbers• Writ<strong>in</strong>g float<strong>in</strong>g-po<strong>in</strong>t numbers• Python float<strong>in</strong>g-po<strong>in</strong>t type• Other Python built-<strong>in</strong> types17


• Because numbers must be stored <strong>in</strong> a limitedamount of space, storage differs depend<strong>in</strong>g on• Whether the number is an <strong>in</strong>teger or a decimal• How large – and <strong>in</strong> the case of decimals, how precise– we require our represented numbers to be• Computer arithmetic dist<strong>in</strong>guishes among• Whole numbers (<strong>in</strong>tegers)• Float<strong>in</strong>g-po<strong>in</strong>t numbers Decimal numbers, but not the same as fixed-po<strong>in</strong>t numbers• Python tries to m<strong>in</strong>imize our need to worryabout these matters, but also to be efficient –which does require some <strong>in</strong>volvement18


• For efficiency, Python uses a size for <strong>in</strong>tegers that ismost efficient for the Python implementation• This restricts the size of both positive and negative <strong>in</strong>tegers thatcan be stored• The largest <strong>in</strong>teger <strong>in</strong> a typical implementation is 2,147,483,647,but you should not memorize this – it is not the same for everyimplementation, and, if you really need it, you can get its valuefor your implementation from the Python library, assys.max<strong>in</strong>t• However, Python allows us to avoid such concerns,us<strong>in</strong>g “unlimited” <strong>in</strong>teger range• When we write an expression, we can simply ignore thelimitation• When Python writes a result larger than the maximum <strong>in</strong>tegerit stores, it appends L (for “long”) to the result, rem<strong>in</strong>d<strong>in</strong>g usthat more storage, and time, is be<strong>in</strong>g used for this number19


• Scientists sometimes require numbers as small as0.00000000000000000000000000000000067(Planck’s Constant)• In other circumstances, a number like588,000,000,000,000,000.0(approximate size of Milky Way <strong>in</strong> miles) can beconsidered small• Number of decimal places needed tosimultaneously represent both these examples ismore than 50• Us<strong>in</strong>g float<strong>in</strong>g-po<strong>in</strong>t numbers, both can berepresented us<strong>in</strong>g three decimal places21


• Idea is to trade accuracy for range• Python must remember three th<strong>in</strong>gs about eachnumber• Sign (+ or -)• Significant digits Ignore lead<strong>in</strong>g and trail<strong>in</strong>g zeros Examples: 67 for Planck’s Constant and 588 for size of MilkyWay• Exponent Number of places to move the decimal po<strong>in</strong>t start<strong>in</strong>g fromthe position after the first significant digit, with sign<strong>in</strong>dicat<strong>in</strong>g direction to move (- for left, + for right) Examples: -34 for Planck’s Constant and +17 for size of MilkyWay22


• You have to express the three important parts:sign, significant digits, and exponent• Significant digits written with decimal afterfirst significant digit• This is called the mantissa of the number• Exponent written after the letter E• Examples• Planck’s Constant: 6.7E-34• Size of Milky Way: 5.88E17• Note: scientists write 6.7·10 -34 and 5.88·10 17 .This is scientific notation.• Easier to use E <strong>in</strong> typ<strong>in</strong>g, though – no superscripts23


• Python uses a storage scheme with very large range, soyou don’t have to worry about that• The covered range <strong>in</strong> a typical implementation is about -10 308 to+10 308• Although the range is large enough so you don’t haveto worry, the accuracy is limited to the extent that youdo have to be concerned• The number of significant digits <strong>in</strong> a typical implementation isabout 15• Bus<strong>in</strong>ess calculations, for example, may need to have greateraccuracy• Examples:24


• Boolean type for logic• Name of type is bool• Only values are True and False (note capitalization)• Python considers bool to be a numeric type, so nonsensearithmetic works (but should not be used)• Str<strong>in</strong>g type for text• Surround text with either ' or " quotes – Pythongenerally uses ' for display• More on this type later• There are types we won’t look at – complex, forexample – but we’ll work with some others later25


• Differences• Addition, subtraction, and multiplication• Division• Powers• The pr<strong>in</strong>t statement26


• Each answer must have a type (<strong>in</strong>t, long, float,str, …)• Sometimes conversion is automatic, but not always• Some new symbols must be used, s<strong>in</strong>ce standardarithmetical symbols aren’t available• Examples: 2 3, 2 3, 3 ÷ 4, ¾, 5• In Python, we use * for multiplication and / for division There are no built-<strong>in</strong> fractions but the library has a way to usethem – <strong>in</strong>stead use decimals We’ll talk about how to deal with roots later27


• For these operations, arithmetic is as usual,except for the symbols used and somesize/accuracy issues• Examples2*3 + 5*6 gives the answer 3664/2 – 5*7 gives the answer -3<strong>2.</strong>3 * 4.2 gives 9.6600000000000001(<strong>in</strong>accuracy after 15 th decimal place)2147483645 + 10 gives 2147483655L(the largest <strong>in</strong>t that can be stored is 2147483647, so thelast result is converted automatically to long)Note that you are not permitted to use commas28


• Even <strong>in</strong> ord<strong>in</strong>ary arithmetic, <strong>in</strong>teger division of<strong>in</strong>tegers differs from decimal division• Example: 11 ÷ 3 gives quotient 3 with rema<strong>in</strong>der 2 for<strong>in</strong>teger division, or 3.666666666666666… (“forever”)for decimal (“long”) division• In Python, if we want an <strong>in</strong>teger result we muststart with two <strong>in</strong>tegers: 11/3 gives 3• For the rema<strong>in</strong>der, use the symbol % (weird!)11%3 gives 2• If at least one of the numbers is a decimal (typefloat), you get a decimal answer11.0/3 and 11/3.0 both give (note the<strong>in</strong>accuracy of the answer) 3.666666666666666529


• In math, we use superscripts to <strong>in</strong>dicateexponents• Example: 2 3 means 2×2×2, whose value is 8• To raise a number to a power <strong>in</strong> Python, use **• Example: 2**3 means 2*2*2, whose value is 8• As <strong>in</strong> mathematics, negative and decimalexponents can be used, with the same mean<strong>in</strong>gas <strong>in</strong> mathematics• Example: Value of 2**-3 is 0.12530


• If you place pr<strong>in</strong>t <strong>in</strong> front of a Python value, anattempt will be made to show you the result <strong>in</strong> a waythat is “prettier” and more expected• Idea is to show what a user would expect• But keep <strong>in</strong> m<strong>in</strong>d that this has no effect on calculations32


• Basics• Nam<strong>in</strong>g variables• Assignment• Us<strong>in</strong>g the pr<strong>in</strong>t statement• Str<strong>in</strong>g concatenation• Increment<strong>in</strong>g a variable33


• We often have to remember some <strong>in</strong>formation forlater <strong>in</strong> a calculation• You may use a variable to refer to that <strong>in</strong>formation• You can use an identifier of your choos<strong>in</strong>g to namethe variable (with<strong>in</strong> some rules we will look at)• More precisely, a variable is someth<strong>in</strong>g that po<strong>in</strong>tsto the <strong>in</strong>formation you want to remember• The difference is irrelevant for numbers, but willsometimes be important• Called a “variable” because you can change the<strong>in</strong>formation to which it po<strong>in</strong>ts• Example: If the variable is called age, on your birthdayyou can change which number it refers to34


• The name of a variable must be a s<strong>in</strong>gle “word”• For example, age but not current age• Case matters• For example, Python considers age and Age to be different• You can use case differences to comb<strong>in</strong>e multiplewords <strong>in</strong>to one, satisfy<strong>in</strong>g Python and aid<strong>in</strong>greadability• Example: currentAge• The underscore symbol _ counts as a “letter,” soyou can use it between words to make the variablename look like two words while satisfy<strong>in</strong>g Python• Example: current_age35


• To associate a name with a value, we use an assignment:variable = value• Examples:granddaughter = 'Jessica'age = 10• Assignment def<strong>in</strong>es the relationship (b<strong>in</strong>d<strong>in</strong>g) between avariable and the value to which it refers• Without a def<strong>in</strong>ition, use of a name is considered anerror• A new assignment changes or redef<strong>in</strong>es the reference36


• The pr<strong>in</strong>t statement dist<strong>in</strong>guishes str<strong>in</strong>gs (text)from variable names because the former appear <strong>in</strong>quotes• If you want to pr<strong>in</strong>t several different items, usecommas to separate the items• Example:37


• Us<strong>in</strong>g a + sign between str<strong>in</strong>gs causes the str<strong>in</strong>gs tobe concatenated (cha<strong>in</strong>ed together)• Unlike the case with pr<strong>in</strong>t, you must put <strong>in</strong> blankspaces yourself• You can also concatenate numbers to str<strong>in</strong>gs, butyou must tell Python to treat the number as astr<strong>in</strong>g, us<strong>in</strong>g str38


• We often want to <strong>in</strong>crease the value of a variableby some amount, e.g., 1• More precisely, we want to replace the value to which avariable refers by one more than that value• This is called <strong>in</strong>crement<strong>in</strong>g the variable• We do this by us<strong>in</strong>g an odd-look<strong>in</strong>g assignmentstatement, <strong>in</strong> which the variable appears on bothsides of =• Examples:39


• Increment<strong>in</strong>g can also be specified us<strong>in</strong>g someshorthand notation• Example: <strong>in</strong>stead ofx = x + 2you can writex += 2• A similar notation works for subtraction and otheroperations• Example: x -= 2 for x = x - 2• These shorthand notations are purely optional –use them if you want to• But you should be able to recognize them if someoneelse writes them40

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!