记Python学习
上周学的Python,感觉有点忘了,现在回顾一下。。。
一、Python安装及测试:https://www.cnblogs.com/weven/p/7252917.html
例子:
Python自带的IDEL工具:
二、安装Pycharm:https://www.cnblogs.com/dcpeng/p/9031405.html
玩了一段时间后,想着python应该有像java这样有类似IDEA的工具吧,于是乎。。。安装可能有几个不一样,但大概都可以看出来。
激活码(好像是这个):https://blog.csdn.net/yournevermore/article/details/90480650
三、学习基础语法(比如菜鸟教程):https://www.runoob.com/python/python-tutorial.html
四、不成文的小笔记
- python 是一种 动态 解释性 强类型 语言(对动态、解释、强类型的理解,看过一篇文章我感觉写的很好,现在没找到了,难受)
- 定位是“优雅”、“明确”、“简单”
- Jyhton
- Python的Java实现,Jython会将Python代码动态编译成Java字节码,然后在JVM上运行。
- 以下关键字不能声明为变量名
- ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
- 变量名只能是 字母、数字或下划线的任意组合,变量名推荐写法:
- 驼峰体 MeetTheGirl
- 下划线 meet_the_girl
- 在64位机器上int的范围是: -2**63~2**63-1,即-9223372036854775808~9223372036854775807
- 在Python中,凡是用引号引起来的,全是字符串
- 在python中,很注重空格格式,要保存统一一致
- x[:,n]就是取所有集合的第n个数据
- x[n,:]就是取第n集合的所有数据
- x[:,m:n],即取所有数据集的第m到n-1列数据
- readlines() 方法用于读取所有行(直到结束符 EOF)并返回列表
- strip() #去掉每行头尾空白(在for循环里)
- mat(矩阵Mat) 好像是把矩阵排成1列
- pyCharm快捷键(小写的就行):
- 自动排版Python代码 Ctrl+Alt+L
- 快速定位到错误行 F2
- 快速查看最近的修改 Alt+Shift+C
五、利用pycharm做一些test
test1
# -*- coding: utf-8 -*- age = 12 age = 12 + 1 age1 = age + 1 print(age1) meet = 1 # 单行注释 ''' 多 行 注 释 ''' print(3 > 7) # 字符串的拼接 S1 = 'a' S2 = 'b' print(S1 + S2) # 相乘 print(S1 * 8) # 字符串与数字拼接 num = 8 print(S1 + str(num)) # 用户交互,需要注意的是input程序交互获取到的内容是字符串,即使输入的是数字 name = input("请输入您的用户名:") print(name) # if ''' age = input("请输入您的年龄:") if int(age) > 18: print("哈哈哈哈") ''' # while if elif while True: age = input("年:") if int(age) >= 22: print("啦啦啦") elif int(age) >= 18: print("哈哈哈") elif int(age) < 18: print("水水水水")
控制台:
test2
# global 要想给全局变量重新赋值,就要global一下全局变量 # 定义全局变量 NAME = "林" def get_NAME(): return NAME def set_NAME(name): global NAME NAME = name return NAME print("全局变量1:" + get_NAME()) set_NAME("奥斯") print("全局变量2:" + get_NAME()) # with...as 执行with后的方法后将方法返回值赋值给as后的变量 class Simple: def __enter__(self): print("enter") return "foo" def __exit__(self, exc_type, exc_val, exc_tb): print("exit") def get_simple(): return Simple() with get_simple() as simple: print("我是" + simple) # 理解lambda g = lambda: "lambda test." print(g()) num1 = lambda x, y=1: x + y print(num1(1)) print(num1(10, 10)) # 理解for循环 x = 'run' for i in range(len(x)): print(x[i]) # 理解yield def test_yield(n): for i in range(n): yield i * 2 # 每次的运算结果都返回 for j in test_yield(3): print(j, ":",)
控制台:
test3 (机器学习回归):https://www.cnblogs.com/wing1995/p/4951500.html ex0.txt
# coding=utf-8 from numpy import * import matplotlib.pyplot as plt def my_scatter(dataMat): x = dataMat[:, 1] y = dataMat[:, 2] plt.xlabel('x') plt.ylabel('y') plt.scatter(x.tolist(), y.tolist()) plt.show() def file2matrix(filename): f = open(filename) contents = f.readlines() length = len(contents) # 得到文件内容的行数 Mat = zeros((length, 3)) # 创建一个空矩阵用于存储文件内容 index = 0 for line in contents: line = line.strip() # 去除每一行的换行符 data = line.split('\t') Mat[index, :] = data # 将每一列数据按照行索引存放到空矩阵 index += 1 return mat(Mat) data_file = "D:\ex0.txt" dataMat = file2matrix(data_file) my_scatter(dataMat)
运行情况: