基础语法PPT学习教案_第1页
基础语法PPT学习教案_第2页
基础语法PPT学习教案_第3页
基础语法PPT学习教案_第4页
基础语法PPT学习教案_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、会计学1基础语法基础语法第1页/共22页第2页/共22页运算符运算符名称名称说明说明例子例子+加两个对象相加3 + 5得到8。a + b得到ab。-减得到负数或是一个数减去另一个数-5.2得到一个负数。50 - 24得到26。*乘两个数相乘或是返回一个被重复若干次的字符串2 * 3得到6。la * 3得到lalala。*幂返回x的y次幂3 * 4得到81(即3 * 3 * 3 * 3)/除x除以y4/3得到1(整数的除法得到整数结果)。4.0/3或4/3.0得到1.3333333333333333/取整除返回商的整数部分4 / 3.0得到1.0%取模返回除法的余数8%3得到2。-25.5%2.

2、25得到1.5第3页/共22页运算符运算符名称名称说明说明例子例子小于返回x是否小于y。所有比较运算符返回1表示真,返回0表示假。这分别与特殊的变量True和False等价。注意,这些变量名的大写。5 3返回0(即False)而3 5返回1(即True)。比较可以被任意连接:3 5 大于返回x是否大于y5 3返回True。如果两个操作数都是数字,它们首先被转换为一个共同的类型。否则,它总是返回False。=小于等于返回x是否小于等于yx = 3; y = 6; x =大于等于返回x是否大于等于yx = 4; y = 3; x = y返回True。=等于比较对象是否相等x = 2; y = 2;

3、 x = y返回True。x = str; y = stR; x = y返回False。x = str; y = str; x = y返回True。!=不等于比较两个对象是否不相等x = 2; y = 3; x != y返回True。第4页/共22页运算符运算符名称名称说明说明例子例子左移把一个数的比特向左移一定数目(每个数在内存中都表示为比特或二进制数字,即0和1)2 右移把一个数的比特向右移一定数目11 1得到5。11按比特表示为1011,向右移动1比特后得到101,即十进制的5。&按位与数的按位与5 & 3得到1。|按位或数的按位或5 | 3得到7。按位异或数的按位异或5 3得到6按位翻

4、转x的按位翻转是-(x+1)5得到-6。第5页/共22页运算符运算符名称名称说明说明例子例子not布尔“非”如果x为True,返回False。如果x为False,它返回True。x = True; not y返回False。and布尔“与”如果x为False,x and y返回False,否则它返回y的计算值。x = False; y = True; x and y,由于x是False,返回False。在这里,Python不会计算y,因为它知道这个表达式的值肯定是False(因为x是False)。这个现象称为短路计算。or布尔“或”如果x是True,它返回True,否则它返回y的计算值。x =

5、 True; y = False; x or y返回True。短路计算在这里也适用。第6页/共22页如果要强制取得浮如果要强制取得浮点结果,则必须确点结果,则必须确保操作数中至少有保操作数中至少有一个为浮点类型一个为浮点类型 ,如如 1/3 示例中所示,示例中所示,其计算结果为整数其计算结果为整数 例:$ python 1 / 30 1.0 / 30.33333333333333331 1.0 / 30.0 1 % 31 1.0 % 31.0 第7页/共22页例: $ python c = 3.0 + 1.2j print c(3+1.2j) print c.real, c.imag3.0 1

6、.2 第8页/共22页例: $ python a = 3.0 print bool(a)True b=0 print bool(b)False c=True print c True第9页/共22页例: $ python print 127 # Using decimal literal127 print 0177 # Using octal literal127 print 0 x7F # Using hexadecimal literal127 第10页/共22页第11页/共22页例: $ python print hello world! hello world print whats

7、your name whats your name print whats your name whats your name print This is the first line. This is the second line. Whats your name? This is the first line This is the second line Whats your name? print This is the first. This is the second. This is the first, This is the second第12页/共22页第13页/共22页

8、例: $ python sr = Discover Python! sr.upper()DISCOVER PYTHON! sr.lower()discover python! sr = This is a test! sr.split()This, is, a, test! sr = 0:1:2:3:4:5:6:7:8:9 sr.split(:)0, 1, 2, 3, 4, 5, 6, 7, 8, 9 sr=: tp = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) sr.join(tp)0:1:2:3:4:5:6:7:8:9 第14页/共22页n#!/usr/bin/pyth

9、on# Filename : helloworld.pyprint Hello Worldn$python helloworld.py Hello Worldn$chmod +x helloworld.py$./ helloworld.pyHello World第15页/共22页第16页/共22页 #!/usr/bin/python# Filename: if.py number = 23guess = int(raw_input(Enter an integer : )if guess = number: print Congratulations, you guessed it. elif

10、 guess number: print No, it is a little higher than that else: print No, it is a little lower than that 第17页/共22页#!/usr/bin/python# Filename: while.pynumber = 23running = Truewhile running: guess = int(raw_input(Enter an integer : ) if guess = number: print Congratulations, you guessed it. running =

11、 False # this causes the while loop to stop elif guess number: print No, it is a little higher than that else: print No, it is a little lower than that else: print The while loop is over. 第18页/共22页 #!/usr/bin/python# Filename: for.pyfor i in range(1, 5): print ielse: print The for loop is over 第19页/共22页#!/usr/bin/python# Filename: break.pywhile True: s = raw_input(Enter something : ) if s = quit: break print Length of the string is, len(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论