variables变量
编程的核心概念之一
用变量把数据储存在计算机内存中
举例,
students_count=1000
当我们运行的时候,python解释器会分配一些内存并把1000储存在内存中。变量students_count就像是一个标签,,可以在程序中任何地方使用该标签或者变量。如果打印students_count,运行后会得到1000.
类型
变量有以下几种:这是自己找的
-
整型变量(int):用来存储整数,例如:age = 20。
-
浮点型变量(float):用来存储实数,例如:price = 3.99。
-
字符串型变量(str):用来存储字符串,例如:name = "Tom"。单引号双引号都可以,因人而异。
-
列表型变量(list):用来存储一系列的值,可以是不同的数据类型,例如:my_list = [1, "two", 3.0]。
-
元组型变量(tuple):与列表类似,但是元素的值不能修改,例如:my_tuple = (1, "two", 3.0)。
-
字典型变量(dict):用来存储键-值对,例如:my_dict = {"name": "Tom", "age": 20}。
-
布尔型变量(bool):用来存储布尔值,即True或False,例如:is_student = True。
-
集合型变量(set):用来存储一组不重复的元素,例如:my_set = {1, "two", 3.0}。
Escape Sequences
\”
\: one of the Escape characters
\”:one of the Escape Sequences
其他:如果打“python\''programing, 只会出现python''programing,其余相同
如果是\n,则换行,如图
format strings
可以改为:f可以大写也可以小写,代表format
first = 'a'
last = 'b'
full = f'{len(first)} {2+2}'
print(full)
结果 1 4
string methods
概念
大小写,去掉两边空白(去掉左空白,lstrip;去掉右空白,rstrip;去掉两边空白,strip)
寻找
但是如果改变大小写,本例来说是找不到的,会显示-1
替换
查询是否有该内容,有则true,无则false
Numbers
10%3取余数
x=x+3可以写成x+=3
Working with numbers--python math modules
math — Mathematical functions — Python 3.12.0 documentation
一 math模块(二三省略)
Python官方提供math模块进行数学运算,如指数、对数、平方根和三角函数等运算。math模块中的函数只是整数和浮点数,不包括复数,复数计算需要使用 cmath 模块。
举例,使用前需要先import一下
引用自:python中math函数怎么用 python中math函数有哪些_mob64ca1412b28c的技术博客_51CTO博客
1.1 舍入函数
math模块提供的舍入函数有:math.ceil(a) 和 math.floor(a),math.ceil(a) 用来返回大于或等于 a 的最小整数,math.floor(a) 返回小于或等于 a 的最大整数。另外,Python还提供了一个内置函数 round(a),该函数用来对 a 进行四舍五入计算。
在Python Shell 中运行示例代码如下:
>>> math.ceil(1.4)
2
>>> math.floor(1.4)
1
>>> round(1.4)
1
>>> math.ceil(1.5)
2
>>> math.floor(1.5)
1
>>> round(1.5)
2
1.2 幂和对数函数
math模块提供的幂和对数函数如下所示。
对数运算:math.log(a[, base]) 返回以 base 为底的 a 的对数,省略底数 base,是 a 的自然数 e 的对数。
平方根:math.sqrt(a) 返回a的平方根。
幂运算:math.pow(a, b) 返回 a 的 b 次幂的值。
在 Python Shell 中运行示例代码如下:
>>> math.log(8, 2)
3.0
>>> math.pow(2, 3)
8.0
>>> math.log(8)
2.0794415416798357
1.3 三角函数
math模块中提供的三角函数有如下几种:
math.sin(a):返回弧度a的三角正弦。
math.cos(a):返回弧度a的三角余弦。
math.tan(a):返回弧度a的三角正切。
math.asin(a):返回弧度a的反正弦。
math.acos(a):返回弧度a的反余弦。
math.atan(a):返回弧度a的反正切。
上述函数中a参数是弧度。有时需要将弧度转换为角度,或将角度转换为弧度,math模块中提供了弧度和角度函数。
math.degree(a):将弧度a转换为角度。
math.radians(a):将角度a转换为弧度。
在 Python Shell 中运行示例代码如下:
>>> math.degrees(0.5 * math.pi) # 调用degrees()函数将弧度转换为角度,其中math.pi是数学常量π
90.0
>>> math.radians(180 / math.pi) # 调用radians()函数将角度转换为弧度
1.0
>>> a = math.radians(45 / math.pi) # 将45度角转换为0.25弧度
>>> a
0.25
>>>
>>> math.sin(a)
0.24740395925452294
>>> math.asin(math.sin(a))
0.25
>>> math.asin(0.2474)
0.24999591371483254
>>> math.asin(0.24740395925452294)
0.25
>>>
>>> math.cos(a)
0.9689124217106447
>>> math.acos(0.9689124217106447)
0.2500000000000002
>>> math.acos(math.cos(a))
0.2500000000000002
>>>
>>> math.tan(a)
0.25534192122103627
>>> math.atan(math.tan(a))
0.25
>>> math.atan(0.25534192122103627)
0.25
Type Conversion
打代码的时候注意代码类型一致
比如
x = input("x:")
y = int(x)+1 这里不能是x,因为如果是x,则y='x'+1,加号两侧不一样
print(f"x:{x},y:{y}")
QUIZ
1.what are the primitive types in python?
strings,numbers(integer,floats,complex numbers),booleans
2.
fruit = "Apple"
print(fruit[1])
>>p
3.
fruit = "Apple"
print(fruit[1:-1])
>>ppl
4.
fruit = "Apple"
print(fruit[1:])
>>pple
5.10%3
>>1
6.print(bool("Flause"))
>>True