Python 基础语法复习

2022/9/29 2:16:15

本文主要是介绍Python 基础语法复习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

由于选修了《人工智能模式识别》的课程,要求用phthon来实现算法,乘着周三晚上没课,就来回顾一下python的主要语法。

环境: Anaconda

    Python3.6

1.变量:

  • 在python中,变量是不需要提前声明类型的
#data type
str_test = "China"
int_test = 123
float_test = 122.5

print(str_test)
print(int_test)
print(float_test)
1 #data type 2 str_test = "China" 3 int_test = 123 4 float_test = 122.5 5 6 print(str_test) 7 print(int_test) 8 print(float_test)
  • 类型可以转换,用type查看变量类型
#转换
str_eight = str(8)
eight = 8
str_eight_two = str(eight)

str_eight = "8"
int_eight = int(str_eight)

print(int_eight)
print(type(int_eight))
1 #转换 2 str_eight = str(8) 3 eight = 8 4 str_eight_two = str(eight) 5 6 str_eight = "8" 7 int_eight = int(str_eight) 8 9 print(int_eight) 10 print(type(int_eight))

2.list类型

  • list类型里的元素类型可以不同,添加元素时可用list.apppend(),添加时自动为其设置下标index,可使用下标访问list中的元素
countries = []
temperatures = []

countries.append("China")
countries.append("India")
countries.append("United States")

temperatures.append(30.5)
temperatures.append(25.0)
temperatures.append(15.1)

print(countries)
print(temperatures)

china = countries[0]
china_temperature = temperatures[0]
print(china)
print(china_temperature)
1 countries = [] 2 temperatures = [] 3 4 countries.append("China") 5 countries.append("India") 6 countries.append("United States") 7 8 temperatures.append(30.5) 9 temperatures.append(25.0) 10 temperatures.append(15.1) 11 12 print(countries) 13 print(temperatures) 14 15 china = countries[0] 16 china_temperature = temperatures[0] 17 print(china) 18 print(china_temperature)
  • 计算list长度,切片操作,值得注意的是list[-1] == list[length-1], 即下标是循环的
int_months = [1,2,3,4,5,6,7,8,9,10,11,12]
length = len(int_months)
print(length)
index = len(int_months)-1
last_value = int_months[index]
print(last_value)
print(int_months[-1])       #倒数也可
#切片
two_four = int_months[2:4]    #取头不取尾
print(two_four)
tree_last = int_months[3:]
print(tree_last)
1 int_months = [1,2,3,4,5,6,7,8,9,10,11,12] 2 length = len(int_months) 3 print(length) 4 index = len(int_months)-1 5 last_value = int_months[index] 6 print(last_value) 7 print(int_months[-1]) #倒数也可 8 #切片 9 two_four = int_months[2:4] #取头不取尾 10 print(two_four) 11 tree_last = int_months[3:] 12 print(tree_last)

3.程序的结构

  • loop>>for, range()的用法值得注意
#loop
cities = ["Austin","Dallas","Houston"]
for city in cities:
    print(city)
for i in range(10):
    print(i)
1 #loop 2 cities = ["Austin","Dallas","Houston"] 3 for city in cities: 4 print(city) 5 for i in range(10): 6 print(i)
  • loop>>while
i = 0
while i < 3:
    i += 1
    print(i)
1 i = 0 2 while i < 3: 3 i += 1 4 print(i)
  • 双重循环
cities = [["Austin","Dallas","Houton"],["Haerbin","Shanghai","Beijing"]]
print(cities)
#for city in cities:
    #print(city)
    
for i in cities:
    for j in i:
        print(j)
1 cities = [["Austin","Dallas","Houton"],["Haerbin","Shanghai","Beijing"]] 2 print(cities) 3 #for city in cities: 4 #print(city) 5 6 for i in cities: 7 for j in i: 8 print(j)
  • 判断语句
#if statements
sample_rate = 700
greater = (sample_rate > 5)
if greater:        #也可以为表达式
    print(sample_rate)
else:               #不写else也可以
    print(less than)
1 #if statements 2 sample_rate = 700 3 greater = (sample_rate > 5) 4 if greater: #也可以为表达式 5 print(sample_rate) 6 else: #不写else也可以 7 print(less than)
  • list中查找的简写
