什么是变量
变量就是对内存地址空间的一个引用
在 python 中,每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建
等号(=)用来给变量赋值
=左边是一个变量名
=右边是存储在变量中的值
变量名 = 值
变量名的命名规则
一.在定义变量时,为了保证代码格式, = 的左右两边该各保留一个空格
二.在python中,如果变量名需要由两个或多个单词组成,可以按照以下方式命名(python中常用)
1.每个单词都使用小写字母
2.单词与单词之间使用_下划线连接
3.例如:first_name,last_name….
驼峰命名法:(其他语言常用的命名法)
当变量名是由两个或多个单词组成,还可以利用驼峰命令法来命名
小驼峰命名法
第一个单词以小写字母开始,后续单词的首字母大写
firstName lastName
大驼峰命名法
每一个单词的首字母都采用大写字母
FirstName LastName
python中变量的数据类型
整型——int
a = 1
print(a)
print(type(a))
1
<class ‘int’>

浮点型——float
b = 2.5
print(type(b))
<class ‘float’>

字符串型——str
c = ‘westos’
print(type©)
<class ‘str’>

布尔型——bool(非0即真,只要值不为0,即为真)
d = 1
print(bool(d))
d = 0
print(bool(d))
d = ‘redhat’
print(bool(d))
True
False
True

数据类型的转化
>>> a = 1
>>> type(a)
<class 'int'> ##a的类型是整形
>>> float(a) ##改变a的类型为浮点型
1.0
>>> type(a)
<class 'int'> ##再次查看,a的类型依旧为整形,说明上一次改变a的类型在内存中被释放
>>> b = float(a) ##重新开辟一个内存空间为b,让b=a转换后的浮点型a
>>> b
1.0
>>> type(b)
<class 'float'> ##此时b的类型为浮点型
>>> b = 2.0
>>> int(b)
2
>>> c = 'redhat'
>>> int(c) ##报错,含有字母的字符串不能转化为整型
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'redhat'
>>> b = 123
>>> str(b) ##整型可以转化为字符串
'123'
>>> c = '123'
>>> int(c) ##纯数字的字符串可以转化为整型
123
在内存中删除一个变量——关键字del
>>> del a
>>> a
Traceback (most recent call last): ##变量a在内存中不存在,报错
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> del b
>>> b
Traceback (most recent call last): ##变量b在内存中不存在,报错
File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
python中的输入输出
python2.x:
input():只支持正确的数值类型(整型和字符串)
raw_input():数值和字符串(全部被当作字符串处理)
>>> input('Num:')
Num:2
2
>>> input('Num:')
Num:1.2
1.2
>>> input('Num:')
Num:redhat ##输入为字符串时,报错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'redhat' is not defined
>>> raw_input('Num:')
Num:2
'2'
>>> raw_input('Num:')
Num:1,2
'1,2'
>>> raw_input('Num:')
Num:redhat
'redhat'
>>>
python3.x
input():接收任意数据类型,没有raw_input()
>>> input('Num:')
Num:2
'2'
>>> input('Num:')
Num:abc
'abc'
>>> input('Passwd:')
Passwd:123
'123'
>>> import getpass ##导入getpass包
>>> num = getpass.getpass('请输入密码:') ##输出内容不回显
请输入密码:
>>> print(num)
123
##如果接收到的数值要进行比较的时候,一定要转换为同一种类型
>>> age = input('age:') ##此时输入的类型为字符串
age:19
>>> age
'19'
>>> age > 18 ##字符串和整型进行比较,报错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'
>>> age = int(age) ##转化为整型进行比较
>>> age
19
>>> age > 18
True
>>> age = int(input('age:')) ##直接进行转化
age:18
>>> age
18
deployment.extensions/httpd rolled back
格式化输出
%s:代表字符串 %d:整型
>>> name = 'westos'
>>> name
'westos'
>>> age = 12
>>> print('%s的年龄是%d' %(name,age))
westos的年龄是12
>>> age = 18
>>> print('%s的年龄是%d' %(name,age))
westos的年龄是18
>>> age = '19'
>>> print('%s的年龄是%d' %(name,age))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str
>>> age = 19.5
>>> print('%s的年龄是%d' %(name,age))
westos的年龄是19
%f:浮点型 %.3f:保留三位小数
>>> money=23121312.32314432
>>> name = 'Tom'
>>> print('%s的工资为%f' %(name,money))
Tom的工资为23121312.323144
>>> money=60000
>>> print('%s的工资为%f' %(name,money))
Tom的工资为60000.000000
>>> money=60000.99
>>> print('%s的工资为%f' %(name,money))
Tom的工资为60000.990000
>>> print('%s的工资为%.2f' %(name,money))
Tom的工资为60000.99
>>> money=60000
>>> print('%s的工资为%.3f' %(name,money))
Tom的工资为60000.000
整数的占位:不够的位数 前面补0 %.5d小数点前面5位,不够补0
>>> sid = 1
>>> name = 'lily'
>>> print('%s的学号为000%d' %(name,sid))
lily的学号为0001
>>> sid = 2
>>> print('%s的学号为000%d' %(name,sid))
lily的学号为0002
>>> sid = 10
>>> print('%s的学号为000%d' %(name,sid))
lily的学号为00010
>>> print('%s的学号为%.5d' %(name,sid))
lily的学号为00010
>>> sid = 1
>>> print('%s的学号为%.5d' %(name,sid))
lily的学号为00001
>>> sid = 20
>>> sid = 100
>>> print('%s的学号为%.5d' %(name,sid))
lily的学号为00100
百分号的实现
>>> scale = 0.1
>>> print('数据的比例是:%.2f' %(scale))
数据的比例是:0.10
>>> print('数据的比例是:%.2f' %(scale * 100))
数据的比例是:10.00
>>> print('数据的比例是:%.2f%' %(scale * 100))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: incomplete format
>>> print('数据的比例是:%.2f%%' %(scale * 100))
数据的比例是:10.00%
算术运算符
在python2.x和python3.x中,加减乘时一样的,除不一样
python2.x中的除:
>>> 5/2 ##两个数为整型,结果也为整形
2
>>> 100/300
0
>>> 5.0/2 ##其中一个为浮点型,则结果也是浮点型
2.5
>>> 100/300.0
0.3333333333333333
python3.x中的除:
>>> 5/2 ##两个数为整型,结果为浮点型
2.5
>>> 100/300
0.3333333333333333
>>>
取余
>>> 5%2
1
取整
>>> 5//2
2
自加
>>> a = 1
>>> a = a+1
>>> a
2
>>> a += 1
>>> a
3
逻辑运算符
and
1 and 2
两个条件同时满足 就返回True
只要有一个条件不满足,就返回False
or
1 or 2
两个条件只要满足一个 就返回True
两个条件都不满足,就返回False
1584



被折叠的 条评论
为什么被折叠?



