一。字符串
1。双引号与单引号灵活应用
str0 = 'I told my friend, "Python is my favorite language!"' str1 = "The language 'Python' is named after Monty Python, not the snake." str2 = "One of Python's strengths is its diverse and supportive community." print(str0) print(str1) print(str2)
2。修改大小写
name = "ada Lovelace" # 非永久修改 print(name.title()) print(name) print("\n") print(name.upper()) print(name) print("\n") print(name.lower()) print(name) print("\n") # 永久修改 name = name.title() print(name) # 首字母大写 name = name.upper() print(name) # 全部大写 name = name.lower() print(name) # 全部小写
3。格式化输出
first_name = "ada" last_name = "lovelace" full_name = f'{first_name} {last_name}' print(f"Hello, {full_name.title()}!")
4。制表符与换行符
print("Languages:\n\tPython\n\tC\n\tJavaScript")
5。删除空白
favorite_language = ' python ' print(favorite_language.rstrip()) # 删除尾空白,非永久 print(favorite_language) print(favorite_language.lstrip()) # 删除头空白,非永久 print(favorite_language) print(favorite_language.strip()) # 删除头尾空白,非永久 print(favorite_language)
二。数
1。运算
f1 = 3 * 1.414 ** 2 # 结果自动转换为浮点型,**为乘方运算 print(f1) ######################### # 计算结果--精确位数不确定 # 5.998187999999999 #########################
2。数字分组--下划线
universe_age = 14_000_000_000 print(universe_age)
3。多变量同时赋值
x, y, z = 1, 2, 3 print(f"x = {x}, y = {y}, z = {z}")
4。常量
PI = 3.1415926 print(f'pi = {PI}')
三。Python之禅
写程序的原则很重要,需经常对照体会。理念:简约而清晰。
>>> 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!
标签:name,language,python,编程,favorite,better,--,print,than From: https://www.cnblogs.com/duju/p/16747855.html