1.变量赋值与语句

#python 不需要手动指定变量类型。不需要分号
#To assign the value 365 to the variable days,we enter the variable name, add an equals sign(=)
days=365

2.输出 print()

1 #print(),python3中,必须加括号。

2 number_of_days = 365

3 print('Hello python')

4 print(number_of_days)

3.常见数据类型(str,int,float)

str_test='China'int_test=365float_test=34.22
print(type(str_test)) #type()为输出数据类型

输出为:

4.LIST基础

数值类型转换

str_eight=str(8) # str()令8转化为字符型,字符型无法做计算操作
print(type(str_eight))
int_eight=int(str_eight)
print(type(int_eight))

输出:

常见计算符号:加法 + ;减法 -;乘法 *;n次幂 **n;

1 x=2
2 y=x**3
3 print(y)

4 #输出 8

1 #LIST 如何定义LIST类型,如第一行所示
2 months=[]
3 print(type(months))
4 print(months)
5 months.append("January")
6 months.append("February")
7 print(months)
8
9 #输出
10 
11 []
12 ['January', 'February']
1 #LIST中元素的类型可以不同
2 months=[]
3 months.append(1)
4 months.append("January")
5 months.append(2)
6 months.append("February")
7 print(months)
8 #输出
9 [1, 'January', 2, 'February']
1 #LIST中寻找特定值
2 countries =[]
3 temperatures=[]
4
5 countries.append("China")
6 countries.append("India")
7 countries.append("United States")
8
9 temperatures.append(32.2)
10 temperatures.append(43.2)
11 temperatures.append(23.2)
12
13 print(countries)
14 print(temperatures)
15 china=countries[0] #取第一个元素
16 china_temperature = temperatures[1] #取第二个元素
17 print(china)
18 print(china_temperature)

#输出

['China', 'India', 'United States']
[32.2, 43.2, 23.2]
China
43.2

len() 得到LIST的元素数量

int_months = [1,2,3,4,5]
lenght = len(int_months) #包含多少个元素数量
print(lenght)

#输出 5

int_months = [1,2,3,4,5]
lenght = len(int_months) #包含多少个元素数量
index = len(int_months)-1last_value =int_months[index]
two_four = int_months[2:4] #冒号左右为起始位置(取)和结束位置(不取),。取左不取右
three_six = int_months[3:] #从第index=3(第四个元素)开始取到最后的值
print(lenght)
print(last_value)
print(two_four)
print(three_six)

#输出

5
5[3, 4]
[4, 5]
1 #查找LIST中是否存在特定元素
2
3 animals=["cat","ice","o""dog"]
4 if "cat" inanimals:
5 print("cat_found")

5.循环结构:

5.1 for循环

1 #python中通过缩进表示结构。
2 cities=[["China","aa","adfa"],["adfaf","adf2","2oo"]]
3 for city incities:
4 for j incity:
5 print (j)

输出:

China
aa
adfa
adfaf
adf2
2oo

5.2 while循环

1 i=0
2 while i<3:
3 i+=1
4 print(i)

输出:

1
2
3

range()

1 #range(5)代表0到4
2 for i in range(5):
3 print(i)

输出:

0
1
2
3
4

6.判断结构 &布尔类型(bool)

1 cat=True
2 dog=False
3 print(type(cat))

输出:

1 t=True
2 f=False
3 ift:
4 print("Now you see me")
5 iff:
6 print("Supring")
7 输出:
8 Now you see me
9
10 #输出0代表False,其他数字都代表True

7,字典

1 #dictionaries 字典结构,字典中Key表示键,value表示值,键值对一一对应
2 #字典中
3 scores={} #定义字典
4 print(type(scores))
5 scores["Jim"] = 80 #字典初始化方法1 变量名["键"]= Value
6 scores["Sue"] = 75
7 scores["Ann"] = 85
8 print(scores)
9 print(scores["Jim"])
10
11 #输出
12 
13 {'Jim': 80, 'Sue': 75, 'Ann': 85}
14 80

另一种字典初始化方法

1 students={}
2 #字典初始化方法2
3 students={"Jim":80,"Sue":75,"Ann":85}
4 print(students)
5
6 #对字典中的Value进行操作
7 students["Jim"]=students["Jim"] + 5
8 print(students)
10 #输出
11 {'Jim': 80, 'Sue': 75, 'Ann': 85}
12 {'Jim': 85, 'Sue': 75, 'Ann': 85}

判断某键是否在字典中

#判断某键是否在字典中

#print("Jim" in students) #输出 True
if "Jim" instudents:
print("True")
else:
print("False")

#输出

True

用字典统计某LIST中特定元素的数量

1 pantry=["apple","orange","grape","apple","orange","orange","grape"]
2 pantry_counts={}
3
4 for item inpantry:
5 if item inpantry_counts:
6 pantry_counts[item] = pantry_counts[item] + 1
7 else:
8 pantry_counts[item]=1
9 print(pantry_counts)
10
11 #输出
12 {'apple': 2, 'orange': 3, 'grape': 2}

8.文件操作

1 f=open("C:\Users\*\Desktop\test.txt",'w')
2 f.write('12345')
3 f.write(' ')
4 f.write('23456')
5 f.close()

6 #清空文件并读入此2行数字,记得关闭文件。

1 weather_data=[]
2 f = open("C:\Users\Allen\Desktop\test\weather.csv",'r',encoding = 'utf-8-sig') #读取格式问题要注意。
3 data =f.read()
4 #rows = data.split(' ')
rows=list(filter(None,data.split(' ')))
5 for row inrows:
6 split_row = row.split(',',1000)
7 weather_data.append(split_row)
8 print( weather_data)
9
10 #输出,存在问题:读取内容多了最后一个空元素 #

11 [['1', 'Sunday'], ['2', 'Sunday'], ['3', 'Sunday'], ['4', 'Sunday'], ['5', 'Windy'], ['6', 'Windy'], ['7', 'Windy'], ['']]

改正后空字符消失

1 weather=[]
2 for row inweather_data:
3 weather.append(row[0]) #把row第一列的数据提取出来
4 print(weather)
5 f.close()
6
7 #输出
8 ['1', '2', '3', '4', '5', '6', '7', '']

8 函数

#def代表函数开始, def 函数名(传入参数) 传入参数无需定义类型

defprintHello():
print('Hello pythy')
return
defprintNum():
for i in range(4):
print(i)
return
defadd(a,b):
return a+b
printHello()
print(printNum())
print(add(3,4))

#输出

Hello pythy
0
1
2
3None
7