变量
变量(variable) 是指代表某个值的名字,例如:>>>x = 3 就是用x代表3,这种操作也叫做赋值 ,变量一旦赋值之后,就可以在表达式中使用变量。
在python中变量赋值更加随性,也不用事先声明变量。
基本类型
- 整型:42,39…
- 长整型:42L,1000L
- 浮点型:42.5,42.5e-2
- 复合型:77+4j
- 字符串:‘football’,‘hello’
- Unicode:u’foo’,u"bar"
1)变量名可以包括字母、数字和下划线(-),但不能以数字开头;
2)两个整数相除,计算结果的小数部分被截除,只留下整数部分,如果两个数中有一个为浮点数,则计算结果亦为浮点数。
字符串
单引号字符串和转义字符串
python在打印字符串时通常用单引号和双引号,将文本内容括起来。
// 单引号
>>>'hello!world'
'hello!world'
//双引号
>>>"hello!world"
'hello!world'
当出现歧义字符串时,需要对字符串进行转义:
>>>"let\'s go"
"let's go"
#双引号同理
拼接字符串
>>> "hello"+"world"
'helloworld'
>>> x="hello"
>>> y=".world"
>>> x+y
'hello.world'
str和repr
之前所有通过python打印出来的字符串都被引号括起来了,只有通过str()和repr()这两个函数才能将值转换成合理形式的字符串。
#str()
>>>temp = 42
>>>print("the temp is:" +str(temp))
the temp is:42
#repr()
>>> temp = 42
>>> print('the temp is:' +repr(temp))
the temp is:42
模块
可以把模块想象成导入到python以增强其功能的扩展。需要使用特殊的命令import 来导入模块。
>>>from math import sqrt
>>>sqrt(9)
3.0
函数
python3.9.3内置函数文档: link.