Python基础教程(自学记录)

上传人:公**** 文档编号:512143890 上传时间:2023-09-09 格式:DOCX 页数:39 大小:89.26KB
返回 下载 相关 举报
Python基础教程(自学记录)_第1页
第1页 / 共39页
Python基础教程(自学记录)_第2页
第2页 / 共39页
Python基础教程(自学记录)_第3页
第3页 / 共39页
Python基础教程(自学记录)_第4页
第4页 / 共39页
Python基础教程(自学记录)_第5页
第5页 / 共39页
点击查看更多>>
资源描述

《Python基础教程(自学记录)》由会员分享,可在线阅读,更多相关《Python基础教程(自学记录)(39页珍藏版)》请在金锄头文库上搜索。

1、第一章 快速改造:基础知识1.2交互式解释器在IDLE编辑器,在提示符后输入help然后按回车;也可以按下F1获得有关IDLE的帮助信息Tile Edi t Shell Dtlbug Opti gm Wi nd&w Helppnon z.7.id (aetatiitr May 23 2 01b, 03::32) rJ5C v-ibao 32 ri (Intel)j wi 32Type ,C3EyrrgktH! F w ere lit 311 or M license (M f 3r nare inf oriia-xor. helpTipe helf (j f cr in.teTa5tj.ve

2、helpr or help (abjes) for help ahout object- kelp (Welcome tc Python 2.7! This is -he aEilme help二亡y.If this is y3tzr firsr *iroe nsing Pyrhein, you shoulcs definitely check isuit: Z-ne.ezi the :nHexiiE: at http : dJ /decs . sythan ozg/2:. */* utcial/ .Znter t;ne sazae at any mcciuleF Jce3mrd2 5z to

3、pzc 3 get i&_p cn writing ?yt;i!ion pragrsiLS and using Pjhen nizidinleg - To qul his help utilj.cy and. return tc ths 二nt巴工hb二er # jjst type 而仲.To get a 工二二 of 二l=bJ_u modulesr kuywuzrd与/ 口二 二u9二03中 typu *me-dule r Wkeyward。1*caplcaf+. Each raadj.lti dilao can曰巴 Hflch u aiLt-lr.e工yof uhau it: does;

4、 list the mcdileE vhsse sjnmarres cnDali a given word such as sp=”, type: Hjicciules Hpam.help sqzzlq Eytllor. d口二 umu口Uet二口n f cund for 1 z,qr 8help printThe 11Pl:工匚t 3tatenent1.4数字和表达式1/2返回0,整除除法;1.0/2返回0.5,用一个或者多个包含小数点的数字参与计算。另外改变除法的执行方式:from_future_import division可以实现整除,1.0/2.0返回0.0 %取余数;*募运算; 1

5、/2# 1.0/20.5 1.0/2.00.0 10%31 9*(2)1 9*(1.0/2)3.0 2.75%0.50.25 -9%43 -3%21 -3/2-21.1.1 长整数普通整数不能大于 2147483647也不能小于-2147483648,若更大 的数,可以使用长整数。长整数结尾有个 L,理论上小写也可以,不 过为了便于识别,尽可能用大写。1.01.1.2 十六进制和八进制0XAF 返回 175 ,十六进制;010 返回8,八进制 0xAF175 01081.5 变量包含字母、数字和下划线。首字母不能是数字开头。1.8 函数Pow计算乘方:pow (2,3), 2*3均返回8; p

6、ow等标准函数称为 内建函数。Abs(-10)求绝对值,返回10; round (1.0/2.0)返回1.0,把浮点数四 舍五入为最接近的整数值。 pow(2,3)8 2*38 abs(-10)10 round(1.0/2.0) round(8.06,2)8.06 round(8.06,1)8.11.9 模块 import import math math.floor(8.8) 向下取整8.0 math.ceil(8.8)向上取整9.0 int(math.ceil(32.1)33 int(32.9)32 flo=math.floor flo(33.9)33.0使用了 from 模块 impor

7、t 函数 ,这种方式的 import 命令之后,就可以直接使用函数, 而不需要使用模块名最为前缀了。 但是要注意在不同模块引用,可能导致函数冲突。 from math import sqrt sqrt(9)3.01.9.1 cmath和复数 nan- not a number返回的结果Cmath 即 complex math 复数模块 import cmath cmath.sqrt(-1)1j返回的 1j 是个虚数,虚数以 j 结尾;这里没有使用 from cmath importsqrt,避免与 math的sqrt冲突。1.10.3注释符号:#1.11字符串,使用 ” ”可以进行转义。1.1

8、1.2 拼接字符串 Hello, WorldHello, World Hello, WorldHello,World Hello, +WorldHello, World Hello, +5Traceback (most recent call last):File , line 1, in Hello, +5TypeError: cannot concatenate str and int objects需要保证两边是一样的字符串,而有其他格式要报错的1.11.3 字符串表示str 和 repr- 两个均为函数, 事实上 str 是一种类型Str 会将值转换为合理形式的字符串。另外一种是通过r

9、epr 函数,创建一个字符串。Repr(x)也可以写作x实现(注意:、是反引号),python3.0中已经不适用反引号了 print hello,worldhello,world print repr(hello,world)hello,world print str(hello,world)hello,world print 1000L# 1000L1000L print repr(1000L)1000L print str(1000L)1000 tmp=42 print The number is:+tmpTraceback (most recent call last):File , l

10、ine 1, in print The number is:+tmpTypeError: cannot concatenate str and int objects print The number is:+tmpThe number is:42 print The number is:+str(tmp)The number is:42 print The number is:+repr(tmp)The number is:421.11.4 input 和 raw_input 的比较 name=input(Whats your name:)Whats your name:GumbyTrace

11、back (most recent call last):File , line 1, in name=input(Whats your name:)File , line 1, in NameError: name Gumby is not defined name=input(Whats your name:) Whats your name:Gumby 后面输入的字符串增加了引号不报错。 input(Enter a number:)Enter a number:33 name=input(Whats your name:) Whats your name:Gumby raw_input(

12、Whats your name:) Whats your name:Gumby Gumby raw_input(Whats your name:) Whats your name:GumbyGumby raw_input(Enter a number:)Enter a number:331.11.5 长字符串、原始字符串和 unicode(1)长字符串 使用三引号;转义的反斜杠用于行尾 print hello, world!hello, world! print hello, world!hello, world! 1+2+3+4101+2+3 +410pt工七七1h&lla, woz?ld!

13、 hells f 界口工工 print 1 hellc world!,1 hello, world I(2)原始字符串,对于反斜线并不会特殊对待,以 r开头,注意字 符串尾部 print c:nowhere c:owhere print r c:nowhereSyntaxError: invalid syntax print c:nowhere c:owhere print rc:nowherec:nowhere print rThis is illegal、SyntaxError: EOL while scanning string literal print rThis is illegal、”This is illegal、 print rThis is illegal 、This is illegal 口=ink 1 c: nawhere , c:owhere pzin- ; nowtiexe SyDtaxError: invalid syrtax 二一二二二 1 c: Knottiere Rovriiere print r1 c:nowheie * cinowhererr,This is illegal

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 营销创新

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号