由于选修了《人工智能模式识别》的课程,要求用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. qt 如何安装 Debuggers 调试器 ?

    1.下载 SDK 或 WDK 打开网址:https://developer.microsoft.com/zh-cn/windows/hardware/windows-driver-kit 选择 SDK ...

  2. 常用的CSS框架

    常用的CSS框架 之前在写自己的个人网站的时候,由于自己Web前端不是特别好,于是就去找相关的CSS框架来搭建页面了. 找到以下这么一篇文章(列出了很多常用的CSS框架): http://w3scho ...

  3. datanode启动不起来的各种原因

    一般在数据节点的log日志信息里能找到导致启动不起来的原因. 1.Namenode和Datanode的NamenodeID不一致 描述:一般在集群多次重新格式化HDFS之后,或者刚安装时会碰到.日志信 ...

  4. 微软Surface Book推送Windows 10新固件更新:增强系统和电池

    微软公司最近为Surface Book推出了新的Windows 10固件系统和驱动的更新,并且以MSI的文件格式上传到了微软的下载中心里面.此次更新的内容主要是改进了Surface Book的系统的稳 ...

  5. dm642的视频口输出

    void VP1_EDMA(int displayMode,unsigned int w,unsigned int h) {      unsigned int i=0,k=0;  EDMA_Hand ...

  6. 通过grub-install命令把grub安装到u盘-总结

    通过grub-install命令把grub安装到u盘 ①准备一个u盘,容量不限,能有1MB都足够了. ②把u盘格式化(我把u盘格式化成FAT.fat32格式了,最后证明也是成功的).③开启linux系 ...

  7. Flex中的FusionCharts 2D柱形图

    1.2D柱形图源码 <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:f ...

  8. Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWo

    1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...

  9. DirectShow中写push模式的source filter流程 + 源代码(内附详细注释)

    虽然网上已有很多关于DirectShow写source filter的资料,不过很多刚开始学的朋友总说讲的不是很清楚(可能其中作者省略了许多他认为简 单的过程),读者总希望看到象第一步怎么做,第二步怎 ...

  10. 芝麻HTTP:Python爬虫进阶之Scrapy框架安装配置

    初级的爬虫我们利用urllib和urllib2库以及正则表达式就可以完成了,不过还有更加强大的工具,爬虫框架Scrapy,这安装过程也是煞费苦心哪,在此整理如下. Windows 平台: 我的系统是 ...