目录
1. 变量
1.1 变量定义
变量是一个可以赋值的量,他可以取到不同类型的数据,比如整数型int (integer前三个字母),浮点型(float),还有字符串。
变量必须是由数字、字母(区分大小写)以及下划线(_)组成,且开头不能是数字,不能与关键字(if, else, while, elif …)重复。
>>> a=3
>>> a
3
>>> _b=3+3 #3+3的结果赋值给b
>>> _b
6
>>> a3_c=10*5 #10*5的结果赋值给a3_c,但是这个变量名不太合适
>>> a3_c
50
>>> _b+_ #后面的下划线表示上面的值
56
变量命名尽量能够一目了然能够看出具体的意义,如若由两个单词则可以使用下划线(_)来间隔。
>>> first_name='Max'
>>> last_name="Wang"
>>> student_height=180
相等符号"=“用来给变量赋值(但不是相等的意思),如果说某个变量等于某个值用==表示相等(布尔值会涉及到)。
>>> width=10 #赋值给变量width
>>> height=20 #赋值给height
>>> width*height #直接使用变量的赋值得到最后的结果
200
>>> price=100
>>> discount=0.8
>>> price*discount
80.0
>>> m,n,p=3,2,1#多个变量同时赋值
>>> m
3
>>> n
2
>>> p
1
>>> a=5+3 #把5+3=8赋值给a
>>> b=6+10 #把6+10赋值给b
>>> a+b
24
1.2 报错
如果没有提前给变量赋值,则该变量是未定义的变量,不能直接使用
“>>> a+b # try to access undefined variables
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a+b' is not defined”
1.3 关键词
关键词是python程序中已经使用的标识符,是固定不可变的。
比如:if, else, elif, while, import …
2. 输入输出
2.1 输入
input(),可以直接输入需要的内容
>>> input('Please enter something:') #括号中可以把单引号‘ ’中的内容原文输出,并且直接输入内容即可
Please enter something:I am Max, I am new to python.
'I am Max, I am new to python.' #显示输入的内容
>>> input("----->You are great, tell me your name:") #同样可以用双引号
----->You are great, tell me your name:Max
'Max'
2.2 输出
print(),可以直接打印输出得到的结果
>>> print(3+4) #直接输出结果
7
#变量a和b赋值后,直接打印输出a+b的结果
>>> a=3
>>> b=3.1415926
>>> print(a+b)
6.1415926
>>> print(a,b)#可以同时输出多个结果,print(obj1,obj2,obj3,...)
3 3.1415926
>>> print('I am Max, I am new to python')#原文输出,不带双引号
I am Max, I am new to python
3 数字型
数字型数据主要是涉及到数字的类型的数据,比如整数,小数等等。
3.1 整数型(int)
整数型的数据只能取到整数
>>> id(int) #int在内存的位置
4305048320
>>> dir(int) #dir可以输出int的内置函数
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
int()可以作为一个函数,可以把输入的数据变成整数。
>>> type(3) #type返回3的类型为int
<class 'int'>
>>> int(3.2) #int()可以把一个数的小数部分去掉取整
3
>>> int(-4.5) #int()可以把一个数的小数部分去掉取整
-4
>>> int('123') #可以把字符串变成整数型
123
可以进行第一篇文章里的运算,不再肇述。
3.2 浮点型(float)
浮点型可以说是小数,取到的值都会加上小数部分。
>>> type(2.0) #type输出数据的类型
<class 'float'>
>>> type(3.1234532464357)
<class 'float'>
>>> float(2) #float()可以把整数型变成浮点型
2.0
>>> float('1234') #可以把字符串变成浮点型
1234.0
>>> round(3.25456,3) #round(num, k)可以把小数保留k个小数位
3.255
>>> round(2.2345) #round(a)返回的是近似之后的整数
2
>>> round(-2.35345)
-2
3.3 复数型(complex)
复数表示以及运算在复数已经总结过了
3.4布尔型(booleans)
3.4.1 True and False
布尔型的数据只有 True 和 False,同时True可以用1表示, False用0表示。是返回输入的结果是为True或者False.
>>> True=1 #True不能赋值
SyntaxError: can't assign to keyword
>>> 1>2 #1>2是错误的,返回False
False
>>> 3>-1 #3>-1是正确的,返回True
True
>>> True #本身就是True
True
>>> False #本身就是False
False
>>> 2==2
True
>>> 2!=3
True
>>> 4/2!=2 #!=表示不等,返回的结果是False
False
3.4.2 and , or, not
很多时候需要判断的不只有一个,会同时判断多个,通过 and、or、not运算:
and运算是与运算,只有所有都为True,and运算结果才是True。
-or运算是或运算,只要其中有一个为True,or运算结果就是True。not运算是非运算,它是一个单目运算符,把True变成False,False变成True。
| Ture | False |
|---|---|
| True and True | True and False |
| True or True | False and False |
| True or False | False or False |
>>> 1==1 and 2==2 #都为正确,返回True
True
>>> 1==1 and 2==1 #任何一个错误都返回False
False
>>> 3<2 and 4!=4 #都为错,返回False
False
>>> 1==1 or 2==1 #只要有一个正确返回True
True
>>> 3<2 or 4!=4 #都错误,返回False
False
>>> 1 and 0 #1是True,0是False
0
>>> 1 and 1
1
>>> 0 and 0
0
>>> 1 or 0
1
>>> 0 or 0
0
>>> not 1==1 #not表示对立的
False
>>> not 3>2
False
>>> not 4<3
True
Ture 和False是很多地方都会用到,经常用在条件判断。
例如:
age=int(input()) #int把输入的数字转化为整数型
if age >= 18:
print('adult')
else:
print('teenager')
3.5比较特殊的:空值
空值是Python里一个特殊的值,用None表示。None不能理解为0,因为0是有意义的,而None是一个特殊的空值。
Python中会把下面几种情况当做空值来处理:
None‘ ‘[],(),{}False
>>> a=None
>>> a
>>> #把None赋值给a,返回为空
>>> None==0 #返回值为False,则None不为0
False
>>> None=='' #返回值为False,则None不为‘’
False
>>> None==[] #返回值为False,则None不为[]
False
>>> None=={} #返回值为False,则None不为{}
False
>>> a=None #把None赋值给a
>>> if a==None:
print('Right')
Right
本文介绍了Python中的变量定义、错误处理、关键词概念,重点讲解了整数型、浮点型、复数型和布尔型数字类型的特性和用法,包括变量命名规则、输入输出操作以及空值的特殊含义。
1098

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



