end在python中的用法
一、认识Python编程语言
Python是一种高级的、解释性的编程语言,由荷兰人Guido van Rossum于1989年创建。它被设计为易读易懂的语法风格,并且具有动态类型和垃圾回收等特性。由于其简洁而强大的特点,Python已经成为了应用广泛且备受欢迎的编程语言之一。
二、什么是`end`?
在Python中,`end`是一个可选参数,用于指定打印函数(如`print()`)输出结束之后所要输出的字符。默认情况下,`end`参数被设置为`\n`,即换行符。
三、使用`end`实现多行输出
1. 通过将`end`设置为空字符串来实现打印不换行:
```python
print("Hello,", end='')
python index函数 print("World!")
```
运行结果:Hello,World!
2. 设置其他特殊字符作为结束字符:
```python
print("Hello,", end='******')
print("World!")
```
运行结果:Hello,******World!
四、将多个打印输出放在同一行
通过使用逗号`,`分隔每个要打印输出的内容可以将它们放在同一行。这也可以与 `end` 参数联合使用。
```python
print("Welcome", "to", "Python!", end='\n\n')
```
运行结果:
Welcome to Python!
五、`end`的应用举例
1. 打印九九乘法表:
```python
for i in range(1, 10):
for j in range(1, i+1):
print(f"{j}*{i}={i*j}", end='\t')
print()
```
运行结果:
```
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14"91"21 =211 "**********Encountered Exception**********
==== Naive Bayes Allocations ====
Model: sat_image_raster_svm_20 (is_baseline_model=False is_smooth_output=False is_crater_label=False)
Normalization net uid: ae257943-93bf-11eb-b68d-acde48001122 normalizer = norm.batchnorm.v0
id int64
assoc int64
index_list int32[?]
pixels_count int64[?]
raters string[?]; S450
image_name string; S150
real_angles float32[?]
nearest_landmark bool[?]
distance_to_landmark_meters float32[?]
std_dev float32[?]
unnormalized_log_probs float32[2, ?]
rejection_prob float32[?]
sat_image_quality int64
prediction int64
non_normalized_prediction int64
crater_label bool
prob_true float32
sas_score float32
一个到相关资源的快捷方法是使用Python的内置函数`help()`,以及在互联网上搜索Python相关文档和教程。
六、总结
在Python中,`end`是一个可选参数,用于控制打印函数完成输出后所要输出的字符,默认为换行符。通过合理使用`end`参数,我们可以实现多行输出、将多个打印内容放在同一行,并且可以灵活地设置结束字符的样式。熟练掌握`end`的用法有助于提高代码的可读性和程序运行效率。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论