Python变量
变量:在程序运行过程中,值会发生变化的量。
Python中的变量不须要申明类型
用“=”号来给变量赋值
每个变量在应用前都必须赋值,变量赋值当前才会被创立。即:新的变量通过赋值的动作,创立并开拓内存空间,保留值。如果没有赋值而间接应用会抛出赋值前援用的异样或者未命名异样。
在Python中,变量自身没有数据类型的概念。即:通常所说的“变量类型”是变量所援用的对象的类型,或者说是变量的值的类型。
“=”号这个赋值运算符是从右往左的计算程序
Python容许同时为多个变量赋值
! 请牢记:Python中的一切都是对象,变量是对象的援用!
2.1. 变量命名规定:
1.不以下划线为结尾,如_user等(有非凡含意);
2.变量命名容易读懂:如user_name
3.不实用规范库中内置的模块名或者第三方的模块名称;
4.不要用python内置的关键字:

In [3]: import keywordIn [4]: keyword.kwlistOut[4]: ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

2.2. 如何了解python变量赋值

s = 'hello'#变量名 s 调配给 hello 这个对象

hello 这个对象会先在内存中创立进去,而后再把变量名 s 调配给 此对象。
所以python中变量赋值,应始终看 等号左边

2.3. 多元赋值

In [8]: n1, n2 =1, 2In [9]: n1Out[9]: 1In [10]: n2Out[10]: 2In [11]: n1 = 1, 2In [12]: n1Out[12]: (1, 2)
In [13]: s1, s2 = '12'  #序列类型数据In [14]: s1Out[14]: '1'In [15]: s2Out[15]: '2'
In [16]: num, s = [10, 'hello'] #列表In [17]: numOut[17]: 10In [18]: sOut[18]: 'hello'

2.4. python中的判断

In [1]: n =10In [2]: n == 10    #判断n等于10Out[2]: True       #条件为真,返回TrueIn [3]: n != 10    #判断n不等于10Out[3]: False      #条件为假,则返回FalseIn [4]: n > 10     #大于Out[4]: FalseIn [5]: n < 10     #小于Out[5]: FalseIn [6]: n >= 10Out[6]: TrueIn [7]: n <= 10Out[7]: True
In [10]: n = input("请输出一个数字>>:")请输出一个数字>>:10In [11]: n == 10Out[11]: FalseIn [12]: nOut[12]: '10'   #'10'字符串

上述判断会发现返回后果为 False
在编程语言中,数据是有类型之分的。
input() 在承受到任何数据都会成为 字符串类型(str),即一般字符串
而等号左边的 10 是整型(int)

如:In [13]: '10' == 10Out[13]: FalseIn [14]: '10' > 10---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-14-a2286912985a> in <module>----> 1 '10' > 10TypeError: '>' not supported between instances of 'str' and 'int'

以上就是本次分享的全部内容,当初想要学习编程的小伙伴欢送关注Python技术大本营,获取更多技能与教程。