Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python基础入门-1 #2

Open
millzhang opened this issue Mar 1, 2018 · 0 comments
Open

Python基础入门-1 #2

millzhang opened this issue Mar 1, 2018 · 0 comments

Comments

@millzhang
Copy link
Owner

millzhang commented Mar 1, 2018

前言:基于《Python编程:从入门到实践》一书的笔记摘要

命令行命令

  1. 查看版本:python -v
  2. 执行命令:python
  3. 退出Python返回终端:exit()或在windows中ctrl+z然后回车。
  4. 安装格式化工具:& python -m pip install -U yapf

vs code安装pylint出错

参考:http://www.cnblogs.com/Loonger/p/7815335.html

错误:

Exception:
Traceback (most recent call last):
  File "C:\Users\lupn\AppData\Local\Programs\Python\Python36\lib\site-packages\pip\compat\__init__.py", line 73, in console_to_str
    return s.decode(sys.__stdout__.encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1 in position 43: invalid start byte

During handling of the above exception, another exception occurred:

分析是编码问题,直接修改__init__.py文件中第73行的utf-8gbk

变量的命名规则

  • 变量名只能包含字母、数字和下划线。变量名可以字母或下划线打头,但不能以数字打头;
  • 变量名不能包含空格,但可使用下划线来分隔其中的单词;
  • 不要将 Python 关键字和函数名用作变量名,即不要使用 Python 保留用于特殊用途的单词;
  • 变量名应既简短又具有描述性;
  • 慎用小写字母 l 和大写字母 O ,因为它们可能被人错看成数字 1 和 0。

字符串

  • 引号可以是单引号也可以说双引号

方法

  • title()以首字母大写的方式显示每个单词
  • upper(),将字符串字母全部大写
  • lower(),将字符串字母全部小写
  • rstrip(),去除尾部空格
  • lstrip(),去除头部空格
  • strip(),去除首尾空格
  • endswith(),以指定字符串结尾

整数

  • +-*/加减乘除
  • 3**2表示3的2次方
  • 整型转字符串str()

浮点数

带小数点的数统称为浮点数。

列表(数组)

  • arr.append(),将元素附加到列表末尾
  • arr.insert(index,element),在列表任何位置添加新元素
  • del element,从列表中删除元素
  • arr.pop(),删除列表末尾的元素,返回删除的元素
  • arr.pop(index),删除指定下标的元素,返回删除的元素
  • arr.remove(element),删除元素值
  • arr.sort(),对列表进行永久性排序,(按首字母)
  • arr.sort(reverse=True),反排序
  • sorted(arr),对列表进行临时排序,不会修改列表的值
  • arr.reverse(),反转列表
  • len(arr),获取列表的长度

每当需要访问最后一个列表元素时,都可使用索引 -1 。这在任何情况下都行之有效,即便你最后一次访问列表后,其长度发生了变,仅当列表为空时,这种访问最后一个元素的方式才会导致错误。

列表解析

根据已有列表,高效创建新列表的方式
列表解析是Python迭代机制的一种应用,它常用于实现创建新的列表,因此用在[]中

语法:

 1. [expression for iter_val in iterable]
  2. [expression for iter_val in iterable if cond_expr]
# 列表解析
import os

for item in range(1,21):
    print(item)

arr = range(1,1000000)
#for item in arr:
#    print(item)

print(sum(arr))

js = range(1,20,2)
#for item in js:
#    print(item)

for item in range(3,31,3):
    print(item)

squre = [value**3 for value in range(1,10)]
print(squre)

files = [file for file in os.listdir('./') if file.endswith('.py')]
print(files)

列表切片

语法:arr[start:end]

  • [:3]->0,1,2
  • [2:]->2~结尾
  • [1,3]->1,2
  • [-3:]->最后三个元素
  • [:]->从头到尾,相当于复制列表

数值列表

  • range(from,to,step),创建范围内的数字,前闭后开
  • list(range(from,to)),将range函数转为列表
  • min(arr),求最小值
  • max(arr),求最大值
  • sum(),求和

元组

Python将不能修改的值称为不可变的,而不可变的列表称为元组。元组使用圆括号来标识。
(2,3),也可通过索引来访问。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant