# 怎样理解pycharm中的默认代码:

怎样理解pycharm中的默认代码:

def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint.

Press the green button in the gutter to run the script.

if name == 'main':
print_hi('PyCharm')

#问题:
#1. 函数中的print(f'Hi, {name}'),怎么理解这里的f ?
#2. name == 'main',怎么理解?

可以看成是一种标志,加了f,则’ '内中出现了{ }括起来的表达式,则{xxx}会外面被传过来的数据替换掉;如果没有,则直接输出Hi, {name}.
例子

img

img

""" 
CSDN : heart_6662
PYTHON amateur   
"""
def print_hi(name):
    print(f'Hi,{name}')

if __name__ == '__main__':
    print_hi('Pycharm')
    print_hi('heart_6662')


if name == 'main':
1)如果你学过其他语言肯定知道主函数这个东西,比如c语言 int main(){}
这个就是类似的意思,告诉程序我们在这里开始执行,主函数前可以定义各种函数,类什么的
可以让我们代码简洁
2)验证是不是自己的模块(这个可以先不要懂这么多)
name__就是个验证用的变量,来看下执行的模块是外来户,还是本地人,是本地的都运行;不是本地人的,就运行能够引入的那一部分
参考https://blog.csdn.net/heqiang525/article/details/89879056?ops_request_misc=&request_id=&biz_id=102&utm_term=if%20__name
%20==%20%27__main_%27:&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-0-89879056.pc_search_em_sort&spm=1018.2226.3001.4187

字符串格式化