python编程从入门到实践--第2章 变量和简单数据类型

一。字符串

  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)     # 全部小写
复制代码

 

1
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!
复制代码
1
<br><br><br><br>
posted @   獨懼  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示