python-基础结构
来源:互联网 发布:中国2017网络暴力案例 编辑:程序博客网 时间:2024/06/01 15:43
1 特殊运算符
** : 指数符号
10**2 = 100
2 声明
声明并初始化
months = []
3 添加元素
通过append接口添加元素
months.append("January")months.append("February")
添加不同类型的元素到list中,比如float,int,string等类型。
months=[]months.append(1)months.append("January")months.append(2)months.append("February")
4 访问
索引是从0开始的,通过索引访问某个元素。
monnub = months[0]
通过 len
接口获取当前定义的List的元素个数;
numb = len(months)lastEle = months(numb-1)
python还提供了从后往前数索引元素的方式,同样想获取最后一个元素:
lastEle = months(-1)
另一种特殊访问-切片:
slicing 切片,获取某个连续区间的元素,例如months[1,3] = months[1], months[2], 不包括months[3]。
months = ["Jan","Feb","Mar","Apr"]febMar = months[1,3]
slicing 特殊,例如 months[1:] 表示months从索引1到最后一个元素。
aftFebs = months[1:]
5 遍历
for循环,
基本结构 for item in items:
一定注意后面有一个 : ,自动换行号,会有一个tab的缩进(缩进4个空格)。在python中通过缩进控制代码结构,这是非常重要的。 python中没有大括号等结构控制代码执行顺序。
months = ["Jan","Feb","Mar","Apr"]for month in months: print(month)
range(n)接口返回1~n,的数值,打印的结构为1~10.
for i in range(10): print(i)
两层for循环:
months=[["Jan","Feb"],["Mar","Apr"]]for i in months: for j in i: print(j)
while遍历,与C++,C#,等语言近似。
i=0while i<3: i+=1 print(i)
6 bool
== 判断相等;!= 不等于
greater = sampleRate>5if greater: print(sampleRate)else: print('less than')
7 字典
找一个元素是否存在,list效率低,而字典更高。
用list判断一个元素是否存在:
months = ["Jan", "Feb", "Mar"]if "Feb" in months: print("Feb found")Apr_found = "Apr" in months;print(Apr_found) 打印为false
students = ["Tom, "Jim"]score = 0scores = [70, 80]indexs = [0,1]name = "Jim"for i in indexes: if students[i]==name: score = scores[i]
以上方式通过两个list,想找Jim的分数便非常繁琐,用字典很方便:
scores = {}scores["Tom"]= 70 #添加一个key和valuescores["Jim"] = 80 #再添加print(scores.keys())print(scores) #{"Tom":70, "Jim":80}print(scores["Jim"])
字典初始化,
方式1
students = {"Tom":70, "Jim":80}
方式2
students ["Tom"]= 70 #添加一个key和valuestudents ["Jim"] = 80 #再添加
修改值,
students["Tom"] = 65
判断键是否在字典中,
print("Tom" in students) #trueprint("Toms" in students) #false
统计元素出现的个数,
pantry=["apple", "orange", "grape", "apple", "orange", "apple", "tomato","potato", "grape"]pantryCounts = {}for item in pantry: if item in pantryCounts: pantryCounts[item] = pantryCounts[item]+1 else: pantryCounts[item]=1print(pantryCounts)
8 文件处理
打开一个文件,比如最简单的txt文件,
读取文件,如果文件不存在会报FileNotFound异常。
f = open("test.txt","r") #建立连接(r:仅仅读取文件)g = f.read() #读入print(g)f.close() #关闭进程
写入文件,文件可以不存在。
f = open("testWrite.txt","w")f.write("123")f.write("\n")f.write("321")f.close()
读取.csv文件,
weatherData=[]f = open("weather.csv",'r')data = f.read()rows = data.split('\n')for row in rows: splitRow=row.split(",") weatherData.append(splitRow)print (weatherData) [['1','sunny'],['2','rain']]f.close()
9 函数
def 定义函数
def myPrint(a,b): print(a,b)
最后再说一遍:
python冒号,下一行一定有tab空格。
- python-基础结构
- 自学Python之Python基础:(三)Python代码结构
- [零基础学python]网站的结构
- 零基础学python-3.4 模块结构
- Python 基础语法_Python脚本文件结构
- Python程序设计基础 循环结构习题
- python基础系列(二)程序控制结构
- Python 基础语法_Python脚本文件结构
- python基础 表达式与选择结构
- python基础 循环结构程序设计(上)
- python基础 循环结构程序设计(下)
- Python基础05 三种基本结构&选择结构
- python程序设计基础3:python选择和循环结构
- Python基础(2)——Python选择、循环结构
- Python新手学习基础之循环结构练习
- python基础学习四——控制结构与异常
- 基础结构
- python结构
- NSCache优于NSDictionary的几点?
- Quartz立即执行
- 什么时候会报unrecognized selector错误?
- android学习:设置全屏、点击响应、布局背景
- 描述Jsp页面的运行过程
- python-基础结构
- 我的程序员之路(英语的学习)
- PPT | 云客堂——基于云应用的公众号后台开发
- 输入整型数组和排序标识,对其元素按照升序或降序进行排序
- 英国CodaOctopus公司的Echoscope®实时3D声呐
- POJ 1408[Fishnet]题解
- 字符统计
- verilog 运算符
- 总结