#find a value 
animals = ["cat","dog","rabbit"]
for animal in animals:
    if animal == "cat":
        print("Cat found")
if "cat" in animals:
    print("2 is also right")
1 #find a value 2 animals = ["cat","dog","rabbit"] 3 for animal in animals: 4 if animal == "cat": 5 print("Cat found") 6 if "cat" in animals: 7 print("2 is also right")

4.dictionary类型

  • 字典的创建、初始化和赋值
students = {}
students["Tom"] = 60
students["Jim"] = 70
print(students)

students = {}
students = {
    "Tom": 60,
    "Jim": 70
}
print(students)
1 students = {} 2 students["Tom"] = 60 3 students["Jim"] = 70 4 print(students) 5 6 students = {} 7 students = { 8 "Tom": 60, 9 "Jim": 70 10 } 11 print(students)
  • 字典的应用----统计个数
#统计
pantry = ["apple", "orange", "grape", "apple", "orange", "apple", "tomato", "potato", "grape"]
pantry_counts = {}

for item in pantry:
    if item in pantry_counts:
        pantry_counts[item] = pantry_counts[item] + 1
    else:
        pantry_counts[item] = 1
print(pantry_counts)
1 #统计 2 pantry = ["apple", "orange", "grape", "apple", "orange", "apple", "tomato", "potato", "grape"] 3 pantry_counts = {} 4 5 for item in pantry: 6 if item in pantry_counts: 7 pantry_counts[item] = pantry_counts[item] + 1 8 else: 9 pantry_counts[item] = 1 10 print(pantry_counts)

5.文件处理

  • 文件的读与写
