Python 中 Hello World 的 10 种写法
干了一件比较蛋疼的事情,收集了一下 Python 中 Hello World 的 10 种写法。
Python 中 Hello World 的 10 种写法
再推荐你一个–JPype
帖子标题问的是Python里打印"Hello World"的十种方法。这哥们儿估计是想看看Python的灵活性,或者就是个趣味挑战。行,我直接上代码,从最基础的到一些“花活”都列出来,每种写法都保证能跑。
# 写法1:最经典,没有之一
print("Hello World")
# 写法2:用变量存一下
message = "Hello World"
print(message)
# 写法3:字符串拼接(虽然这里没啥好拼的)
part1 = "Hello"
part2 = "World"
print(part1 + " " + part2)
# 写法4:用f-string格式化(大材小用但很流行)
print(f"{'Hello'} {'World'}")
# 写法5:使用字符串的format方法
print("{} {}".format("Hello", "World"))
# 写法6:古老一点的%格式化
print("%s %s" % ("Hello", "World"))
# 写法7:写到标准错误,而不是标准输出(控制台看起来一样,但流不同)
import sys
sys.stderr.write("Hello World\n")
# 写法8:利用os.system调用系统echo(这有点脱裤子放屁了,而且平台依赖)
import os
os.system('echo Hello World')
# 写法9:通过内置的__import__搞点奇怪的(绝对不推荐,纯属好玩)
getattr(__import__('builtins'), 'print')("Hello World")
# 写法10:最后来个递归的(同样,毫无必要但能跑)
def print_hello(n):
if n > 0:
print_hello(n-1)
else:
print("Hello World", end='')
print_hello(1)
print() # 补个换行
核心就第一种,其他都是图一乐,展示下语法特性。真要写代码,无脑选第一种就行。
总结:知道第一种就够了,其他都是炫技。
我不小心联想到了 茴香豆的 4 种写法
加上一种,py2
(lambda _, , , ____, , , ______, ________:
getattr(
import(True.class.name[] + [].class.name[]),
().class.eq.class.name[:] +
().iter().class.name[:_]
)(
_, (lambda _, __, ___: (, __, __))(
lambda , __, :
chr( % __) + (, __, ___ // __) if ___ else
(lambda: ).func_code.co_lnotab,
_ << ___,
((( << ) + ) << (( << ____) - )) + ((((( << )
- ) << ) + ) << (( << ) + ( << ))) + ((( <<
__) - ) << ((((( << ) + )) << ) + ( << ))) + (((
<< ) + ) << (( << ) + )) + ((( << ) - ) <<
(( << ))) + ((( << ) - ) << (((( << ) + ) <<
) - )) - ( << (((( << __) - ) << __) + )) + (
<< ((((( << ___) + _)) << _))) - (((((( << ) + )) << ) +
) << (((( << ) + ) << ))) + ((( << ) - ) <<
((((( << ) + )) << ))) + ((( << ) + ) << (( <<
))) + ( << _____) + ( << ___)
)
)
)(
*(lambda _, __, ___: (, __, ___))(
(lambda , , _:
[([(lambda: _).func_code.co_nlocals])] +
(, __, ___[(lambda _: _).func_code.co_nlocals:]) if ___ else []
),
lambda _: _.func_code.co_argcount,
(
lambda _: _,
lambda _, __: _,
lambda _, __, ___: _,
lambda _, __, ___, ____: _,
lambda _, __, ___, ____, _____: _,
lambda _, __, ___, ____, _____, ______: _,
lambda _, __, ___, ____, _____, ______, _______: _,
lambda _, __, ___, ____, _____, ______, _______, ________: _
)
)
)
对不起,我根本不懂 py
import hello
这个很少人知道吧?
Python 不是将就只用一种方法做事么?
有点强行了。。。
疼
#3 我觉得我要专门撸个轮子帮我在 IDE 里数下划线的字符个数
- ctypes
这个好像不对吧,python 里面字符串是一个 list,你这样传给 c 的函数,c 只能接受一个 h,后面的是没法传递过去的。
这个是会自动转换的
> None, integers, longs, byte strings and unicode strings are the only native Python objects that can directly be used as parameters in these function calls. None is passed as a C NULL pointer, byte strings and unicode strings are passed as pointer to the memory block that contains their data (char * or wchar_t *). Python integers and Python longs are passed as the platforms default C int type, their value is masked to fit into the C type.
https://docs.python.org/2/library/ctypes.html
我是 python3,确实是这样。python2 估计是自动转换的了。
[!ctypes_demo]( http://xray.qiniudn.com/ctypes_demo.png)
也想到了茴香豆。。。。
#3 有 3 的版本吗 
py 不是跑友的意思么?
我觉得挺好的,收藏了,尤其是 python 调用 c 的代码
学到了
666666
最装逼的一种写法你没列出
import hello


