首页 > 编程语言 >python编程从入门到实践--第2章

python编程从入门到实践--第2章

时间:2022-10-01 21:55:07浏览次数:68  
标签:name language python 编程 favorite better -- print than

一。字符串

  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

相关文章

  • VMware中Ubuntu扩容
    https://blog.csdn.net/weixin_47306605/article/details/121727192?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Ra......
  • 买菜车
    main.c点击查看代码/*!----------------------------------------------------------------------------*PDOA跟随小车控制板程序*@author广州联网科技有限公......
  • CF1144G & CF1693D & CF1647F
    CF1144G:给定一个长度为\(n\)的序列\(A\)。问能否把它拆成一个严格递增序列和一个严格递减序列(可以为空),如果有解则输出方案。\(n\le2\times10^5\)。设\(f_{i,0}......
  • 基于思维导图的情境游戏教学对学生在英语课程中的写作表现、学习感知和消化导出知识的
    基于思维导图的情境游戏教学对学生在英语课程中的写作表现、学习感知和消化导出知识的影响(Impactsofamindmapping-basedcontextualgamingapproachon EFLstuden......
  • 10.1 noip 模拟赛
    10.1noip模拟赛目录10.1noip模拟赛\(\to\rmlink\leftarrow\)\(\rmT1\)猜道路\(\rmT2\)简单环\(\rmT3\)汉明距离\(\rmT4\)勇者的后缀\(\to\rmlink\left......
  • docker-compose + yaml 发布系统:使用总结
    docker-compose使用总结 docker-compose+yaml发布系统:使用总结1.下载安装docker-compose下载curl-Lhttps://get.daocloud.io/docker/compose/releases/download/......
  • 复杂参数
    Map、Model:​​(map、model里面的数据会被放在request的请求域request.setAttribute)​​@GetMapping("/params")publicStringtestParam(Map<String,Object>map,......
  • 参数绑定和自定义参数绑定
    参数绑定当前端页面提交表单,表单数据对应后端实体类,控制层处理请求时,会将表单数据与实体类对象属性自动绑定(即转为对象)#前端:姓名:<inputname="userName"/><br/>年龄:<inp......
  • 参数解析原理
    ctrl+n搜索DispatcherServlet,再搜索doDispatch按住doDispatch,鼠标点击进入protectedvoiddoDispatch(HttpServletRequestrequest,HttpServletResponseresponse)thr......
  • 基本注解
    @PathVariable使用#localhost:8080/car/1/owner/aaa@GetMapping("/car/{id}/owner/{username}")publicMap<String,Object>getCar(@PathVariable("id")Integeri......