PYTHON学习笔记一之基本运算方法

数值

  1. 有两种基本的数值类型:int float
  2. 基本的运算符号:+ - * / ** //(求除数) %(求余)
  3. print函数:print(value1,value2)
         print(2+1,3+4,3/1)
  1. 变量
  2. abs int round函数
  3. 在运算时尽可能多的使用括号

字符串

  1. 字符串定义:由单引号’'或者双引号""包围的一个字符串序列
  2. 字符串的引用:[m:n],find,rfind,[-m:-n],[m]
     s="hello world"
     print(s[0])
     print(s[1:4])
     print(s.find('h'))
     print(s.rfind('0'))
  1. input函数:
    input("please enter your name:")
  1. eval int float strl 函数
    其中eval函数是将一个表达式赋值为整型或浮点型,长与input连用
    a = eval(input("please enter your age:"))

输出

  1. 可选参数sep:可以是空格符号或者其他字符串
    print("hello","world",sep="**")
    print("hello","world",sep="  ")
  1. 可选参数end:由于一个print函数=打印出来的结尾是换行回车,所以在结尾时用end来替换原来的\n
    print("hello",end="  ")
    print("world")
  1. \t 和 \n
  2. format方法
    print("{}".format())

列表 元组

  1. 列表:一串有序的序列,用中括号括起来,元素之间用逗号隔开
    [1,2,3,4,5]
    ["hello","world","!"]
    ["name","age","sex"]

列表也可以进行切片操作:list[1:4]
split join方法

  1. 元组
    t = a, b, c
    s = (a, b, c) 
  1. 嵌套列表:既有元组又有列表
    L = [("name","Bob"),("age",12),("sex","femal"),("score","99")]
    print(L[0],L[0][0],L[-1][-1])

参考资料:《Python程序设计》

相关文章: