Data Types in Python

Source: Internet
Author: User

Data Types in Python

What are the important data types in Python: strings, lists, dictionaries, and tuples? Can you give me a few examples! Next, let's discuss it.

Data Type:

?

1

2

3

4

Float-floating point number can be precise to 15 digits after the decimal point

Int-integer can be infinitely large

Bool-non-zero: true; zero: false

List-list

Float/Int:

OPERATOR:

/-Floating point Operation Division

//-When the result is a positive number, it is rounded up. 11 // 5 = 2; 11 // 4 = 2

When the result is a negative number, it is rounded down to an integer.-11 // 5 =-3;-11 // 4 =-3

When the denominator is float, the result is float.

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

%-Remainder

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:

Create: a_list = ['A', 'B', 'mpilgrim', 'z', 'example ']

A_list [-1]-'example'

A_list [0]-'A'

A_list []-['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 different lists, but they have the same elements.

A_list [x: y]-Get list slice. x indicates the starting position of the first slice index, and y indicates the ending but not including the slice index.

Add elements to the 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 ', 'Ω']

Other functions of list:

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' is not in a_list.

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

A_list.remove ('new')-['A', mpilgrim', 'new']

Note: remove only deletes the first 'new'

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

A_list.pop (0)-'A'/['mpilgrim'] (delete and 0th elements are returned)

The empty list is false, and other lists are true.

Tuples (the element is an unchangeable list ):

Definition: similar to the definition of the list, except that the entire element set is enclosed by parentheses rather than square brackets.

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 over lists:

1. Fast

2. "Write Protection", more secure

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

The built-in tuple () function accepts a list parameter and converts the list to a metagroup.

Similarly, the list () function converts tuples to lists.

Multiple values are assigned at the same time:

V = ('A', 2, True)

(X, y, z) = v-x = 'A', y = 2, z = True

Range ()-built-in function for continuous variable assignment

(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, and the range () function returns an iterator.

Set (the values in it are unordered ):

Create a set: Separate values with commas (,) and include all values in braces.

A_set = {1}

Type (a_set )-

Create a set based on the 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 ()-Get an empty set

A_dic ={}-Get an empty dic

Modify set:

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, 4, 5, 6, 8, 9, 13}

A_set.update ([])-{, 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 (15)-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 set and returns the value.

A_set.clear ()-set ()

A_set.pop ()-through exception.

Other operations on the set:

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

30 in a_set-False

4 in a_set-True

B _set = {3, 4, 10, 12}

A_set.union (B _set)-sum of the Two Sets

A_set.intersetion (B _set)-intersection of two sets

A_set.difference (B _set)-a_set contains elements that are not in B _set.

A_set.symmetric_difference (B _set)-returns all elements that appear only in one set.

A_set.issubset (B _set)-judge whether a_set is a subset of B _set.

B _set.issuperset (a_set)-determines whether B _set is a superset of a_set.

In a Boolean context, the empty set is false, and any set containing more than one element is true.

Dictionary (unordered set of key-value pairs ):

Create a dictionary:

A_dic = {'server': 'db .dive?python3.org ',

'Database': 'mysql '}

A_dic ['server']-'db .dive?python3.org'

A_dic ['database']-'mysql'

Modify the dictionary:

A_dic ['user'] = 'mark'-{'user': 'mark', 'server': 'db .dive?python3.org ', 'database': 'blog '}

A_dic ['database'] = 'blog '-{'user': 'mark', 'server': 'db .dive?python3.org', 'database': 'blog '}

A_dic ['user'] = 'bob'-{'user': 'bob', 'server': 'db .diveappspython3.org ', 'database': 'blog '}

A_dic ['user'] = 'mark'-{'user': 'bob', 'uuser': 'mark', 'server': 'db .dive1_python3.org ', 'database': 'blog '}

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

2. You can add new key-value pairs at any time;

3. The dictionary key is case sensitive.

Mixed value dictionary:

Suffixes = {1000: ['kb', 'mb', 'gb', 'tb', 'pb', 'EB ', 'zb', 'yb'],

1024: ['kiib ', 'mib', 'gib', 'tib', 'pib', 'eiib ', 'zib', 'yib ']}

Len (suffixes)-2

1000 in suffixes-True

Suffixes [1024]-['kiib ', 'mib', 'gib', 'tib', 'pib', 'eiib ', 'zib', 'yib ']

Suffixes [1000] [3]-'tb'

The empty dictionary is false, and all other dictionaries are true.

The above is all the content of this article. I hope you will like it.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.