2020-02-19 python简介和第一个python程序

import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Python之禅 by Tim Peters --赖勇浩
优美胜于丑陋(Python 以编写优美的代码为目标)
明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似)
简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现)
复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁)
扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套)
间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题)
可读性很重要(优美的代码是可读的)
即便假借特例的实用性之名,也不可违背这些规则(这些规则至高无上)
不要包容所有错误,除非你确定需要这样做(精准地捕获异常,不写 except:pass 风格的代码)
当存在多种可能,不要尝试去猜测
而是尽量找一种,最好是唯一一种明显的解决方案(如果不确定,就用穷举法)
虽然这并不容易,因为你不是 Python 之父(这里的 Dutch 是指 Guido )
做也许好过不做,但不假思索就动手还不如不做(动手之前要细思量)
如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然(方案测评标准)
命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召)

python的运行方式有两种:
第一种:shell方式,shell是交互式的解释器
输入一行命令,解释器就解释运行出相应结果。

>>>myString = ‘hello world’
>>>print (myString)
hello world

第二种:文件方式

  1. 在python的IDE环境中,创建一个以py为扩展名的文件
    2.用python解释器在Shell中运行出结果
#Filename:hello_world.py
print (myString)
hello world

IDE:集成开发环境(IDE,Integrated Development Environment )是用于提供程序开发环境的应用程序,一般包括代码编辑器、编译器、调试器和图形用户界面等工具。说白了,IDE 是开发者创建程序时使用的软件包。如最受 Python 开发者欢迎的 IDE--PyCharm。>

中文分词工具包jieba
在操作系统Shell 下的安装方式为:
pip install jieba

测试为:

>>>python
>>>import jieba
>>>list(jieba.cut('我一把把把把住了'))#cut()是用来为词的
#['我', '一把', '把', '把', '把住', '了']

python输出:print函数,参数可以是变量或者字符串。
-print (变量)
-print (字符串)

python输入:input函数

>>>price = input('input the stock price of apple: ')
#input the stock price of apple: 109
>>>price 
#'109'
>>>tpye (price)
#class 'str'   input函数的放回类型是str
#所以,常常需要用int(),float()这样的函数,返回成我们需要的类型
price = int(input('input the stock price of apple: ')) 
price = eval(input('input the stock price of apple: '))

eval() 函数用来执行一个字符串表达式,并返回表达式的值。

>>>x = 7
>>> eval( '3 * x' )
21

python风格一:
注释问题,python中,注释以“#”开头

python风格二:
\为续行符,当语句较长时使用

>>>#long sentence
>>>if signal =='red' and\
        car =='moving':
                car = 'stop'
        elif signal =='green' and\
        car =='stop':
                 car = 'moving'

无需续行符可直接换行的两种情况:
-小括号、中括号、花括号的内部可以多行书写
-三引号包括下的字符串也可以跨行书写

python风格三:
一行多语句,一般不会这样子做,除非有非常紧密的联系。

>>> x ='today'; y ='is';z = 'moom'; print(x,y,z)
#today is moom

python风格四:
01:增加缩进表示语句块的开始
02:python用相同的缩进表示同级别语句块
03:减少缩进表示语句块的退出

import numpy as np
from scipy.cluster.vq import vq, kmeans, whiten
list1 = [88.0, 74.0, 96.0, 85.0]
list2 = [92.0, 99.0, 95.0, 94.0]
list3 = [91.0, 87.0, 99.0, 95.0]
list4 = [78.0, 99.0, 97.0, 81.0]
list5 = [88.0, 78.0, 98.0, 84.0]
list6 = [100.0, 95.0, 100.0, 92.0]
data = np.array([list1,list2,list3,list4,list5,list6])
whiten = whiten(data)
centroids,_ = kmeans(whiten, 2)
result,_= vq(whiten, centroids)
print(result)      # result可能是[0 1 1 1 0 1]或其他类似列表
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0., 4., 0.1)
plt.plot(t, t, t, t+2, t, t**2)
image.png
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0., 4., 0.1)
plt.plot(t, t,  t, t**2)

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容