
hello_world.py。.py 指出这是一个 Python 文件,因此编辑器将使用 Python 解释器来运行它。python 代码解读复制代码message = 'Hello Python world!'
print(message)message 的变量。python 代码解读复制代码message = 'Hello Python world!'
print(message)
message = 'Hello Python Crash Course world!'
print(message)hello_world.py,使其再打印一条消息。print。就目前而言,应使用小写的 Python 变量名。虽然在变量名中使用大写字母不会导致错误,但是大写字母在变量名中有特殊含义。
字符串 就是一系列字符。Python 中用引号括起来的都是字符串,其中的引号可以是单引号,也可以是双引号。python 代码解读复制代码name = 'ada lovelace'
print(name.title())title() 以首字母大写的方式显示每个单词,即将每个单词的首字母都改为大写。python 代码解读复制代码print(name.upper())
print(name.lower())python 代码解读复制代码first_name = 'ada'
last_name = 'lovelace'
full_name = f"{first_name} {last_name}"
print(full_name)f,再讲要插入的变量放入花括号内。f字符串 可完成很多任务,如利用与变量关联的信息来创建完整的消息,如下所示:python 代码解读复制代码first_name = 'ada'
last_name = 'lovelace'
full_name = f"{first_name} {last_name}"
print(f"{full_name.title()}")
f字符串是 Python 3.6 引入的。如果你使用的是 Python 3.5 或更早的版本,需要使用format()方法,而非这种f语法。
python 代码解读复制代码full_name = "{} {}".format(first_name, last_name)
print(full_name)\t\n开头 和 末尾 多余的空白。要确保字符串 末尾 没有空白,可使用方法 rstrip()。python 代码解读复制代码>>> favourate_language = 'python '
>>> favourate_language
'python '
>>> favourate_language.rstrip()
'python'
>>> favourate_language
'python '
>>>这种删除只是暂时的,要永久删除这个字符串中的空白,必须将删除操作的结果关联到变量。
python 代码解读复制代码>>> favourate_language = favourate_language.rstrip()
>>> favourate_language
'python'
>>>lstrip() 和 strip()。语法错误是一种你时不时会遇到的错误。程序中包含非法的 Python 代码时,就会导致语法错误。
python 代码解读复制代码>>> universe_age = 14_000_000_000
>>> universe_age
14000000000
>>>python 代码解读复制代码>>> x, y, z = 0, 0, 0python 代码解读复制代码MAX_CONNECTIONS = 5000vbnet 代码解读复制代码>>> import this
The Zen of Python, by Tim Peters
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.
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!
>>>本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。