python的变量和简单数字类型详解
作者:CV_Begineer 发布时间:2021-01-10 15:34:40
标签:python,变量,简单数字类型
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!
来源:https://blog.csdn.net/cd2000hzb/article/details/120277543
0
投稿
猜你喜欢
- 对于大前端来说,JS可谓是我们的神器,从页面的效果到数据的传递,再到后台的业务,无处不充斥着JS的身影,但是万能的JS也有贪玩的时候,某一时
- 对Python字符串,除了比较老旧的%,以及用来替换掉%的format,及在python 3.6中加入的f这三种格式化方法以外,还有可以使用
- 1.sort.Sort介绍使用sort.Slice进行排序,因为slice把struct抽象化了,且slice封装过了,简单的基础类型可以使
- 本教程中将详细阐述传统的图形设计元素是如何应用在现代(2.0时代)的页面设计中的,然后我将解释为什么它们能流行开来,以及如何、何时、在哪里使
- 1.列表(List)元组是由一对方括号构成的序列。列表创建后,可以根据自己的需要改变他的内容>>> list=[1,2,3
- 错误类型: Microsoft JET Database Engine (0x80040E10) 至少一个参数没有被指定值。 原因:在写SQ
- 说明本文根据https://github.com/liuchengxu/blockchain-tutorial的内容,用python实现的,
- system函数 说明:执行外部程序并显示输出资料。 语法:string system(string command, int [retur
- 代码如下: <% Dim oConn, ors, aRows Dim i,j Set oConn=Server.CreateObjec
- 我们知道在PHP4里面,当我们使用一个类的时候,必须将该类所以文件使用include或require加进来,这样我们就可以使用类了。而对于P
- InstrRev描述:返回某字符串在另一个字符串中出现的从结尾计起的位置。语法:InstrRev(string1, string2
- 最近的项目涉及到很多表单的制作,特别是复选框(checkbox)和单选框(radio)。但是在前端开发过程中发现,单(复)选框和它们后面的提
- Sybase于2008年11月4日在大中华区用户大会上宣布,联手神州数码金程(北京)科技有限公司对旗下领先的SQL Anywhere数据库进
- 第一种情况:有RAID,还需要做数据库备份吗?回答:需要。有了RAID,万一部份磁盘损坏,可以修复数据库,有的情况下数据库甚至可以继续使用。
- super()函数可以用于继承父类的方法,语法如下:super(type[, object-or-type])虽然super()函数的使用比
- 代码很简洁,功能很实用,这里就不多废话了,直接奉上:<?php/** * 获取客户端IP * @param&nbs
- 无意中看到一位学员的屏保,感觉挺有意思的,就把它实现了下来效果如下:<!DOCTYPE html PUBLIC "-//W3
- 平时自己写了很多代码,但从没好好计算总共写了多少行,面试时被问起来,就傻了。。。闲来无事,写个python程序来统计下import os##
- 笔者电脑系统是win7,同时安装了Python2.7和Python3.6,但是在通过命令行直接使用“pip install XXX”安装Py
- 本文实例讲述了Python图像处理之直线和曲线的拟合与绘制。分享给大家供大家参考,具体如下:在数据处理和绘图中,我们通常会遇到直线或曲线的拟