您的位置:首页 > 编程语言 > Python开发

Python学习基础笔记——modules(模块操作)

2017-12-27 21:59 627 查看

modules(模块)

1.引入模块,例如
math
模块

import math
root=math.sqrt(36)
flr=math.floor(89.9)
print(root)
print(flr)
6.0
89.0


2.给模块的名字赋予简称,之后都可以使用简称

import math as m
root=m.sqrt(64)
print(root)
8.0


3.模块中包含许多函数,只引用其中一个或两个

from math import sqrt
root=sqrt(49)
print(root)
7.0


from math import *


4.提取模块中的一个变量

import math
print(math.pi)
3.141592653589793


5.打开和读取csv文件,将文件内容变换为list类型

import csv
f=open("nfl.csv")
csvraeder=csv.reader(f)
nfl=list(csvreader)


[['2009', '1', 'Pittsburgh Steelers', 'Tennessee Titans'],
['2009', '1', 'Minnesota Vikings', 'Cleveland Browns'],
['2009', '1', 'New York Giants', 'Washington Redskins'],
['2009', '1', 'San Francisco 49ers', 'Arizona Cardinals'],
['2009', '1', 'Seattle Seahawks', 'St. Louis Rams'],
['2009', '1', 'Philadelphia Eagles', 'Carolina Panthers'],
['2009', '1', 'New York Jets', 'Houston Texans'],
['2009', '1', 'Atlanta Falcons', 'Miami Dolphins'],
['2009', '1', 'Baltimore Ravens', 'Kansas City Chiefs'],
['2009', '1', 'Indianapolis Colts', 'Jacksonville Jaguars'],
['2009', '1', 'New Orleans Saints', 'Detroit Lions'],


6.读取list列表中的信息

import csv
f=open("nfl.csv")
csvreader=csv.reader(f)
nfl=list(csvreader)
patriots_wins=0
for row in nfl:
if row[2]=="New England Patriots":
patriots_wins+=1

61


7.自定义函数

import csv
f = open("nfl.csv", 'r')
nfl = list(csv.reader(f))

# Define your function here.
def nfl_wins(team):
count=0
for row in nfl:
if row[2]==team:
count=count+1
return count
cowboys_wins=nfl_wins("Dallas Cowboys")
falcons_wins=nfl_wins("Atlanta Falcons")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python