- 输入:input --> python提供的可以和程序进行交互的一个方法。
- 无论输入的内容是什么,输出的数据类型永远是字符串
- type() -->可以查看数据类型
- 出现input的程序,如果不给input做操作(回车),程序会一直等下去
str1 = input('请输入:')
print(str1)
print(type(str1))
- 输出:print --> python提供的能够查看数据的方法。
a. 可以打印数据
num = 1 print(num, 12, 45, 'abcde', True, False)
b. 可以修改打印结果的样式
- sep – 分隔字符串;参数默认值为空格
print(1, 2, 3, 4, 1+2) print(1, 2, 3, 4, 5, sep='♠')
- end – 结束字符串;参数默认值为换行n
print(1, end='n') print(2, end='-') print(3)
- sep 和 end 必须放到print的末尾。
- 字符串:文本,概念:一串有限个数的符号的合集。
print('小明今年', 20, '岁')
- 旧式的格式化字符串:%s、%d、%f等
- str % values --> str :字符串; values:向字符串中传入的值;%:传入值的固定语法
- %s对应字符串,%d对应整型,%f对应浮点型
- 字符串中的%s、%d、%f可以理解为占位符
- values如果只传入一个值,直接写到%后即可;如果传入多个值,使用小括号括起来
- %f默认保留6位小数,保留一位小数:%.1f
- 旧式的格式化字符串可以将传入的任何类型的数据转换为字符串
name = '小明'
age = 20
str1 = 'name今年age岁'
print(str1)
str2 = '%s今年%d岁' % (name, age)
print(str2)
print('%f' % 1.1)
print('%.1f' % 1.1)
print(type('%.1f' % 1.1))
- format的用法
- format不但能格式化整体字符串,还可以单独格式化传入的内容
a. >、<、^ --> 右对齐、左对齐、居中对齐
b. : --> 左边为空,默认对传入值进行操作
c. >3 --> 传入值的长度小于3,右对齐,左边使用空格补全
d. 0>3 --> 传入值的长度小于3,右对齐,左边使用0补全
id = 1
stu_id = 'python{}'.format(id)
print(stu_id)
stu_id1 = 'python{:>3}'.format(id)
stu_id2 = 'python{:0>3}'.format(id)
print(stu_id1)
print(stu_id2)
stu_id3 = 'python{:<3}'.format(id)
stu_id4 = 'python{:0<3}'.format(id)
print(stu_id3)
print(stu_id4)
stu_id5 = 'python{:0^2}'.format(id)
print(stu_id5)
- % --> 将传入的值转换成百分数,自动进行四舍五入。
num = 0.123456789
str4 = '{:.2%}'.format(num)
print(str4)
- , --> 千分位符
str5 = '我有{:,}元'.format(100000000)
print(str5)
- 格式化字符串字面值(f-字符串)
- 在字符串前面添加一个f或者F,字符串中变得位置使用{},{}中放变量。
id = 1
str6 = f'python{id}'
print(str6)
str7 = f'python{id:0>3}'
print(str7)
python之禅
- 什么是Python之禅?
- 在Python中,有一首诗,也被称为Python的「八荣八耻」,这首诗的名字被叫做eython之狮》。为的是告诉大家,如何优美的编写代码。
- Python之禅在哪里?
- 在Python的交互式解释器中,输入import this即可调用。
import this print(this)这首诗,希望每一位python程序员都能牢记于心。
- Beautiful is better than ugly.(优美胜于丑陋。)
- Explicit is better than implicit.(显式胜于隐式。)
- Simple is better than complex.(简单胜于复杂。)
- Complex is better than complicated.(复杂胜于难懂。)
- Flat is better than nested.(扁平胜于嵌套。)
- Sparse is better than dense.(分散胜于密集。)
- Readability counts.(可读性应当被重视。)
- Special cases aren’t special enough to break the rules.Although practicality beats purity.(尽管实⽤性会打败纯粹性,特例也不能凌驾于规则之上。)
- Errors should never pass silently.Unless explicitly silenced.(除⾮明确地使其沉默,错误永远不应该默默地溜⾛。)
- In the face of ambiguity, refuse the temptation to guess.(⾯对不明确的定义,拒绝猜测的诱惑。)
- There should be one-- and preferably only one --obvious way to do it.(⽤⼀种⽅法,最好只有⼀种⽅法来做⼀件事。)
- Although that way may not be obvious at first unless you’re Dutch.(虽然⼀开始这种⽅法并不是显⽽易⻅的,但谁叫你不是 Python 之⽗呢。)
- Now is better than never.Although never is often better than right now.(做⽐不做好,尽管⽴⻢去做有时还不如不做。)
- If the implementation is hard to explain, it’s a bad idea.(如果实现很难说明,那它是个坏想法。)
- If the implementation is easy to explain, it may be a good idea.(如果实现容易解释,那它有可能是个好想法。)
- Namespaces are one honking great idea – let’s do more of those!(命名空间是个绝妙的想法,让我们多多地使⽤它们吧!)



