定义函数
def GetList(id):
list=["aaa","bbb","ccc"]
str =""
for li in list:
str+=li
return str
数据结构
a = [555, 133, 555, 1,356,77.7]
print(a.count(555), a.count(66.25), a.count('y'))
a.insert(2, 1000)
a.append(444)
print(a.index(555))
a.remove(555)
print(a)
del a[0]
print(a)
封装模块化
添加文件ObjList.py

在Main.py中调用GetList函数

错误和异常处理
while True:
try:
x = int(input("请输入一个数字: "))
break
except ValueError:
print("您输入的不是数字,请再次尝试输入!")
import sys
for arg in sys.argv[1:]:
try:
f = open(arg, 'r')
except IOError:
print('cannot open', arg)
else:
print(arg, 'has', len(f.readlines()), 'lines')
f.close()
try:
abc() #未定义的函数
except AssertionError as error:
print(error)
else:
try:
with open('abc.txt') as file:
read_data = file.read()
except FileNotFoundError as fnf_error:
print(fnf_error)
finally:
print('本行代码一定会被执行。')
本文深入探讨了Python中的函数定义、数据结构操作、模块封装及异常处理等核心概念。通过具体实例,如函数GetList的定义、列表操作、文件读取及自定义异常处理,展示了Python编程的基本技巧和最佳实践。
1万+

被折叠的 条评论
为什么被折叠?



