Python01-注释、变量、数据类型、输出、输⼊、转换数据、运

2024-05-20  本文已影响0人  喂_balabala

注释

注释分为两类:==单⾏注释== 和 ==多⾏注释==。

只能注释⼀⾏内容,语法如下:

# 注释内容

可以注释多⾏内容,⼀般⽤在注释⼀段代码的情况, 语法如下:

"""
 第⼀⾏注释
 第⼆⾏注释
 第三⾏注释
"""
'''
 注释1
 注释2
 注释3
'''
特殊注释
#!/usr/bin/env python3

关于脚本第一行的 #!/usr/bin/python 的解释,相信很多不熟悉 Linux 系统的同学需要普及这个知识,脚本语言的第一行,只对 Linux/Unix 用户适用,用来指定本脚本用什么解释器来执行。

有这句的,加上执行权限后,可以直接用 ./ 执行,不然会出错,因为找不到 python 解释器。

#!/usr/bin/python 是告诉操作系统执行这个脚本的时候,调用 /usr/bin 下的 python 解释器。

#!/usr/bin/env python 这种用法是为了防止操作系统用户没有将 python 装在默认的 /usr/bin 路径里。当系统看到这一行的时候,首先会到 env 设置里查找 python 的安装路径,再调用对应路径下的解释器程序完成操作。

#!/usr/bin/python 相当于写死了 python 路径。

#!/usr/bin/env python 会去环境设置寻找 python 目录,可以增强代码的可移植性,推荐这种写法。

分成两种情况:

(1)如果调用 python 脚本时,使用:

python script.py
#!/usr/bin/python 被忽略,等同于注释

(2)如果调用python脚本时,使用:

./script.py
#!/usr/bin/python 指定解释器的路径

PS:shell 脚本中在第一行也有类似的声明。

变量

标识符

标识符命名规则是Python中定义各种名字的时候的统⼀规范,具体如下:

False None True and as assert break class 
continue def del elif else except finally for
from global if import in is lambda nonlocal
not or pass raise return try while with 
yield

命名习惯

使⽤变量

变量名 = 值 1

my_name = '法外狂徒张三'
print(my_name)

数据类型

a = 1
print(type(a)) # <class 'int'> -- 整型
b = 1.1
print(type(b)) # <class 'float'> -- 浮点型
c = True
print(type(c)) # <class 'bool'> -- 布尔型
d = '12345'
print(type(d)) # <class 'str'> -- 字符串
e = [10, 20, 30]
print(type(e)) # <class 'list'> -- 列表
f = (10, 20, 30)
print(type(f)) # <class 'tuple'> -- 元组
h = {10, 20, 30}
print(type(h)) # <class 'set'> -- 集合
g = {'name': 'TOM', 'age': 20}
print(type(g)) # <class 'dict'> -- 字典

输出

print('hello Python')
age = 18
print(age)

格式化输出

格式符号 转换
%s 字符串
%d 有符号的⼗进制整数
%f 浮点数
%c 字符
%u ⽆符号⼗进制整数
%o ⼋进制整数
%x ⼗六进制整数(⼩写ox)
%X ⼗六进制整数(⼤写OX)
%e 科学计数法(⼩写'e')
%E 科学计数法(⼤写'E')
%g %f和%e的简写
%G %f和%E的简写
技巧
age = 18
name = 'TOM'
weight = 75.5
student_id = 1

# 我的名字是TOM
print('我的名字是%s' % name)

# 我的学号是0001
print('我的学号是%4d' % student_id)

# 我的体重是75.50公⽄
print('我的体重是%.2f公⽄' % weight)

# 我的名字是TOM,今年18岁了
print('我的名字是%s,今年%d岁了' % (name, age))

# 我的名字是TOM,明年19岁了
print('我的名字是%s,明年%d岁了' % (name, age + 1))

# 我的名字是TOM,明年19岁了
print(f'我的名字是{name}, 明年{age + 1}岁了')

转义字符

\n :换⾏。
\t :制表符,⼀个tab键(4个空格)的距离。

结束符

在Python中,print(), 默认⾃带 end="\n" 这个换⾏结束符,所以导致每两个 print 直接会换⾏
展示,⽤户可以按需求更改结束符。

print('输出的内容', end="\n") 

输入

input("提示信息")
password = input('请输⼊您的密码:')
print(f'您输⼊的密码是{password}')
# <class 'str'>
print(type(password))

转换数据

函数            说明
int(x [,base ]) 将x转换为⼀个整数
float(x ) 将x转换为⼀个浮点数
complex(real [,imag ]) 创建⼀个复数,real为实部,imag为虚部
str(x ) 将对象 x 转换为字符串
repr(x ) 将对象 x 转换为表达式字符串
eval(str ) ⽤来计算在字符串中的有效Python表达式,并返回⼀个对象
tuple(s ) 将序列 s 转换为⼀个元组
list(s ) 将序列 s 转换为⼀个列表
chr(x ) 将⼀个整数转换为⼀个Unicode字符
ord(x ) 将⼀个字符转换为它的ASCII整数值
hex(x ) 将⼀个整数转换为⼀个⼗六进制字符串
oct(x ) 将⼀个整数转换为⼀个⼋进制字符串
bin(x ) 将⼀个整数转换为⼀个⼆进制字符串
# 1. float() -- 转换成浮点型
num1 = 1
print(float(num1))
print(type(float(num1)))

