Python beginners (3): python beginners

Source: Internet
Author: User

Python beginners (3): python beginners

8. Sequence (string, list, tuples, and dictionary)

A string is an immutable Character Sequence. You can use subscript or sharding to operate it.

The list is represented by brackets []. elements are separated by commas. The data types of each element in a sequence can be different. The elements in the list are ordered, you can perform operations on the subscripts or shards. The two lists "add" will generate a new list, containing all the elements of the two lists, and keep the original order.

The tuples are similar to the list. The difference is that the tuples are represented by parentheses () and the elements cannot be modified. The list can change the elements.

The dictionary is represented by braces {}. The dictionary elements are represented by key: value pairs, separated by commas (,). The dictionary elements are unordered, you cannot operate on an ordered sequence such as subscript. You can only operate on the value of an element's key. The values of different keys can be different data types; when traversing the entire dictionary, you can only traverse its key and its value.

>>># String >>> ch = 'Hello python! '>>> Ch [4] 'O' >>> ch [: 4] 'heller' >>> ch [4:] 'o python! '>>># List >>> lst = [] >>> lst = lst + [3, 'A', {'hello': 'python! '}] >>> Lst [3, 'A', {'hello': 'python! '}] >>> Lst [: 2] [3, 'a'] >>> lst [2:] [{'hello': 'python! '}] >>># Tuples >>> tup = (3, 'A', {'A':' B '}) >>> tup (3, 'A', {'A': 'B'}) >>> tup = tup + ([1, 2, 3, 4],) >>> tup (3, 'A', {'A': 'B'}, [1, 2, 3, 4]) >>> tup [1:-1] ('A ', {'A': 'B'}) >>> tup [1] = 'bb' Traceback (most recent call last): File "<pyshell #16> ", line 1, in <module> tup [1] = 'bb' TypeError: 'tuple' object does not support item assignment >>>#dictionary >>> dct = {1: 'A', 2: 'B', 3: 'C', 4: 'D'} >>> dct [1] = [1, 2, 3, 4] >>> dct {1: [1, 2, 3, 4], 2: 'B', 3: 'C', 4: 'D '}>>> dct [0] = 'hello' >>> dct {0: 'hello', 1: [1, 2, 3, 4], 2: 'B', 3: 'C', 4: 'D '}View Code

9. set

The set is also represented by braces {} and defined by set. It has the same concept as a set in mathematics. It does not have the same elements, and the elements are unordered. It can perform intersection, union, and difference set operations.

>>># Set () can be a string, list, or tuples >>> se = set ([1, 2, 3]) >>> se {1, 2, 3 }>>> se1 = set (2, 3, 3, 5, 6) >>> se1 {2, 3, 5, 6 }>>> se & se1 # intersection {2, 3 }>>> se | se1 # Union {1, 2, 3, 5, 6 }>>> se-se1 # difference set {1 }>>>

 

10. if, while, and

If and while are followed by a Boolean value. if it is true, the operation continues. if it is false, the loop is skipped or ended.

For is used to traverse an object. You can use continue to end this loop of the current layer loop, and use break to end the entire loop of the current layer.

>>> For I in range (5): print (I) for j in range (20, 25): if j = 21: continue if j = 23: break print (I, j) 00 20 #21 items none because continue ended "j = 21" this cycle 0 22 #23 and 24 items is because break ended this layer for loop 11 201 222 2 202 2233 203 224 # break does not apply to the for loop of the outer layer, so it will loop to the end 4 204 22 >>>

 

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.