2-1 简单消息
message = "I love python"
print(message)
输出
I love python
2-2 多条简单消息
message = "I love python"print(message)message = "I love C++, too"print(message)
输出
I love pythonI love C++, too
2-3 个性化消息
name = "Eric"print("Hello "+name+", would you like to learn some python today?")
输出
print("Albert Einstein once said, \"A person who never made a mistake never tried anything new.\"")
Hello Eric, would you like to learn some python today?
2-4 调整名字的大小写
name = "huang sheng"print(name.lower())print(name.upper())print(name.title())
输出
huang shengHUANG SHENGHuang Sheng
2-5 名言
print("Albert Einstein once said, \"A person who never made a mistake never tried anything new.\"")
输出
Albert Einstein once said, "A person who never made a mistake never tried anything new."
2-6 名言2
name="Albert Einstein"message="\"A person who never made a mistake never tried anything new.\""print(name+" once said, "+message)
输出
Albert Einstein once said, "A person who never made a mistake never tried anything new."
2-7 剔除人名中的空白
name="\n\tAlbert Einstein\t\n"print(name)print("------")print(name.lstrip())print("------")print(name.rstrip())print("------")print(name.strip())
输出
Albert Einstein ------Albert Einstein ------ Albert Einstein------Albert Einstein
2-8 数字8
print(7+1)print(9-1)print(2*4)print(16/2)
输出
8888.0
2-9 最喜欢的数字
favorite_number = 26message="My favorite number is "+str(favorite_number)+"."print(favorite_number)print(message)
输出
26My favorite number is 26.
2-10 添加注释
favorite_number = 26message="My favorite number is "+str(favorite_number)+"." #str()函数将数字转换成字符,以便输出print(favorite_number)print(message)
name = "huang sheng"print(name.lower()) #lower()函数将所有字符变成小写print(name.upper()) #upper()函数将所有字母变成大写print(name.title()) #title()函数将首字母大写
2-11 Python之禅
import this
输出
The Zen of Python, by Tim PetersBeautiful 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!