f = open("test_write.txt","w")  #不存在的文件自动新建
f.write("123456")
f.write("
")
f.write("234567")

f.close()
1 f = open("test_write.txt","w") #不存在的文件自动新建 2 f.write("123456") 3 f.write(" ") 4 f.write("234567") 5 6 f.close()
#File
#打开
f = open("test.txt","r")

#处理
g = f.read()
print(g,type(g))

#关闭
f.close()     #不要忘了关闭
1 #File 2 #打开 3 f = open("test.txt","r") 4 5 #处理 6 g = f.read() 7 print(g,type(g)) 8 9 #关闭 10 f.close() #不要忘了关闭
  • 文件csv的操作举例以及split()的使用
weather_data = []
f = open("weather.csv",r)
data = f.read()
#print(data)
rows = data.split("
")
#print(rows)
for row in rows:
    split_row = row.split(",")
    print(split_row)
    weather_data.append(split_row[0])
print(weather_data)
f.close()
1 weather_data = [] 2 f = open("weather.csv",r) 3 data = f.read() 4 #print(data) 5 rows = data.split(" ") 6 #print(rows) 7 for row in rows: 8 split_row = row.split(",") 9 print(split_row) 10 weather_data.append(split_row[0]) 11 print(weather_data) 12 f.close()

操作该部分时,我先建立了一个xlsx文件,随后将名称改为了csv文件,打不开文件,使用“rb”操作后显示乱码。随后发现另存为csv文件可以很好的解决这个问题,对csv文件的构成也有了深刻的了解。

6.函数的操作

def printHello():
    print("hello python")
    
def printNum():
    for i in range(0,10):
        print(i)
    return
def add(a,b):
    return a+b
printHello()
printNum()
add(1,2)
1 def printHello(): 2 print("hello python") 3 4 def printNum(): 5 for i in range(0,10): 6 print(i) 7 return 8 def add(a,b): 9 return a+b 10 printHello() 11 printNum() 12 add(1,2)

与C/C++不同,传入参数不需要申明类型。

由于选修了《人工智能模式识别》的课程,要求用phthon来实现算法,乘着周三晚上没课,就来回顾一下python的主要语法。 环境: Anaconda     Python3.6 1.变量: 在python中,变量是不需要提前声明类型的 1 #data type 2 str_test = "China" 3 int_test = 123 4 float_test = 122.5 5 6 print(str_test) 7 print(int_test) 8 print(float_test) 类型可以转换,用type查看变量类型 1 #转换 2 str_eight = str(8) 3 eight = 8 4 str_eight_two = str(eight) 5 6 str_eight = "8" 7 int_eight = int(str_eight) 8 9 print(int_eight) 10 print(type(int_eight)) 2.list类型 list类型里的元素类型可以不同,添加元素时可用list.apppend(),添加时自动为其设置下标index,可使用下标访问list中的元素 1 countries = [] 2 temperatures = [] 3 4 countries.append("China") 5 countries.append("India") 6 countries.append("United States") 7 8 temperatures.append(30.5) 9 temperatures.append(25.0) 10 temperatures.append(15.1) 11 12 print(countries) 13 print(temperatures) 14 15 china = countries[0] 16 china_temperature = temperatures[0] 17 print(china) 18 print(china_temperature) 计算list长度,切片操作,值得注意的是list[-1] == list[length-1], 即下标是循环的 1 int_months = [1,2,3,4,5,6,7,8,9,10,11,12] 2 length = len(int_months) 3 print(length) 4 index = len(int_months)-1 5 last_value = int_months[index] 6 print(last_value) 7 print(int_months[-1]) #倒数也可 8 #切片 9 two_four = int_months[2:4] #取头不取尾 10 print(two_four) 11 tree_last = int_months[3:] 12 print(tree_last) 3.程序的结构 loop>>for, range()的用法值得注意 1 #loop 2 cities = ["Austin","Dallas","Houston"] 3 for city in cities: 4 print(city) 5 for i in range(10): 6 print(i) loop>>while 1 i = 0 2 while i < 3: 3 i += 1 4 print(i) 双重循环 1 cities = [["Austin","Dallas","Houton"],["Haerbin","Shanghai","Beijing"]] 2 print(cities) 3 #for city in cities: 4 #print(city) 5 6 for i in cities: 7 for j in i: 8 print(j) 判断语句 1 #if statements 2 sample_rate = 700 3 greater = (sample_rate > 5) 4 if greater: #也可以为表达式 5 print(sample_rate) 6 else: #不写else也可以 7 print(less than) list中查找的简写 1 #find a value 2 animals = ["cat","dog","rabbit"] 3 for animal in animals: 4 if animal == "cat": 5 print("Cat found") 6 if "cat" in animals: 7 print("2 is also right") 4.dictionary类型 字典的创建、初始化和赋值 1 students = {} 2 students["Tom"] = 60 3 students["Jim"] = 70 4 print(students) 5 6 students = {} 7 students = { 8 "Tom": 60, 9 "Jim": 70 10 } 11 print(students) 字典的应用----统计个数 1 #统计 2 pantry = ["apple", "orange", "grape", "apple", "orange", "apple", "tomato", "potato", "grape"] 3 pantry_counts = {} 4 5 for item in pantry: 6 if item in pantry_counts: 7 pantry_counts[item] = pantry_counts[item] + 1 8 else: 9 pantry_counts[item] = 1 10 print(pantry_counts) 5.文件处理 文件的读与写 1 f = open("test_write.txt","w") #不存在的文件自动新建 2 f.write("123456") 3 f.write(" ") 4 f.write("234567") 5 6 f.close() 1 #File 2 #打开 3 f = open("test.txt","r") 4 5 #处理 6 g = f.read() 7 print(g,type(g)) 8 9 #关闭 10 f.close() #不要忘了关闭 文件csv的操作举例以及split()的使用 1 weather_data = [] 2 f = open("weather.csv",r) 3 data = f.read() 4 #print(data) 5 rows = data.split(" ") 6 #print(rows) 7 for row in rows: 8 split_row = row.split(",") 9 print(split_row) 10 weather_data.append(split_row[0]) 11 print(weather_data) 12 f.close() 操作该部分时,我先建立了一个xlsx文件,随后将名称改为了csv文件,打不开文件,使用“rb”操作后显示乱码。随后发现另存为csv文件可以很好的解决这个问题,对csv文件的构成也有了深刻的了解。 6.函数的操作 1 def printHello(): 2 print("hello python") 3 4 def printNum(): 5 for i in range(0,10): 6 print(i) 7 return 8 def add(a,b): 9 return a+b 10 printHello() 11 printNum() 12 add(1,2) 与C/C++不同,传入参数不需要申明类型。

这篇关于Python 基础语法复习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程