- Mastering Python Scripting for System Administrators
- Ganesh Sanjiv Naik
- 243字
- 2021-07-02 14:00:22
Understanding lists
Python supports a data structure called list, which is a mutable and ordered sequence of elements. Each element in that list is called as item. Lists are defined by inserting values between square brackets [ ]. Each element of list is given a number, which we call as a position or index. The index starts from zero; that is, the first index is zero, the second index is 1, and so on. We can perform the following operations on lists: indexing, slicing, adding, multiplying, and checking for membership.
Python's built-in length function returns the length of that list. Python also has function for finding the largest and smallest item of list. Lists can be numbered lists, string lists, or mixed list.
The following is the code for creating a list:
l = list()
numbers = [10, 20, 30, 40]
animals = ['Dog', 'Tiger', 'Lion']
list1 = ['John', 5.5, 500, [110, 450]]
Here, we've created three lists: the first is numbers, the second is animals, and the third is list1. A list within another list is called as nested list. Our list1 is a nested list. A list containing no elements is called an empty list; you can create one with empty brackets, [].
As you might expect, you can assign list values to variables:
>>> cities = ['Mumbai', 'Pune', 'Chennai']
>>> numbers_list = [75, 857]
>>> empty_list = []
>>> print (cities, numbers_list, empty_list)
['Mumbai', 'Pune', 'Chennai'] [75, 857] []
- Mastering JavaScript Functional Programming
- CockroachDB權(quán)威指南
- Visual Studio 2012 Cookbook
- Beginning Java Data Structures and Algorithms
- Java高并發(fā)核心編程(卷2):多線程、鎖、JMM、JUC、高并發(fā)設(shè)計(jì)模式
- Getting Started with ResearchKit
- JavaScript+jQuery開發(fā)實(shí)戰(zhàn)
- 我的第一本算法書
- HTML5+CSS3基礎(chǔ)開發(fā)教程(第2版)
- Microsoft 365 Certified Fundamentals MS-900 Exam Guide
- STM8實(shí)戰(zhàn)
- PHP動(dòng)態(tài)網(wǎng)站開發(fā)實(shí)踐教程
- Python Penetration Testing Essentials
- Building Clouds with Windows Azure Pack
- 美麗洞察力:從化妝品行業(yè)看顧客需求洞察