1 / 39
文档名称:

Python基础教程(自学记录).docx

格式:docx   大小:1,359KB   页数:39页
下载后只包含 1 个 DOCX 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

Python基础教程(自学记录).docx

上传人:儒林 2022/4/20 文件大小:1.33 MB

下载得到文件列表

Python基础教程(自学记录).docx

文档介绍

文档介绍:Python基础教程(自学记录)
快速改造:基础知识

在IDLE编辑器,在提示符后输入help然后按回车;也可以按下F1获得有关IDLE的帮助信息

1/2返回0,整除除法;'str' and 'int' objects
>>> print 'The number is:'+`tmp`
The number is:42
>>> print 'The number is:'+str(tmp)
The number is:42
>>> print 'The number is:'+repr(tmp)
The number is:42
input和raw_input的比较
>>> name=input("What's your name:")
What's your name:Gumby
Traceback (most recent call last):
File "<pyshell#60>", line 1, in <module>
name=input("What's your name:")
File "<string>", line 1, in <module>
NameError: name 'Gumby' is not defined
>>> name=input("What's your name:")
What's your name:'Gumby'
后面输入的字符串增加了引号不报错。
>>> input('Enter a number:')
Enter a number:3
3
>>> name=input("What's your name:")
What's your name:'Gumby'
>>> raw_input("What's your name:")
What's your name:Gumby
'Gumby'
>>> raw_input("What's your name:")
What's your name:Gumby
'Gumby'
>>> raw_input('Enter a number:')
Enter a number:3
'3'
>>>
、原始字符串和unicode
(1)长字符串 使用三引号;转义的反斜杠用于行尾
>>> print 'hello, \
world!'
hello, world!
>>> print '''hello,
world!'''
hello,
world!
>>> 1+2+3\
+4
10
(2)原始字符串,对于反斜线并不会特殊对待,以r开头,注意字
符串尾部
>>> print 'c:\nowhere'
c:
owhere
>>> print r 'c:\nowhere'
SyntaxError: invalid syntax
>>> print 'c:\nowhere'
c:
owhere
>>> print r'c:\nowhere'
c:\nowhere
>>> print r"This is illegal\"
SyntaxError: EOL while scanning string literal
>>> print r"This is illegal\\"
This is illegal\\
>>> print r"This is illegal" "\\"
This is illegal\
(3)Unicode在字符串前增加前缀U
>>> print u'hello, world'
hello, world
列表和元组
序列中的每个元素被分配一个序号--à即元素的位置,也被称为索引。第一个索引为‘0’,最后一个元素可以使用-1标记

Python包含6中内建的序列:列表,元组,字符串,unicode字符串,buffer对象和xrange对象。
列表和元组的主要区别:列表可以修改,元组则不能。内建函数返回元组。几乎所有情况下都可以使用列表代替元组。特殊情况之一:使用元组作为字典的键,因为键不可以更改,所以不能用列表。
列表的各个元素通过逗号进行分隔,写在方括号内。
>>> edward=['Edward Gumy',42]
>>> john=['John Smith',50]
>>> database=[edward,john]
>>> database
[['Edward Gumy', 42], [