English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes the initialization method of list in Python. Shared for everyone's reference, as follows:
1Basic method.
lst = [1, 2, 3, 4, 5)
2、Initialize consecutive numbers.}}
>>> lst = [n for n in range(5, 10) >>> print(lst) [5, 6, 7, 8, 9)
3、Initialize n with the same value. (Two methods)
>>> lst = ['x' for n in range(5) >>> print(lst) ['x', 'x', 'x', 'x', 'x'] >>> lst = ['z']*5 >>> print(lst) ['z', 'z', 'z', 'z', 'z'] >>> lst = [0]*3 >>> print(lst) [0, 0, 0]
4、Python's four data types dictionary, set, list, tuple are represented by curly braces, square brackets, and parentheses, respectively. For example:
Dictionary: dic={'a':12, 'b':34] Set: s = {1, 2, 3, 4] List: li=[1, 2, 3, 3) Tuple: tup=(1, 2, 3, 4) # Tuples are immutable lists
Readers who are interested in more content related to Python can check the special topics on this site: 'Summary of Python List (list) Operation Skills', 'Summary of Python Image Operation Skills', 'Python Data Structures and Algorithms Tutorial', 'Summary of Python Socket Programming Skills', 'Summary of Python Function Usage Skills', 'Summary of Python String Operation Skills', 'Classic Tutorial of Python Entry and Advanced', and 'Summary of Python File and Directory Operation Skills'.
I hope this article will be helpful to everyone in Python program design.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)