On the data types in Python

Source: Internet
Author: User
Tags sin

Python very important data types: strings, lists, dictionaries, tuples, often have netizens asked them what is the important difference between them? Can you give me a few examples to illustrate the next! Let's talk about it.

Data type:

?

1 2 3 4 float-floating-point numbers can be accurate to 15 digits after the decimal point int-integer can be infinitely large bool-Non-zero to True, 0 is false list-list

Float/int:

Operator:

/-Floating-point operations except

-When the result is positive, rounding; 11//5 = 2; 11//4 = 2

When the result is negative, the downward rounding; -11//5=-3; -11//4=-3

When the numerator's denominator is float, the result is float type

**-compute power; 11**2 =121

%-to take more than

Other mathematical operations:

1. Score:

Import fractions;

Fractions. Fraction (1,3) -1/3

Import Math;

-math.sin ()

-math.cos ()

-math.tan ()

-math.asin ()

math.pi-3.1415926 ...

Math.sin (MATH.PI/2)-1.0

Math.tan (MATH.PI/4)-0.9999999999 ...

Math.sin (); Math

List:

Created: A_list = [' A ', ' B ', ' Mpilgrim ', ' z ', ' example ']

a_list[-1]-' Example '

a_list[0]-' a '

a_list[1:3]-[' B ', ' Mpilgrim ', ' Z ']

A_list[:3]-[' A ', ' B ', ' Mpilgrim ']

A_list[3:]-[' z ', ' example ']

