由于选修了《人工智能模式识别》的课程,要求用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查看变量类型
 #转换
str_eight = str(8)
eight = 8
str_eight_two = str(eight) str_eight = ""
int_eight = int(str_eight) print(int_eight)
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)
  • 判断语句
 #if statements
sample_rate = 700
greater = (sample_rate > 5)
if greater: #也可以为表达式
print(sample_rate)
else: #不写else也可以
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)
  • 字典的应用----统计个数
 #统计
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)

5.文件处理

  • 文件的读与写
1 f = open("test_write.txt","w")  #不存在的文件自动新建
2 f.write("")
3 f.write("\n")
4 f.write("")
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("\n")
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 基础语法复习的更多相关文章

  1. Python基础语法复习

    1.数据类型 List 列表 函数 append(): 在列表末尾追加. count(): 计算对象在列表中出现的次数. extend():将列表内容添加到列表中. index(): 计算对象在列表中 ...

  2. python之最强王者(2)——python基础语法

    背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...

  3. Python 基础语法(三)

    Python 基础语法(三) --------------------------------------------接 Python 基础语法(二)------------------------- ...

  4. Python 基础语法(四)

    Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)------------------------- ...

  5. Python 基础语法(二)

    Python 基础语法(二) --------------------------------------------接 Python 基础语法(一) ------------------------ ...

  6. Python 基础语法

    Python 基础语法 Python语言与Perl,C和Java等语言有许多相似之处.但是,也存在一些差异. 第一个Python程序 E:\Python>python Python 3.3.5 ...

  7. 吾八哥学Python(四):了解Python基础语法(下)

    咱们接着上篇的语法学习,继续了解学习Python基础语法. 数据类型大体上把Python中的数据类型分为如下几类:Number(数字),String(字符串).List(列表).Dictionary( ...

  8. python学习第五讲,python基础语法之函数语法,与Import导入模块.

    目录 python学习第五讲,python基础语法之函数语法,与Import导入模块. 一丶函数简介 1.函数语法定义 2.函数的调用 3.函数的文档注释 4.函数的参数 5.函数的形参跟实参 6.函 ...

  9. python学习第四讲,python基础语法之判断语句,循环语句

    目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...

随机推荐

  1. the c programing language 学习过程3

    ControlFlow  控制流 specify 指定 compound statement 复合语句 cryptic有隐含意义的 ambiguity歧义 robust稳健 disintegratio ...

  2. UVA1602

    实现的细节很多,学到了如何翻转.旋转.平移,get很多技巧,值得一做. AC代码: #include<cstdio> #include<cstring> #include< ...

  3. Redux:从action到saga

    前端应用消失的部分 一个现代的.使用了redux的前端应用架构可以这样描述: 一个存储了应用不可变状态(state)的store 状态(state)可以被绘制在组件里(html或者其他的东西).这个绘 ...

  4. 中小研发团队架构实践之微服务MSA

    一.MSA简介 1.1.MSA是什么 微服务架构MSA是Microservice Architecture的简称,它是一种架构模式,它提倡将单一应用程序划分成一组小的服务,服务之间互相通讯.互相配合, ...

  5. xpadder教程:自定义设置游戏手柄的图片

    关于xpadder设置按键的教程,网上已经很多,我就不凑这个热闹了.这里介绍的是如何自定义设置手柄的图片,就是按钮的背景图,如下图所示: 步骤: 1)准备一张背景图 注意:格式必须是24位色的BMP位 ...

  6. 看eShopOnContainers学一个EventBus

    最近在看微软eShopOnContainers 项目,看到EventBus觉得不错,和大家分享一下 看完此文你将获得什么? eShop中是如何设计事件总线的 实现一个InMemory事件总线eShop ...

  7. Spring MVC处理(下周完善)

    http://www.cnblogs.com/xiepeixing/p/4244574.html http://blog.csdn.net/kobejayandy/article/details/12 ...

  8. C语言视频简介

    通过学习<C语言基础视频教程>,可以让你对C语言有一个基础的了解,并且会编写一些基础的程序,本次视频主要讲解的内容有: 1. 数字的进制转换 2. 变量.运算符和表达式 3. 流程控制语句 ...

  9. 【前端】Vue2全家桶案例《看漫画》之五、引入axios

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_app_5.html 项目github地址:https://github.com/shamoyuu/ ...

  10. R语言︱SNA-社会关系网络—igraph包(中心度、中心势)(二)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- SNA社会关系网络分析中,关键的就是通过一些指 ...