Data Types - Python Academy: The Stress Free Way To Learning Python Inside & Out (2014)

Python Academy: The Stress Free Way To Learning Python Inside & Out (2014)

Chapter 3. Data Types

In Python, input data are sorted according to different categories. The primary purpose of sorting is to help programmers like you in processing information more efficiently. Such categories function as data storage locations that you can access whenever you run the Python platform.

Variables

Variables contain values that have been specifically allocated to them. If you are working on complex codes for applications, you may want to store your information in these variables. Do not worry because you can access them anytime you need them. You can even use them to ensure that the information you gather from your end users stay safe and secured.

Numeric Types

Numbers in the Python programming language are different from the numbers you use to solve problems in Algebra. In Mathematics, adding .0 at the end of a number practically means nothing. It does not make any difference to its value. For instance, 3 and3.0 are the same.

In Python, however, 3 and 3.0 are different numbers. Before the program processes it, it has to undergo certain data processing methods. As a programmer, you have to learn about the different numeric types.


Integers

All whole numbers are integers. Numbers that contain a decimal point are not whole numbers; therefore, 3 is a whole number while 3.0 is not. Integers in Python are characterized by the data type int.

Take note that integers have capacity limitations. You will generate an error if you try to process a value beyond the allowed limits. Integers typically process numbers between -9,223,372,036.854 and 9,223,372,036.854.

There are interesting features that come with the int variable. For instance, base 2 only uses 0 and 1, base 8 uses numbers from 0 to 7, base 10 has similar properties with the decimal system and base 16 uses the letters A to F and the numbers 0 to 9 as digits.

Floating Point Values

Any number that contains a decimal point is considered as a floating point value in Python. It does not matter if the number after the decimal point is 0 or not. 3.0, 1.5, and 11.4, for example, are all floating point values. They are stored in the float data type. One huge advantage of floating point values over integers is that they have bigger storage spaces; hence, they are capable of storing very small or large values.

Then again, you should not think that they have an unlimited storage space. There is still a limitation. Nevertheless, they can contain as little as±2.2250738585072014× 10-308 and as much as 1.7976931348623157× 10-308. There are a couple of ways to allocate values with the use of floating point values. First, you can directly assign them. Second, you can use a scientific notation. Keep in mind that negative exponents produce fraction equivalents.

Complex Numbers

These numbers consist of real numbers and imaginary numbers combined. Usually, they are used in dynamic systems, quantum mechanics, computer graphics, electrical engineering and fluid dynamics. Complex numbers can be processed in Python and a few other programming languages.


Boolean Values

These are the two constant objects True and False. They represent truth values. When used in a numeric context, they function as 0 and 1. You can use the function bool to assign a value to a Boolean if such value may be interpreted as a truth value.

Strings

They are groups of characters that are connected with double quotation marks. Consider the following example:

TheString =“ Python got its name from a popular comedy show.“

As you can see in the sample code shown above, the phrase Python got its name from a popular comedy show. is assigned to the variable TheString.

Computers cannot understand letters, only numbers. So when you write a program, Python reads and interprets it based on the numbers that represent its letters. For example, in the American Standard Code for Information Interchange (ASCII), the number 65represents the letter A. So if you type in

ord (“ A“ )

you will get an output of

65

Because computers cannot understand letters, you have to convert strings into numbers. You can use int or float to do this. In case you need to convert numbers into strings, you can use str.