White Science Python (2): basic data type (on)

Life is short, I chose Python

introduction

The foregoing Portal

White learn Python (1): Opening

Contact with a new language, certainly must first understand its underlying data type. What? You ask why I must first understand the underlying data type?

In order to secure your life, obediently listen to my BB it, some did not even think about those.

Python has a lot of basic data types, so, what type of data is it?

Open Baidu, pick a Copy:

Data type defined in the data structure is a set of values ​​and defining a set of operations on a set value.

Place where the variable is used to store values, they have names and data types. Data type of a variable determines how the bits are stored on behalf of these values ​​to the computer's memory.

Xiao Bian you do not run, I do not kill you, data types not figure out, you have raised a variable concept

Of course, if you learn a programming language, such as computer grade examination in the C language, these two concepts are not difficult to understand.

If you have not learned, then it is a little difficult, after all, the basic concept is always the most difficult to explain.

Surely we all drink tea.

When buying tea in the tea shop will be a small cup, medium cup, large cup.

Here's a small cup, medium cup, large cup is the data type of which is variable in full bloom tea, milk tea name is the variable name.

Is not it just great to understand more?

digital

There are four types of data in the Python numbers, respectively:

  • int (signed integer)
  • long (Long)
  • float (float)
  • complex (plural)

Small tora Q: shrimp? Digital out a whole can type so much, could you fooled me?

Xiao Ming students, you will not be able to quietly listen to BB I finished it.

Python provides a function for us type(), we can obtain the current data type through this function. Examples are as follows:

print(type(123))

print(type(123.0))

print(type('123'))

print(type("123"))

Operating results of the above examples as follows:

<class 'int'>
<class 'float'>
<class 'str'>
<class 'str'>

It can be concluded that:

123 is a shaping (int)
123.0 is a float (float)
and the type of '123' and '123' are identical, are strings str (note that there are single or double quotation marks, the marks will not be considered if there is no string Types of)

We then used the isinstance another function (), the function returns fact can be seen from the literal meaning, determining whether an object is a known type, the specific syntax is as follows:

isinstance(object, classinfo)
  • object - the object instance.
  • classinfo - may be directly or indirectly, class name, or basic types tuple composed thereof.

Sample code is as follows:

print(isinstance(123, int))

print(isinstance(123.0, float))

print(isinstance('123', str))

Test results are as follows:

True
True
True

Plastic

Shaping means that integer, for example:

-1 -10,1,2333333333 like.

As for the length of how much we have to be a good test, the sample code as follows:

print(10**1000)

** The meaning of power, the sample code above this means that the 10 th 1000, show the following results:

10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Exactly how many zeros I do not want to count, anyway, so long it is certainly good enough.

Ary

Mentioned figures would have to mention another concept is hexadecimal, binary surely we all know, the world is more common in the computer's binary system, the more life there is often heard octal and hexadecimal.

Specific expressed as follows:

  • Decimal: normal written ok, all the numbers are the default decimal.
  • Binary: in the front added 0bprefix, and must be part of a composition, or only by 0 (nonsense, other figures also what is called binary), such as: 0b10101010101
  • Octal: at the top to add 0oa prefix, and must be part of only the digits 0 to 7 composition, such as: 0o12345670
  • Hex: adding at the top 0xprefix, and the part of 0 to 9 and A ~ F composition, such as: 0xdb273dc (Note: no distinction capitalization, may be written as 0xDB273DC)

Float

In simple terms is a float with a decimal point

Note: decimal floating point numbers can only be

In fact, further comprising a float fraction, for example:

print(1/2)

print(1/3)

print(1/6)

The results are as follows:

0.5
0.3333333333333333
0.16666666666666666

Because it is infinite decimals, so there will be errors.

plural

Come, first review the basic concept of a wave of junior high school:

  • Rational: rational number refers to the ratio of two integers. Rational number is the set of integers and fractions. In simple terms is finite or infinite decimal integer +.
  • Irrational number: Simply, it is an infinite non-repeating decimals.

Rational and irrational numbers together to become a real number, there is a real number in addition to the complex is called.

  • Complex: We the form z = a + bi (a, b are real numbers) referred to the complex, where a is called the real part, b is called the imaginary part, I referred to an imaginary unit.

Want more content please remember Baidu own right.

In Python, complex performance as follows:

print(1+1j)

print(type(1+1j))

The results are as follows:

(1+1j)
<class 'complex'>

The concept was all speak, the complex is the real and imaginary parts, then how to get it in Python? Examples are as follows:

print((2.46+1.37j).real)

print((2.46+1.37j).imag)

Python always feel science is a test of my English, realtrue, imagfalse.

Boolean value

Boolean value that in fact the two values, right or wrong, true or false.

In many other languages ​​into Boolean values ​​are listed as a separate data type, but Python did not.

So, boolean what use is it?

This use may become significant, such as frequently used in a variety of logic judgment, this of course we talk to you later.

Look at how simple it is to use, for example:

print(123 == 123.0)

print(123 == '123')

Test Results:

True
False

What? 123 turned out to be equal to 123.0, not to say that they are two types of inconsistencies it?

Note: ==only be used to make the comparison value, and will not compare the current data type, 123 and 123.0 are equal. And '123' is not equal 123 because '123' is a string, not numerical computation, and 123 numbers.

Sample Code

This series of small series all the code will be placed on code management repository Github and Gitee, to facilitate access.

Sample Code -Github

Sample Code -Gitee

Guess you like

Origin www.cnblogs.com/babycomeon/p/11711446.html