A_list[:]/a_list-[' A ', ' B ', ' Mpilgrim ', ' z ', ' example '

* Note: a_list[:] and a_list return a different list, but they have the same element

A_list[x:y]-Gets the list slice, x specifies the starting position of the first slice index, and y Specifies the slice index location that is not included in the cutoff.

To add an element to a list:

A_list = [' A ']

A_list = A_list + [2.0, 3]-[' A ', 2.0, 3]

A_list.append (True)-[' A ', 2.0, 3, True]

A_list.extend ([' Four ', ' Ω '])-[' A ', 2.0, 3, True, ' Four ', ' Ω ']

A_list.insert (0, ' ω ')-[' ω ', ' a ', 2.0, 3, True, ' Four ', ' Ω ']

List Other Features:

A_list = [' A ', ' B ', ' new ', ' Mpilgrim ', ' new ']

A_list.count (' new ')-2

A_list.count (' Mpilgrim ')-1

' New ' in A_list-true

A_list.index (' new ')-2

A_list.index (' Mpilgrim ')-3

A_list.index (' C ')-through a exception because ' C ' isn't in a_list.

Del a_list[1]-[' A ', ' new ', ' Mpilgrim ', ' new '

A_list.remove (' new ')-[' a ', Mpilgrim ', ' new '

Note: Remove deletes only the first ' new '

A_list.pop ()-' new '/[' a ', Mpilgrim '] (delete and return the last element)

A_list.pop (0)-' a '/[' Mpilgrim '] (delete and return No. 0 Element)

The empty list is false and the other list is true.

Tuples (elements are immutable lists):

Definition: Same as the definition of a list, except for the collection of the entire element with parentheses, not square brackets closed

A_tuple = ("A", "B", "Mpilgrim", "Z", "example")

A_tuple = (' A ', ' B ', ' Mpilgrim ', ' z ', ' example ')

Tuple can only be indexed and cannot be modified.

Advantages of tuples in relation to lists:

1. Fast speed

2. "Write protection", more secure

3. Some tuples can be used as dictionary keys??

The built-in tuple () function takes a list parameter and converts the list into tuples

Similarly, the list () function converts a tuple to a list

Assign multiple values at the same time:

v = (' A ', 2, True)

(x,y,z) = V-x= ' A ', y=2, z=true

Range ()-built-in function to assign values to continuous variables

(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday) = range (7)

Monday-0

Thursday-3

Sunday-6

Range ()-The built-in function range () builds an integer sequence in which the range () function returns an iterator.

Collection (the value inside is unordered):

Create collections: Separate each value with commas, and include all values with braces {}.

A_set = {1}

Type (A_set)-

To create a collection based on a list:

A_list = [' A ', ' B ', ' Mpilgrim ', True, False, 42]

A_set = Set (A_list)

A_set-{' A ', ' B ', ' Mpilgrim ', True, False, 42}

A_set = set ()-Gets an empty set

A_dic = {An empty DIC is obtained

To modify a collection:

A_set = {1,2}

A_set.add (4)-{1,2,4}

Len (A_set)-3

A_set.add (1)-{1,2,4}

A_set.update ({2,4,6})-{1,2,4,6}

A_set.update ({3,6,9}, {1,2,3,5,8,13})-{1,2,3,4,5,6,8,9,13}

A_set.update ([15,16])-{1,2,3,4,5,6,8,9,13,15,16}

A_set.discard (16)-{1,2,3,4,5,6,8,9,13,15}

A_set.discard (16)-{1,2,3,4,5,6,8,9,13,15}

A_set.remove (15)-{1,2,3,4,5,6,8,9,13}

A_set.remove ()-through a exception

A_set.pop ()-return 1/{2,3,4,5,6,8,9,13}

Note: A_set.pop () randomly deletes a value from the collection and returns the value.

A_set.clear ()-set ()

A_set.pop ()-through exception.

Other operations for the collection:

A_set = {2,3,4,5,6,8,9,13}

In A_set-false

4 in A_set-true

B_set = {3,4,10,12}

A_set.union (B_set)-two sets of and

A_set.intersetion (B_set)-intersection of two sets

A_set.difference (B_set)-a_set with elements that are not in B_set

A_set.symmetric_difference (B_set)-Returns all elements that appear in only one collection

A_set.issubset (B_set)-Determine if A_set is a subset of B_set

B_set.issuperset (A_set)-Determine if B_set is a superset of A_set

In a Boolean-type context, the empty collection is false, and any collection that contains more than one element is true.

Dictionary (unordered collection of key-value pairs):

To create a dictionary:

A_dic = {' Server ': ' db.diveintopython3.org ',

' Databas ': ' MySQL '}

a_dic[' server ']-' db.diveintopython3.org '

a_dic[' database ']-' MySQL '

To modify a dictionary:

a_dic[' user ' = ' mark '-{' user ': ' Mark ', ' Server ': ' db.diveintopython3.org ', ' Database ': ' Blog '}

a_dic[' database ' = ' blog '-{' user ': ' Mark ', ' Server ': ' db.diveintopython3.org ', ' Database ': ' Blog '}

a_dic[' user ' = ' bob '-{' user ': ' Bob ', ' Server ': ' db.diveintopython3.org ', ' Database ': ' Blog '}

a_dic[' user ' = ' mark '-{' user ': ' Bob ', ' uuser ': ' Mark ', ' Server ': ' db.diveintopython3.org ', ' Database ': ' Blog '}

Note: 1. Duplicate keys are not allowed in the dictionary. Assigning values to an existing key will overwrite the original value;

2. You can add a new key value pair at any time;

3. Dictionary keys are case-sensitive.

Mixed Value Dictionary:

suffixes = {1000:[' KB ', ' MB ', ' GB ', ' TB ', ' PB ', ' EB ', ' ZB ', ' YB '],

1024: [' KiB ', ' MiB ', ' GiB ', ' TiB ', ' PiB ', ' EiB ', ' ZiB ', ' Yib ']}

Len (suffixes)-2

1000 in Suffixes-true

suffixes[1024]-[' KiB ', ' MiB ', ' GiB ', ' TiB ', ' PiB ', ' EiB ', ' ZiB ', ' Yib '

suffixes[1000][3]-' TB '

Empty dictionary is false, all other dictionaries are true

The above mentioned is the entire content of this article, I hope you can enjoy.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.