Python学习笔记(2)

来源:互联网 发布:plc密码破解软件 编辑:程序博客网 时间:2024/05/22 12:58
Python学习笔记(2)

变量

变量名就像我们现实社会的名字,把一个值赋值给一个名字时,它会存储在存储中,称之为变量(Variable),在大多数语言中,都把这种行为称为“给变量赋值”或“把值存储在变量中”。

而Python与大多数其他计算机语言的做法稍有不同,它并不是把值存储在变量中,而更像是把名字贴在值上面。

所以有些Python程序员会说“Python”没有“变量”,只有“名字”。

变量的命名理论可以取任何合法的名字,但是作为一个优秀的程序员,请将变量取一个专业点的名字。

# 将字符串'Python'赋值给study>>> study = 'Python'# 打印study>>> print(study)'Python'

可以看到,赋值就是这么简单。之所以这个叫变量,是因为“它”是可变的

>>> study = 'Python'>>> study'Python'>>> study = 'Java'>>> study'Java'

看吧,换个赋值,study就变成“Java”了

 

其实变量就像小学学的代数一样,x = 1, y = 2求 x + y是多少。不用想x + y = 3啦,变量就是这么好理解

>>> x = 1>>> y = 2>>> print(x + y)3

 

当然也可以将字符串赋值给变量,然后变量相加进行拼接

>>> a = 'yy'>>> b = 'hh'>>> a + b'yyhh'

 

如果变量没有赋值,直接打印变量,就会报错哦

>>> nameTraceback (most recent call last):  File "<pyshell#15>", line 1, in <module>    nameNameError: name 'name' is not defined

 

需要注意的地方

Python的变量名不能以数字和特殊字符开头

>>> $a = 'a'SyntaxError: invalid syntax>>> 5a = 'a'SyntaxError: invalid syntax

 

Python可以使用BIF来命名变量,但是最后不要用,不然赋值将会覆盖BIF自己的特性

# 打印呵呵>>> print('hehe')hehe# 觉得打印太麻烦了,把print赋值吧。>>> print = 'hehe'# 直接打印print,可以成功打印>>> print'hehe'# 我不想hehe了,我想haha,结果使用print打印时,报错。这就是BIF来命名变量的下场>>> print('haha')Traceback (most recent call last):  File "<pyshell#41>", line 1, in <module>    print('haha')TypeError: 'str' object is not callable

 

字符串

到目前为止,我们所认识的字符串就是引号内的一切东西,我们野把字符串叫做文本,文本和数字截然不同的

# 整型相加>>> 2 + 24# 字符串相加>>> '2' + '2''22'# 整型与字符串相加,报错>>> 2 + '2'Traceback (most recent call last):  File "<pyshell#2>", line 1, in <module>    2 + '2'TypeError: unsupported operand type(s) for +: 'int' and 'str'

 

如果变量加上了引号,那就不是变量了,而是字符串。所以操作变量和字符串拼接的时候,要注意了

>>> x = 1>>> y = 2# 直接打印x + y>>> print(x + y)3# 如果想打印x + y = x + y 的话,就不能这样操作了。>>> print('x + y = x + y')x + y = x + y# 正确应该这样写,由于x + y得到的值是整型,而整型与字符串不能相加,所以要将整型转换成字符串>>> print('x + y = ' + str(x + y))x + y = 3

 

所以要告诉Python在创建一个字符串时,就要在字符串两边加上引号,可以是单引号或者双引号,Python表示不挑剔的。但是必须是成对的,不能一边单引号,另一半确实双引号结尾。Python它会“愤怒”的告诉你“SyntaxError”。

>>> 'Python"SyntaxError: EOL while scanning string literal

 

如果字符串中需要出现单引号或者双引号怎么办?

—例如我想打印字符串:Lets go !

 

有两种方法,第一种方法比较常用,就是使用我们的转义符号(\)对字符串中的引号进行转义:

>>> print('Let\'s go!')Let's go!

 

还有一种方法就是双引号中,使用单引号

>>> print("Let's go!")Let's go!

 

多行字符串

多行字符串可以使用一对三引号来表示

>>> print('''aaaaabbbbbcccccddddd''')aaaaabbbbbcccccddddd

 

Python中,无法使用中文的引号来表示字符串,不然会报错的。

>>> “aaa”SyntaxError: invalid character in identifier

 

学习总结

这次学习了变量与字符串

需要注意的是:

变量不要以BIF来命名,否则会覆盖了BIF的特性

字符串不能用中文引号来表示,不然会报错

    </div>    <div class = "postDesc">posted @ <span id="post-date">2015-01-06 10:25</span> <a href='http://www.cnblogs.com/yyhh/'>﹏猴子请来的救兵</a> 阅读(<span id="post_view_count">...</span>) 评论(<span id="post_comment_count">...</span>)  <a href ="http://i.cnblogs.com/EditPosts.aspx?postid=4205424" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(4205424);return false;">收藏</a></div></div><script type="text/javascript">var allowComments=true,isLogined=false,cb_blogId=210495,cb_entryId=4205424,cb_blogApp=currentBlogApp,cb_blogUserGuid='21c31e00-f0c9-e311-8d02-90b11c0b17d6',cb_entryCreatedDate='2015/1/6 10:25:00';loadViewCount(cb_entryId);</script>


刷新评论刷新页面返回顶部
$(function () { loadNewsAndKb(); loadBlogSignature(); LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid); GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate); loadOptUnderPost(); GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate); setTimeout(function () { incrementViewCount(cb_entryId); }, 50);});
</div><!--end: forFlow --></div><!--end: mainContent 主体内容容器--><div id="sideBar">    <div id="sideBarMain">
0 0
原创粉丝点击