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

Python module

編輯:Python

Use the functions in the module
The module is python A core concept of program architecture
Modules are like toolkits , To use the tools in this toolkit , You need to import import This module
Each with an extension py At the end of the python The source code file is a module
Define global variables in the module 、 Functions are tools that modules can provide for external direct use

""" author:claire data:2022 year 06 month 24 """
def print_line(char,times):
""" Print split lines """
print(char * times)
def print_lines(row,char,times):
""" Print multiline split lines :param row: Print a few lines :param char: Split characters used :param times: The number of times the split character is repeated """
for i in range (0,row,1):
print_line(char,times)
name = "ss"
""" author:claire data:2022 year 06 month 24 """
import f4_ Split line module
f4_ Split line module .print_lines(4,"hi ",3)
print(f4_ Split line module .name)
The module name is also an identifier
Identifiers can be made up of letters 、 Underline and numbers make up
What begins with a number cannot be in pycharm By importing the

pyc file

c yes compiled Compiled means

If you browse the program directory, you will find a _pycache_ The catalog of

The list is python The most frequently used data types , In other languages it is 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 , An index can also be called a subscript

Be careful : When taking a value from the list , If the index is out of range , The program will report an error

List method

""" author:claire data:2022 year 06 month 24 """
name_list = ["zhangsan","lisi","wangwu"]
# 1. Value and index 
print(name_list[0])
# Know the content of the data , Want to know where the data is in the list 
print(name_list.index("lisi"))
# 2、 modify 
name_list[0]=" Zhang San "
print(name_list[0])
# 3、 Add data 
# append Method can append data to the end of the list 
name_list.append("ss")
# insert Add... At the selected position 
name_list.insert(2,"xiaoming")
temp_list = ["sunwukong","zhubajie","shashidi"]
# You can add a set of lists 
name_list.extend(temp_list)
# iterable It is a set of data types such as inclusion lists 
print(name_list)
# 4、 Delete 
# remove Method can delete the specified data from the list 
name_list.remove("ss")
# pop Method can delete the last element by default 
name_list.pop()
# You can specify the index location 
name_list.pop(3)
print(name_list)
# clear Method to clear the list 
name_list.clear()
print(name_list)
name_list = ["zhangsan","lisi","wangwu"]
# know del Keywords delete list elements 
# del Keyword is essentially used to delete a variable from memory 
# The following code can no longer use this variable 
del name_list[1]
print(name_list)
# List statistics and deletion method extension 
name_list = ["zhangsan","lisi","wangwu","zhangsan"]
# len Function can count the total number of list elements 
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("zhangsan")
print(" Zhang San appeared %d Time " % count)
# Delete data from the list ,remove first occurence of value
name_list.remove("zhangsan")
print(name_list)
# List sorting method 
# sort Ascending .sort(reverse=true) Descending reverse() Reverse order 
name_list = ["zhangsan","lisi","wangwu","ss"]
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)
# reverse 
name_list.reverse()
num_list.reverse()
print(name_list)
print(num_list)

Iterated list

""" author:claire data:2022 year 06 month 24 """
name_list = ["zhangsan","lisi","wangwu","ss"]
for my_name in name_list:
print(" My name is %s" %my_name)

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