ct38 发表于 2018-8-14 08:47:55

第1章python基础

  一、变量
  1.变量定义规则
  1.1.变量名只能是 字母、数字或下划线的任意组合
  1.2 变量名的第一个字符不能是数字
  1.3 以下关键字不能声明为变量名['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is','lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
  2.定义方式
  驼峰体
  NumberOfStudents = 80
  下划线
  age_of_oldboy = 56
  number_of_students = 80
  二、基本数据类型:
  1.字符串:
  定义: ', ", ''', """引起来的一堆字符
  'hi' ,"hello world"
  基本运算: +/*
  常用方法:
  'count',            =>子字符串出现的次数
  'index', 'rindex',=> 子字符串第一次出现的索引位置,如果不存在报错
  'find', 'rfind',    => 子字符串第一次出现的索引位置,如果不存在返回-1
  'endswith','startswith', 以某个字符串开始或者以某个字符串结束时,返回True,否则返回False
  'format'    字符串格式化
  'join',
  'split', 'rsplit',
  'lower', 'upper'
  'strip, 'lstrip', 'rstrip',
  'replace',
  'splitlines',
  'isalpha', 'isdigit', 'isalnum',, 'islower', 'isupper',
  2.布尔运算
  a and b | a(True) | a(False)
  ---------------------------------
  b(True) |True   |False
  ---------------------------------
  b(False)|False|False
  not a | a(True)| a(False)
  --------------------------
  | False| True
  3.列表:
  定义: []包含, 每个值之间用逗号分隔,每个值可以是任意类型
  编号: 0-N 索引
  -6   -5   -4   -3   -2-1
  0   1   2   3   4   5   6
  ages =
  访问:
  通过索引去访问list中的值
  ages
  ages
  修改:
  ages = value
  增加:
  pass
  删除:
  del ages
  1. 使用的索引必须在list索引范围内
  2. list_name
  list_num = value
  del list_num
  个数: len()
  判断是否在list中 in
  => list(seq)
  range(start, end, step)
  start 开始 每次+step, 直到end(不包含end)
  start, start + step, start +2step = end
  start + n * step
  start =1, end = 10, step = 2
  start = 1
  while True:
  print start
  start += step
  if start >= end:
  break
  range(start, end, step)
  range(end), start = 0, step=1
  range(start, end), step = 1
  四则运算
  +    +
  *    'abc'*5
  切片
  从list中获取其中一部分元素做为一个新的list
  nums = range(10)
  nums =
  nums
  , nums, ..., nums]
  nums
  nums
  nums =>step=1
  nums => end=>len(N)
  nums[:]
  nums[:end]
  nums[:end:step]
  nums[::step]
  nums[:]
  nums = range(10)
  nums2 = nums
  nums3 = nums[:]
  地址, 值
  基本的数据类型 => 值类型
  a = 1
  b = 1
  c = b
  a = []
  a = b
  b发生变化 del, update, 会影响a, 地址传递
  nums = range(10)
  nums = []
  list的方法
  1. 更新list中的元素, 无返回值
  2不更新list中元素,但是有返回值
  3. 更新list中元素,有返回值
  a = 1
  b = 2
  temp = a
  a = b
  b = temp
  1. while
  2. raw_input
  3. if
  if ... else ...
  if ... elif ...
  if ... elif ... else ...
  4. append
  5. len() == 0
  6. pop(0)
  4.元组:
  用()包含起来, 用逗号分隔没一个元素, 只有一个元素时,最后的逗号不能省略
  (1, )
  元组是不能修改的序列(指向不能变,值不能变)
  地址, 值
  mylist = []
  mytuple = (1, 2, 3, mylist, 4)
  tuple()
  三、流程控制
  if 逻辑表达式0:
  子语句0
  elif 逻辑表达式1:
  子语句1
  elif 逻辑表达式2:
  子语句2
  ....
  else:
  子语句N
  if 逻辑表达式0:
  子语句0
  elif 逻辑表达式1:
  子语句1
  elif 逻辑表达式2:
  子语句2
  ....
  while 逻辑表达式:
  子语句
  else:
  子语句
  for var in seq:
  子语句
  range(N) =>
  for name in ['kk', 'woniu']:
  print name
  for i in range(10):
  if i == 5:
  break/continue
  print i
页: [1]
查看完整版本: 第1章python基础