python 知识点整理(一)
本文只是对python部分知识点进行学习和整理
本篇主要是针对python的基本信息的总结
本文目录
interpreter and compiler 解释器和编译器
compiler: C/C++ (一种代码语言到另一种代码语言 再到平台进行运行操作 产物为 [另一种代码])
interpreter: Python (直接进行相关的代码操作 产物为 [运行结果])
elements of python
variables/reserved words
存放在memory的一个命名空间 可以存储数据并被修改数据
valid syntax patterns语法模式
program -interactive/script
python flow (mainly in part2)
- sequential flow
- conditional flow -if /elif/else
- repeated flow
constants & variable
constants
- fixed values
- string constants use the (“”) or (‘’)
rules for defining variables in Python
部分名字不可以被称作变量名:
eg: False True None and as assert break ···
statement /assignment
assignment statement
can be retrieved from located memory 值会被改变
x=10
print(x)
x=100-10+x*3-x/10
print(x)
cascaded assignment 级联
multiple variables can be set as the same value by using single assignment statement
z=y=x=2+7+2
x,y,z
simultaneous assignment
value of two variables can be changed
a="deepsecret"
b="you'll never guess"
b,a=a,b #exchange the vari
c=12
d=34
d,c=c,d#exchange
augmented assignment 增量赋值
x+=1 等价于 x=x+1
x-=2x7 等价于 x=x-(2x7)
x1=22
x1+=7
order evaluation (operator precedence)运算优先级
- highest to lowest
- parenthesis are always with highest priority ()最高优先级
- power **乘积
- multiplication division remainder 乘除余
- addition subtraction 加减
- left to right
division : floor division&divmod
- three different kinds of division:
- floor division(//)
- divition
- divmod()
for the floor division, pay attention to the data type
#floor division
print(143//25)
print(143.4//25)#float
print(9//2.5)#float
5
5.0
3.0
#int
print(256//10)
#float
print(256/10)
#商&余数
divmod(143,25)#5,18
25
25.6
(5, 18)
data type
type matters
- Some operations are prohibited on certain types
- cannot ‘add1’ to the string
- check the type by type()
- type of variable can be changed and will be determined by the value that is last assigned to the variable
type conversion
- Use int() & float() to convert other data type to integer & float
- convert numbers into strin using function str()
string operations
- ‘+’ 连接
- '*'多项连接
str1='abc'
str2='def'
str3='hij'
str1+str2+str3
'abcdefhij'
input & output
input():get a string
eval()
返回传入字符串的表达式的结果。就是说:将字符串当成有效的表达式 来求值 并 返回计算结果。
eval函数就是实现list、dict、tuple与str之间的转化,同样str函数把list,dict,tuple转为为字符串
string="12+7"
print(string)
print(eval(string))
print(string,"=",eval(string))
12+7
19
12+7 = 19