鱼C论坛

 找回密码
 立即注册
查看: 3355|回复: 8

[吹水] 一个 print("Hello world!") 能玩出几个花样

[复制链接]
发表于 2022-10-5 20:48:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 asky533 于 2022-10-6 20:20 编辑

一个 print("Hello world!") 能玩出几个花样

普普通通式
print("Hello world!")

变量式
text = "Hello world!"
print(text)

函数式
def sayhello():
        print("Hello world!")
sayhello()

模块式
a.py:
def main():
                print("Hello world!")


b.py:
import a
a.main()


参数式
def sayhello(x):
        print(x)
sayhello("Hello world!")


类式
class IO:
    def out(self,text):
        print(text)

stdio = IO()
stdio.out('Hello, World!')

Tensorflow 式
import tensorflow as tf
hello = tf.constant("Hello world!")
sess = tf.Session()
h = sess.run(hello)
print(h.decode())


无 print() 式
#myhello.py
import sys
from time import time
from os import system
def _output(string):
    try:
        for i in string:
            sys.stdout.write(i)
            sys.stdout.flush()
        sys.stdout.flush()
        return 0
    except:
        return 1

class my_io(object):
    def __init__(self):
        self.string = '\0'
    def output(self):
        return _output(self.string)
        
def main():
    global text, mio, errno
    errno = 0
    text = 'Hello World!'
    mio = my_io()
    mio.string = text
    if mio.output():
        errno = 1
        return 1
    return 0

if __name__ == '__main__':
    st = time()
    main()
    en = time()
    exit_output = my_io()
    exit_output.string = f"\n--------------------------------\nProcess exited after {round(en - st, 3)} seconds with return value {errno}\n请按回车继续..."
    exit_output.output()
    sys.stdin.readline()
    sys.exit(errno)


ASCII 编码 + 解包式
print(*[chr(len(' a a a a  a a a a a '.split(' ')[i])*100+len('aaaaaaa    a aaa a a a   aaa'.split(' ')[i])*10+len('aa a aaaaaaaa aaaaaaaa a aa aaaaaaaaa a aaaa aaaaaaaa  aaa'.split(' ')[i])) for i in range(12)], sep = '')


彩蛋式
import __hello__


瞧瞧,一个 Hello world 能玩出多少花样,我们在写程序时也不能一根筋的去写
牢记:条条大路通罗马

评分

参与人数 2荣誉 +7 鱼币 +6 贡献 +3 收起 理由
zhangjinxuan + 2 + 1 鱼C有你更精彩^_^
青出于蓝 + 5 + 5 + 3 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-5 21:25:05 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-5 21:37:21 | 显示全部楼层
class IO:
    def out(self,text):
        print(text)

stdio = IO()
stdio.out('Hello, World!')

评分

参与人数 1荣誉 +4 鱼币 +4 贡献 +3 收起 理由
asky533 + 4 + 4 + 3 感谢指出,已添加

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-6 09:56:53 | 显示全部楼层
本帖最后由 zhangjinxuan 于 2022-10-6 10:07 编辑
#myhello.py
import sys
from time import time
from os import system
def _output(string):
    try:
        for i in string:
            sys.stdout.write(i)
            sys.stdout.flush()
        sys.stdout.flush()
        return 0
    except:
        return 1

class my_io(object):
    def __init__(self):
        self.string = '\0'
    def output(self):
        return _output(self.string)
        
def main():
    global text, mio, errno
    errno = 0
    text = 'Hello World!'
    mio = my_io()
    mio.string = text
    if mio.output():
        errno = 1
        return 1
    return 0

if __name__ == '__main__':
    st = time()
    main()
    en = time()
    exit_output = my_io()
    exit_output.string = f"\n--------------------------------\nProcess exited after {round(en - st, 3)} seconds with return value {errno}\n请按回车继续..."
    exit_output.output()
    sys.stdin.readline()
    sys.exit(errno)
【无print!】

评分

参与人数 1荣誉 +4 鱼币 +4 贡献 +3 收起 理由
asky533 + 4 + 4 + 3 感谢指出,已添加^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-6 10:06:39 | 显示全部楼层
>>> print(*[chr(len(' a a a a  a a a a a '.split(' ')[i])*100+len('aaaaaaa    a aaa a a a   aaa'.split(' ')[i])*10+len('aa a aaaaaaaa aaaaaaaa a aa aaaaaaaaa a aaaa aaaaaaaa  aaa'.split(' ')[i])) for i in range(12)], sep = '')
Hello world!

评分

参与人数 3荣誉 +5 鱼币 +10 贡献 +3 收起 理由
临时号 + 5 ASCII码加解包,玩的挺花
zhangjinxuan + 1 + 1 好厉害!
asky533 + 4 + 4 + 3 感谢指出,已添加^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-6 10:08:08 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-6 11:09:35 | 显示全部楼层

评分

参与人数 2荣誉 +2 贡献 +2 收起 理由
zhangjinxuan + 2 厉害啊!
python爱好者. + 2 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-14 21:55:50 | 显示全部楼层
无print式我服了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-29 14:36:09 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-23 03:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表