Python (iv)

Source: Internet
Author: User
Tags set set

1. How to use the collection

In Python, set is the type of a collection of basic data types, and he has a variable set (set ()) and immutable (Frozenset) two, including set set, set set Add, set Delete, intersection, set, difference set, and a series of operations

@ Create Collection Set

The Python set class is in Python's sets module, and for most current versions, you do not need to import the sets module to create the collection directly

>>> set (' Boy ')

Set ([' Y ', ' b ', ' O '])

>>>

The @ collection is added, removed.

Common additions to collections are add and update

Add method: The element to be passed in as a whole heroes to the collection inside:

>>>

>>> a =set (' Boy ')

>>> b =set (' Python ')

>>> A.add (' 123 ')

>>> A

Set ([' Y ', ' 123 ', ' B ', ' O '])

>>>

The method for collection update: is to split the incoming elements into the collection as an individual:

>>> b.update (' 21334324 ')

>>> b

Set ([' H ', ' o ', ' n ', ' 1 ', ' P ', ' 3 ', ' 2 ', ' t ', ' y ', ' 4 '])

>>>

Collection Delete Action method: remove


>>>

>>> c =set (' 1234 ')

>>> C

Set ([' 1 ', ' 3 ', ' 2 ', ' 4 '])

>>> c.remove (' 12 ')

Traceback (most recent):

File "<stdin>", line 1, in <module>

Keyerror: ' 12 '

>>> c.remove (' 1 ')

>>> C

Set ([' 3 ', ' 2 ', ' 4 '])

>>>

Note the deletion can only be deleted one at a time.

Some symbolic descriptions of @set ()

-difference sets, relative complement sets & Intersections | Collection, and set! = not equals = = equals in is a member relationship not in is not a member relationship

>>> a =set (' 123 ')

>>> B = Set (' 345 ')

>>> a&b

Set ([' 3 '])

>>> A | B

Set ([' 1 ', ' 3 ', ' 2 ', ' 5 ', ' 4 '])

>>> A-B

Set ([' 1 ', ' 2 '])

>>> A in B

False

>>> a not in B

True

>>> A = = b

False

>>> A! = B

True

Collections are unordered, so they cannot be manipulated by indexes and slices.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++22. Variables.

In the use of Python, we will use a lot of data, in order to facilitate the operation of the entire program, we will use this data with a simple name, easy to reference in the next program, the variable is the value of this data.

The assignment of a variable is simple, and we can use this variable after copying it;

>>>

>>> x = 6

>>> x*4

24

>>> x+4

10

>>>

At the time of the assignment, the value is the type of the variable, and the variable name references the value and the type.

Naming rules for variable names:

1. Variable names can include letters, numbers, underscores, but cannot start with numbers

2. The system keyword cannot be used as a variable name.

3. In addition to underlining, other symbols cannot be used as variable names.

The variable names for 4.python are case-sensitive.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ the operation of the 3.print statement.


>>> A

' 123 '

>>> Print (a)

123

>>>

The print variable name is a direct output of the contents of variable a, and if there is no print, only the output of the variable name is caused by a single parenthesis.

@ formatted output (string, integer)

Python's print statement is used in conjunction with the string operation% to implement the replacement function,

>>> print "%s is%d old"% (' she ', 12)

She is

>>>

This piece of%s and%d equals placeholders, which are served for strings and shaping.




This article is from the Cloud neutron blog, so be sure to keep this source http://liyongtao.blog.51cto.com/11018743/1869075

Python (iv)

Related Article

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.