Data Types. Every program must deal with data The data is usually described as a certain type This...

7
Data Types

Transcript of Data Types. Every program must deal with data The data is usually described as a certain type This...

Page 1: 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.

Data Types

Page 2: 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.

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 the data

is stored• In Python basic types are integers (int), floating point (float), strings

and Booleans• The first two are numeric and can be used in arithmetic, the last two

are not

Page 3: 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.

Integers vs. Floating point

• Integers are numbers which have no decimals, no fractions• They can be positive, negative, zero• Python can handle numbers of very, very large magnitude • Floating point numbers are ones which have fractions, like 3.14 and

2.0 and 5.2983841983• They are stored differently in the computer and their arithmetic is

done differently

Page 4: 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.

Floating point numbers - storage

• Each floating point number is stored in several pieces. You can think of one as being written in scientific notation, like 3.592 x 102 = 359.2

• In Python this is written as 3.592e2 (the base is assumed to be 10)• The 3.592 is the mantissa• The 2 is the power or exponent – it can be positive or negative.

Positive powers make numbers larger, negative powers make numbers smaller (towards zero)• Example: 4.923e-3 = 0.004923• Example: 7.2341e10 = 72341000000

Page 5: 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.

Floating point numbers – arithmetic

• When you add numbers with decimals together, you must “align the decimals” so you are adding tenths to tenths, hundredths to hundredths and so on• The computer does the same thing behind the scenes• So floating point arithmetic is usually slower than integer arithmetic• Floating point numbers also have an inherent error in them, just because a lot

of numbers cannot be stored precisely in the machine (think of pi or 1/3). Even 0.1 is not precise inside the computer – in binary it is a repeating decimal! • Most calculations are not too much affected by this but you should be aware of

it. If your work depends on many decimal places of accuracy, be careful how you use floating point numbers!

Page 6: 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.

Strings

• These data items are delimited by quotes, either single quotes (‘) or double quotes (“)• (Delimited by means that they are enclosed by the quotes, marked off

by the quotes)• We will use them in many ways • simply as labels “Total of numbers”• variables like names and address• input data

• Examples: “Joe”, “12345923 adfa123.s8234&*”, “”, ‘”hi” he said’

Page 7: 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.

Booleans

• Named after George Boole, English mathematician, logician• Created Boolean algebra, a two-valued logic based on True and False• The basis of all computer circuits (see EE 280)• In Python the Boolean constants are True and False – note that these

are not strings – no quotes• They can be assigned to variables, they can be output• They are used most often with if statements, chapter 6