程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

List of getting started with Python

編輯:Python

Catalog

One 、 Preface

Two 、 keyword 、 Functions and methods

3、 ... and 、 Definition of list

Four 、 List common operations

5、 ... and 、 Loop traversal of list

6、 ... and 、 Application scenarios


One 、 Preface

stay python in , All non numeric ( character string 、 list 、 Tuples 、 Dictionaries ) Variables support the following features : It's all a sequence sequence, It can also be understood as a container ; Value 【】; Traverse for in; Calculate the length 、 Maximum / minimum value 、 Compare 、 Delete ; link + and repeat *; section .

Two 、 keyword 、 Functions and methods

1. Keywords are python Built in 、 Identifier with special meaning , There is no need to use parentheses after keywords

2. Functions encapsulate independent functions , Can be called directly , Functions need to be memorized .

Function name ( Parameters )

3. Methods are similar to functions , It also encapsulates independent functions , Methods need to be called through objects , Indicates the operation to be done for this object .

object . Method name ( Parameters )

3、 ... and 、 Definition of list

List( list ) yes python The most frequently used data type in , In other languages, it is often called an array

Dedicated to storing a bunch of information

List with 【】 Definition , Use... Between data , Separate

The index of the list is from 0 Start , Index is the position number of data in the list , Indexes can also be called subscripts

Be careful : Take values from the list , If the index is out of range , The program will report an error

name_list = ["zhangsan","lisi","wangwu"]

 

Four 、 List common operations

stay ipython3 Define a list in , for example :name_list = []

Input name_list. Press down TAB key ,ipython The following methods can be used for the list :

 

How to add, delete, and modify the list :

name_list = ["zhangsan","lisi","wangwu"]
# 1. Value and index
# list index out of range - List index out of range
print(name_list[0])
# Know the content of the data , Want to determine where the data is in the list
# Use index The method needs attention , If the data passed is not in the list , The program will report an error !
print(name_list.index("lisi"))
# 2. modify
name_list[1] = " Li Si "
# list assignment index out of range - The index specified in the list is out of range , The program will report an error !
# name_list[3] = " Wang Xiaoer "
# 3. increase
# append Method can append data to the end of the list
name_list.append(" Wang Xiaoer ")
# insert Method can insert data at the specified index position of the list
name_list.insert(1," Little stink ")
temp_list = [" The Monkey King "," Pig eight quit "," Monk sha "]
name_list.extend(temp_list)
# 4. Delete
# remove Method can delete the specified data from the list
name_list.remove("wangwu")
# pop Method can delete the last element in the list by default
name_list.pop()
# pop Method can specify the index of the element to be deleted
name_list.pop(3)
# clear Method can empty the list
name_list.clear()
print(name_list)

 del keyword :

name_list = [" Zhang San "," Li Si "," Wang Wu "]
# del keyword (delete) Delete list elements
# Tips : In daily development , To delete data from the list , It is recommended to use the methods provided in the list
del name_list[1]
# del Keywords are essentially used to delete a variable from memory
name = " Xiao Ming "
del name
# Be careful : If you use del Keyword to remove variables from memory
# The following code can no longer use this variable
print(name)
print(name_list)

len Functions and count Method :

name_list = [" Zhang San "," Li Si "," Wang Wu "," Zhang San "]
# len(length length ) The function can count the total number of elements in the list
list_len = len(name_list)
print(" List contains %d Elements " % list_len)
# count Method can count the number of times a data appears in the list
count = name_list.count(" Zhang San ")
print(" Zhang San appeared %d Time " % count)
# Delete the first occurrence of data from the list , If the data doesn't exist , The program will report an error
name_list.remove(" Zhang San ")
print(name_list)

Sorting method :

name_list = ["zhangsan","lisi","wangwu","wangxiaoer"]
num_list = [6,8,4,1,10]
# Ascending
# name_list.sort()
# num_list.sort()
# Descending
# name_list.sort(reverse=True)
# num_list.sort(reverse=True)
# The reverse ( reverse )
name_list.reverse()
num_list.reverse()
print(name_list)
print(num_list)

5、 ... and 、 Loop traversal of list

Traversal is to get data from the list from beginning to end ( For each element inside the loop body , Do the same thing ), stay python In order to improve the efficiency of list traversal , Specially provided iterations iteration Traverse

Use for So we can do iterative traversal

# for Variables used inside the loop in list
for name in name_list:
The inner loop operates on list elements
print(name)

 

name_list = [" Zhang San "," Li Si "," Wang Wu "," Wang Xiaoer "]
# Iterate through the list
"""
Get data sequentially from the list , In every cycle , The data will be saved in my_name In this variable , The function of this variable
This is to access the data obtained this time in the loop body
for my_name in List variables :
print(" My name is %s" % my_name)
"""
for my_name in name_list:
print(" My name is %s" % my_name)

6、 ... and 、 Application scenarios

Even though python Different types of data can be stored in the list of , But in development , More application scenarios are a. Lists store the same type of data ;b. Through iterative traversal , Inside the circulatory body , For each element in the list , Do the same thing


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved