[Python]Sequence

來源:互聯網
上載者:User

In Python, string objects are immutable. Therefore, any operation on a string, including string concatenation,  produces a new string object, rather than modifying an existing one. concatenating N strings thus involves building and then immediately throwing away each of N-1 intermediate results. Performance is therefore vastly better for operations that build no intermediate results. but rather produce the desired end result at once.

Python's string-formatting operator % is one such operation, particularly suitable when you have a few pieces(e.g., each bound to a different variable) that you want to put together, perhaps with some constant text in addition. Performance is not a major issue for this specific kind of task. However, the % operator also has other potential advantages, when compared to an expression that uses multiple + operations on string. % is more readable, once you get used to it. Also, you don't have to call str on pieces that aren't already strings(e.g., numbers), because the format specifier %s does so implicitly. Another advantage is that you can use format specifiers other than %s, so that, for example, you can control how many significant digits the string form of a floating-point number should display.

What is "a Sequence?"

Python does not have a specific type called sequence, but sequece is still an often-used term in Python. sequence, strictly speaking, means: a container that can be iterated on, to get a finite number of items, one at a time, and that also supports indexing, slicing, and being passed to the built-in functin len(which gives the number of items in a container). Python lists are the "sequence" you 'll meet most often, but there are may others(strings, unicode objects, tuples, array.arrays, etc.).

Often, one does not need indexing, slicing, and len the ability to iterate, one item at a time, suffices. In that case, one should speak of an iterable(or, to focus on the finite number of items issue, a bounded iterable). Iterables that are not sequences include dictionaries(iteration gives the keys of the dictionary, one at a time in arbitrary order), file objects(iteration gives the lines of the text ifle, one at a time), and many more, including iterators and generators. Any iteralbe can be used in a for loop statement and in many equivalent contexts(the for clause of a list comprehension or Python 2.4 generator expression, and also many built-ins such as min, max, zip, sum, str.join, etc.).

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.