目录
1. Python安装
a) Python介绍
b) 解释器
c) 交互及运行方式
2. python的输出和输入
a) print
b) input
3. python基础讲解
a) python变量特性+命名规则
b) 注释方法
c) python中“:”作用
d) 学会使用dir( )及和help( )
e) import使用
f) PEP8介绍
4. python数据基本知识
a) python中数据类型
b) 算数运算符
c) 逻辑运算
d) 成员运算符
e) 身份运算符
f) 运算符优先
目前,Python有两个版本,一个是2.x版,一个是3.x版,这两个版本是不兼容的。由于3.x版越来越普及,以下为Python 3.7版本代码,安装见各操作系统上Python的安装。也可以用Anaconda来安装,Anaconda是一个开源的Python包管理器,其包含了conda、Python等180多个科学包及其依赖项,如果只需要某些包,或者需要节省带宽或存储空间,也可以使用Miniconda这个较小的发行版(仅包含conda和 Python),安装见Anaconda的清华下载镜像。
Python解释器就是负责运行Python程序(也即.py文件)的,由于Python语言从规范到解释器都是开源的,所以存在多种解释器:
#!/usr/bin/env python3
$ chmod a+x hello.py
可以打印字符串、整数、计算结果等,需要打印的各项之间用英文逗号“,"隔开,向屏幕输出时遇到逗号“,”会输出一个空格。一个print函数结束时会自动换行。
# print
print('hello', 'xiaoming')
print(300)
print("100 + 200 =", 100+200)
输出如下:
hello xiaoming
300
100 + 200 = 300
输入字符串,并存放到一个字符串变量里。遇到input时会以交互式等待用户输入任意字符串,以“enter”结束输入。input有不提示和提示两种用法:
# input
name1 = input()
name2 = input('please enter the name: ')
输入如下:
he
please enter the name: she
# 变量
a = 'ABC' # 在内存中创建了一个'ABC'的字符串,在内存中创建了一个名为a的变量,并让a指向'ABC'。
b = a # 让b指向a所指向的数据
a = 'XYZ' # 在内存中创建了一个'XYZ'的字符串,并让a指向'XYZ'
print(b) # 此时b仍指向'ABC'
_
的组合,且不能用数字开头。# coding=
#!/usr/bin/python
# -*- coding: -*-
#!/usr/bin/python
# vim: set fileencoding= :
切片操作:从一个序列中提取一部分的方法,支持字符串、列表、字典、元组,一般形式为X[I:J](所提取的内容包括I但不包括J)
各标准用法:如{key:value};for循环、while循环、else代码块的条件后面要跟上:;定义类和定义函数的时候,要跟一个:。
# dir 任意对象的属性和方法列表
print(dir("")) #string对象
print(dir([])) #string对象
import sys
print(dir(sys))#模块对象
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_git', '_home', '_xoptions', 'abiflags', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'set_asyncgen_hooks', 'set_coroutine_wrapper', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions']
>>>help('sys') # 查看 sys 模块的帮助
……显示帮助信息……
>>>help('str') # 查看 str 数据类型的帮助
……显示帮助信息……
>>>a = [1,2,3]
>>>help(a) # 查看列表 list 帮助信息
……显示帮助信息……
>>>help(a.append) # 显示list的append方法的帮助
……显示帮助信息……
Python通过import关键字来导入模块。导入模块这样的设计方便于代码重用、并且可以避免变量名的冲突。import有以下两种用法(里面的模块名就是"模块名.py"文件里的这个文件名):
有一些约定俗成简称,如:
import numpy as np # 使用numpy模块并自定义为np,后续用np.asarray
import tensorflow as tf # 使用tensorflow模块并自定义为tf,后续用tf.Variable
import matplotlib.pyplot as plt #等价于 from matplotlib import pyplot as plt,含义为使用matplotlib模块中的pyplot成员并自定义为plt
PEP8是一个Python代码编码规范,如规定缩进空几个空格、一行最多多少个字符、如何对齐等,遵守代码规范能够提升代码的可读性,使得在大量的Python代码保持一致。包括英文原版在:https://www.python.org/dev/peps/pep-0008/,有一个中文版的博客见:https://blog.csdn.net/ratsniper/article/details/78954852#introduction-%E4%BB%8B%E7%BB%8D,整理的非常好,只是代码里的缩进不是很明显,可以看英文原版里的。
1.23e9,
0.000012可以写成1.2e-5
'
或双引号"
括起来的任意文本。用于判断2个标识符的引用是否一样
https://www.cnblogs.com/uncle-jay/p/7823209.html