介绍
“基础不牢,地动山摇”,新的征程开始前,先把基础打牢。基础梳理系列将从离散数学、python、算法原理等方面并行展开。希望,能够坚持下去,把这份学习记录做好。
系列文章目录
python基础
第一章: 基础梳理-python基础复习-基础知识
第二章: 基础梳理-python基础复习-列表和元组
第三章: 基础梳理-python基础复习-使用字符串
第四章: 基础梳理-python基础复习-字典
第五章: 基础梳理-python基础复习-抽象1
第六章: 基础梳理-python基础复习-抽象2
01 使用python需注意的基本认知
- python2到python3版本变化翻天覆地;
- 若要使用图形用户界面,可启动Python自带的应用程序IDLE;
- 命令行输入help()即可获取解释器使用指南;
- 每行末尾不要加分号;
02 算法的概念
- 算法详尽描述如何完成某种任务;
- 算法由对象和语句组成;
03 数和表达式
- 常见的运算符工作原理与C一致;
>>> 1+2
3
>>> 1*2
2
- 除法的运算结果为浮点数;/
>>> 1/2
0.5
- 有专门的整除运算; //
!!!也适用于浮点数、负数
向下圆整
# python ,注意与C区分
>>> 1//2
0
>>> -1//2
-1
>>> 1.1 // 2
0.0
>>> -1.1 // 2
-1.0
//C, 注意与python区分
int a = 1,b = 2;
float c=0;
c=a/2;
print("c=%f",c);
---------------
0
- 求余(求模)运算符; %
x%y=x-(x//y)*y
!!!也适用于浮点数、负数
向下圆整
>>> 1 % 2
1
>>> -1 % 2
1
>>> 1.1 % 2
1.1
>>> -1.1 % 2
0.8999999999999999
- 求幂; **
>>> 3**3
27
- 进制:十六进制0x__;八进制0o__;二进制0b__
>>> 0xAF
175
>>> 0o88
File "<stdin>", line 1
0o88
^
SyntaxError: invalid digit '8' in octal literal
>>> 0o77
63
>>> 0b11
3
>>> 0b11001
25
04 语句
- 值并非存储在变量中,而是存储在变量指向的计算机内存;
- 多个变量可能指向同一位置;
>>> list1=[1,2,3]
>>> list2=list1
>>> list2[1]=1
>>> list1
[1, 1, 3]
>>> list2
[1, 1, 3]
05 获取用户输入
- input函数以文本、字符串返回
返回输入值= input(“引导字符串”)
引导字符串可忽略
>>> input()
a
'a'
>>> input("请输入")
请输入a
'a'
06 函数
- 函数用于执行特定程序;
- 与自己编写函数,对应的,有:内置函数;
07 模块
- 导入import module_name,使用时module_name.function_name();
>>> import math
>>> sqrt(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sqrt' is not defined
>>> math.sqrt(4)
2.0
- 导入某个模块所有函数from module_name import *,使用时可直接使用function_name(),但有风险:函数名冲突;
>>> from math import *
>>> sqrt(4)
2.0
>>> math.sqrt(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined
- 导入某个模块某些函数from module_name import function1_name(),function2_name(),使用时可直接使用function_name(),但有风险:函数名冲突;
>>> from math import sqrt
>>> sqrt(4)
2.0
>>> math.sqrt(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined
- !!!复数有专门的处理模块:cmath;
- 总结:如引入cmath与math模块的同名函数的冲突很隐蔽,故应坚持使用第一种导入方式;
>>> from math import *
>>> from cmath import *
>>> sqrt(4)
(2+0j)
>>> sqrt(4.0)
(2+0j)
08 保存并自执行
- Unix:在.py文件添加如下代码做为脚本第一行,路径和python之间有一个空格
#!要使用解释器的绝对路径 python
-Windows:双击.py文件即可,要看清楚反馈,在末尾添加如下代码
# 实质,程序在执行完反馈后,等待到任何输入,才完成。
input("Press <enter>")
- 应该保证程序易理解
09 字符串
- 字符串可使用单引号,也可使用双引号括起;
>>> "hello"
'hello'
>>> 'hello'
'hello'
- 当字符串中有引号(单/双)时,与括起字符串的引号(双/单)相区别
>>> "he say:'good'"
"he say:'good'"
>>> 'he say:"good"'
'he say:"good"'
- 字符串内特殊字符(如"")可使用转义符范反斜杠()表示为字符串的一部分;
>>> "he say "good""
File "<stdin>", line 1
"he say "good""
^
SyntaxError: invalid syntax
>>> "he say \"good\""
'he say "good"'
- 函数转换为str用户能看懂;函数repr获得合法python表达式
>>> record="he say \"good\""
>>> record
'he say "good"'
>>> str(record)
'he say "good"'
>>> repr(record)
'\'he say "good"\''
>>> print(record)
he say "good"
- 长字符串两种实现方式如下
1、使用字符串内容改用三引号括起来(形如:’’‘字符串内容 ‘’’)
2、常规字符串跨越多行:行尾加上反斜杠()
>>> record1='''student Li
... is
... very nice!'''
>>> record2='but\
... his teacher
File "<stdin>", line 2
his teacher
^
SyntaxError: EOL while scanning string literal
>>> record2='but\
... his teacher\
... is\
... strict'
>>>
- 原始字符串(形如:r’字符串内容’)
- Unicode、bytes、bytearray
python字符串使用Unicode编码表示文本
大致而言,每个Unicode字符用一个码点表示;
为与C语言互操作或通过网络套接字发送出去,python提出不可变的bytes和可变的bytearray(Unicode编码使得在内存和磁盘中,所有对象都以二进制数字存储,而在如C等编程语言,这些字节完全暴露,字符串也不过是字符序列)
创建:bytes(x=bytes(b’字符串内容’));bytearray(x=bytearray(b’字符串内容’))
bytearray修改:(bytearray_name[索引]=ord(b’替换内容’));!!!替换双方必须等长,ord()函数是指定用于bytearray修改
''' 不可变的bytes'''
>>> b=b'who'
>>> b
b'who'
>>> b[1]=ord(b'a') #bytes不可修改
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'bytes' object does not support item assignment
'''可变的bytearray'''
>>> ba=bytearray(b'who')
>>> ba[1]=ord(b'h') #bytearray可修改
>>> ba
bytearray(b'who')
>>> ba[1]=ord(b'hh') # 能且只能等长替换
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 2 found
- 编码的转换
>>> record1='''student Li
... ... is
... ... very nice!'''
>>> record1
'student Li\n... is\n... very nice!'
ASCII使用对应的数及二进制编码对应、utf-8在字符串包含怪异字符时,差异才体现出来
编码1:编码方式默认为ASCII:“字符串内容”.encode(“编码方式”,“处理错误的方式”);
>>> record1.encode("utf-8")
b'student Li\n... is\n... very nice!'
>>> record1.encode("ascii")
b'student Li\n... is\n... very nice!'
>>> record1.encode("ascii",'ignore')
b'student Li\n... is\n... very nice!'
>>> de=b'student Li\n... is\n... very nice!'
解码1:“字节”.decode()
>>> de.decode()
'student Li\n... is\n... very nice!'
>>> de.decode("utf-8")
'student Li\n... is\n... very nice!'
编码2:bytes(“字符串”,encoding=“编码方式”)
>>> bytes(record1,encoding='ascii')
b'student Li\n... is\n... very nice!'
解码2: str(b’字符串’,encoding=“编码方式”)
>>> str(de,encoding='ascii')
'student Li\n... is\n... very nice!'
总结
以上就是本章内容,本文仅仅梳理了一些浅薄的知识,下一章将继续梳理《列和元组》。
如有错漏,敬请指正