python-基础结构

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=0
while i<3:
    i+=1
    print(i)

6 bool

== 判断相等;!= 不等于

greater = sampleRate>5
if 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 = 0
scores = [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和value
scores["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和value
students ["Jim"] = 80 #再添加

修改值,

students["Tom"] = 65

判断键是否在字典中,

print("Tom" in students) #true
print("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]=1
print(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空格。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值