Python学习一

来源:互联网 发布:大学生分期购物软件 编辑:程序博客网 时间:2024/06/14 02:26

学习教材——麻省理工:计算机科学和 Python 编程导论
学堂在线课程网址:http://www.xuetangx.com/courses/course-v1:MITx+6_00_1x+sp/about
IDE:Enthought Canopy

  • 编译型语言
    • 优点:代码运行速度快,因为编译器会完成到低级语言的转换
    • 缺点:找Bug难,因为要进入到编译后的指令中去
  • 解释型语言(eg. Python)

    • 优点:更容易找到Bug
    • 缺点:慢一些,因为一次执行一条转换
  • 数据类型:int、float、bool、str

  • 运算符:“**”(平方)、*、/、+、-、各种不等号

  • 提取字符串中的一部分(Extracting parts of strings)

    • Indexing 索引
      • ’abc’[1] returns ‘b’
      • ‘abc’[-1] returns ‘c’ (0 -2 -1)
    • Slicing 切片
      • ’str’[start:end] starts at start, ends at end-1
      • ‘abc’[1:3] returns ‘bc’
  • raw_input:把获取的输入,当做字符串存储起来

    • name = raw_input(‘Input your name: ‘)
    • 如果需要一个int型,则需要进行类型转化
      x = int( raw_input(‘Input a x: ‘) )
  • 注释用#

  • 条件判断示例:缩进一致代表处于同一循环

    • ’elif’ means else if
x = int(raw_input('Enter an integer:'))if x%2 == 0:    if x%3 == 0         print('')#此行输出一个换行        print('Divisible by 2 and 3')    else:        print('Divisible by 2 and not by 3')elif x%3 == 0:    print('')    print('Divisible by 3 and not by 2')else:    print('Not divisible by 2 or 3')print('Done')
0 0
原创粉丝点击