
Understanding Data Types and Arithmetic in Python
Data types are essential in programming to determine how data is stored and manipulated. In Python, basic types include integers, floating-point numbers, strings, and Booleans. Integers are whole numbers, while floating-point numbers have decimal fractions. Understanding the differences between these data types is crucial for accurate calculations and data processing in Python.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
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
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
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
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!
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
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