iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python的变量和简单数字类型详解
  • 175
分享到

python的变量和简单数字类型详解

2024-04-02 19:04:59 175人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录1.变量1.1使用变量名时避免命名错误2.字符串2.1修改字符串大小写的方法2.2合并字符串2.3使用制表符或换行符来添加空白2.4删除空白2.5使用字符串时需要避免语法错误3.

1. 变量

  • 每个变量都存储了一个值
  • 在程序中可以随时修改变量,但Python将始终记录变量的最新值

message = "Hello Huang ZB!"
print(message)
message = "Goodbye Huang ZB!"
print(message)

1.1 使用变量名时避免命名错误

查看Traceback明白错误


message = "Hello Huang ZB!"
print(mesage)

2.字符串

Def:字符串就是一串字符。双引号、单引号都可表示

2.1 修改字符串大小写的方法


name = "huang zhibin"
print(name.title())            #title()函数作用:将每个单词首字母改为大写

Huang Zhibin

其他方法:


name = "huang zhibin"
print(name.title())   #title()函数作用:将每个单词首字母改为大写
print(name.upper())   #upper()函数作用:将字符串内容全部转换为大写
print(name.lower())   #lower()函数作用:将字符串内容全部转换为小写

Huang Zhibin
HUANG ZHIBIN
huang zhibin

2.2 合并字符串

方法:拼接


first_name = 'huang'
last_name = 'zhibin'
full_name = first_name + ' ' + last_name
print('Hello, ' + full_name.title() + '!')    #这个 + 不可或缺

Hello, Huang Zhibin!

2.3 使用制表符或换行符来添加空白

  • 在字符串中添加制表符,使用 \t (也可以理解为进位符)

print("python")
print("\tpython")             # \t 表示制表符

python
python

在字符串中添加换行符,使用 \n


print("Languages:\nPython\nC\njavascript")       # \n 表示换行符

Languages:
Python
C
JavaScript

同一字符串中可以同时包含制表符和换行符 字符串" \n\t ": 让python换到下一行


print("Languages:\n\tPython\n\tC\n\tJavaScript")

Languages:
Python
C
JavaScript

2.4 删除空白

  • python能够找出字符串开头和末尾多余的空白,为确保开末尾无空白,使用方法 rstrip()
  • 为确保开开头无空白,使用方法 lstrip()
  • 同时剔除字符串两端的空白,使用方法 strip()

infORMation = '    人生苦短,我学python    '
print(information.rstrip())
print(information.lstrip())
print(information.strip())

​ 人生苦短,我学python

人生苦短,我学python #右边空格依然存在!

人生苦短,我学python

2.5 使用字符串时需要避免语法错误

再修改程序时语法错误也是一个重要的检查指标

3. 数字类型

3.1 整数


>>> 2+3
5
>>> 5-6
-1
>>> 4*5
20
>>> 36/6
6.0
>>> 3**2
9
>>> 2+2**2
6
>>> (2+2)*2
8

3.2 浮点数


>>> 0.2+0.3
0.5
>>> 0.2-0.3
-0.09999999999999998

保留两位小数


print ('{:.2}'.format(变量))

3.3 复数


>>> 2+6j
(2+6j)
>>> (2+6j).real
2.0
>>> (2+6j).imag
6.0

3.4 使用函数str()避免类型错误


age = 21
message = "Happy " + str(age) + "rd Birthday!"     #将非字符串值转化为字符串
print(message)

Happy 21rd Birthday!

4 .注释

单行注释

#

多行注释

‘''

注释不能嵌套!!!!!​

5 .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!

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!

--结束END--

本文标题: python的变量和简单数字类型详解

本文链接: https://www.lsjlt.com/news/135669.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作