# 2. str() -- 转换成字符串类型
num2 = 10
print(type(str(num2)))

# 3. tuple() -- 将⼀个序列转换成元组
list1 = [10, 20, 30]
print(tuple(list1))
print(type(tuple(list1)))

# 4. list() -- 将⼀个序列转换成列表
t1 = (100, 200, 300)
print(list(t1))
print(type(list(t1)))

# 5. eval() -- 将字符串中的数据转换成Python表达式原本类型
str1 = '10'
str2 = '[1, 2, 3]'
str3 = '(1000, 2000, 3000)'
print(type(eval(str1)))
print(type(eval(str2)))
print(type(eval(str3)))

运算符

算数运算符

运算符 描述 实例
+ 1 + 1 输出结果为 2
- 1-1 输出结果为 0
* 2 * 2 输出结果为 4
/ 10 / 2 输出结果为 5
// 整除 9 // 4 输出结果为2
% 取余 9 % 4 输出结果为 1
** 指数 2 ** 4 输出结果为 16,即 2 * 2 * 2 * 2
() ⼩括号 ⼩括号⽤来提⾼运算优先级,即 (1 + 2) * 3 输出结果为 9

混合运算优先级顺序: () ⾼于 ** ⾼于 * / // % ⾼于 + -

赋值运算符

运算符 描述 实例
= 赋值 将 = 右侧的结果赋值给等号左侧的变量

单个变量赋值

num = 1
print(num)

多个变量赋值

num1, float1, str1 = 10, 0.5, 'hello world'
print(num1)
print(float1)
print(str1)

多变量赋相同值

a = b = 10
print(a)
print(b)

复合赋值运算符

运算符 描述 实例
+= 加法赋值运算符 c += a 等价于 c = c + a
-= 减法赋值运算符 c -= a 等价于 c = c- a
*= 乘法赋值运算符 c *= a 等价于 c = c * a
/= 除法赋值运算符 c /= a 等价于 c = c / a
//= 整除赋值运算符 c //= a 等价于 c = c // a
%= 取余赋值运算符 c %= a 等价于 c = c % a
**= 幂赋值运算符 c ** = a 等价于 c = c ** a
a = 100
a += 1
# 输出101 a = a + 1,最终a = 100 + 1
print(a)

b = 2
b *= 3
# 输出6 b = b * 3,最终b = 2 * 3
print(b)

c = 10
c += 1 + 2
# 输出13, 先算运算符右侧1 + 2 = 3, c += 3 , 推导出c = 10 + 3
print(c)

⽐较运算符

运算符 描述 实例
== 判断相等。如果两个操作数的结果相等,则条件结果为真(True),否则条件结果为假(False) 如a=3,b=3,则(a == b) 为 True
!= 不等于 。如果两个操作数的结果不相等,则条件为 真(True),否则条件结果为假(False) 如a=3,b=3,则(a == b) 为 True如a=1,b=3,则(a != b) 为 True
> 运算符左侧操作数结果是否⼤于右侧操作数结果,如果⼤于,则条件为真,否则为假 如a=7,b=3,则(a > b) 为 True
< 运算符左侧操作数结果是否⼩于右侧操作数结果,如果⼩于,则条件为真,否则为假 如a=7,b=3,则(a < b) 为 False
>= 运算符左侧操作数结果是否⼤于等于右侧操作数结果,如果⼤于,则条件为真,否则为假 如a=7,b=3,则(a < b) 为 False如a=3,b=3,则(a >= b) 为 True
<= 运算符左侧操作数结果是否⼩于等于右侧操作数结果,如果⼩于,则条件为真,否则为假 如a=3,b=3,则(a <= b) 为 True
a = 7
b = 5
print(a == b) # False
print(a != b) # True
print(a < b) # False
print(a > b) # True
print(a <= b) # False
print(a >= b) # True

逻辑运算符

运算符 逻辑表达式 描述 实例
and x and y 布尔"与":如果 x 为 False,x and y 返回False,否则它返回 y 的值。 True and False, 返回False。
or x or y 布尔"或":如果 x 是 True,它返回 True,否则它返回 y 的值。 False or True, 返回True。
not not x 布尔"⾮":如果 x 为 True,返回 False 。如果 x为 False,它返回 True。 not True 返回 False, not False 返回 True
a = 1
b = 2
c = 3
print((a < b) and (b < c)) # True
print((a > b) and (b < c)) # False
print((a > b) or (b < c)) # True
print(not (a > b)) # True

拓展

a = 0
b = 1
c = 2
# and运算符,只要有⼀个值为0,则结果为0,否则结果为最后⼀个⾮0数字
print(a and b) # 0
print(b and a) # 0
print(a and c) # 0
print(c and a) # 0
print(b and c) # 2
print(c and b) # 1
# or运算符,只有所有值为0结果才为0,否则结果为第⼀个⾮0数字
print(a or b) # 1
print(a or c) # 2
print(b or c) # 1
上一篇 下一篇

猜你喜欢

热点阅读