Python 基础【二】 下

  str()的方法
  字符串练习

  1.str.capitalize
  str.capitalize #返回首字母大写,其他字母小写的字符串
  >>> a = ‘gwdsr‘
  >>> a.capitalize()
  ‘gwdsr‘
  >>>
  2.str.casefold
  str.casefold #字符串转换成小写,用于不区分大小写的字符串比较

  >>> a = ‘gwdsr‘
  >>> b = a.capitalize()
  >>> b
  ‘gwdsr‘
  >>> b.casefold()
  ‘gwdsr‘
  3.str.center
  str.center #返回指定长度的字符串,字符串内容居中,并使用指定字符填充

  >>> b.center(40,‘-‘)
  ‘-----------------gwdsr------------------‘
  4.str.count
  str.count #返回子字符串在字符串中出现的次数
  >>> b.count(‘a‘)
  0
  >>> b.count(‘e‘)
  2
  >>> b
  ‘gwdsr‘
  5.str.encode
  str.encode #对字符串进行编码,返回字节对象
  >>> b.encode(‘gbk‘)
  b‘gwdsr‘
  6.str.endswith
  str.endswith #判断字符串是否以指定的后缀结尾
  >>> b
  ‘gwdsr‘
  >>> b.endswith(‘r‘)
  True
  >>> b.endswith(‘d‘)
  False
  7.str.expandtabs
  str.expandtabs #使用空格替换tab

  >>> str.expandtabs(‘hello\tworld‘)
  ‘hello world‘
  8.str.find
  str.find #返回子字符串在字符串中第一次出现的位置;如没找到,返回-1
  >>> c.find(‘wo‘)
  8
  >>> c
  ‘hello world‘
  9.str.format
  str.format #执行字符串格式化操作,替换字段使用{}分隔,替换字段可以是表
  >>> str.format(‘hello {}‘,‘world‘)
  ‘hello world‘
  10.str.format_map
  str.format_map #执行字符串格式化操作,替换字段使用{}分隔,同str.for
  11.str.index
  str.index #同find(),但如果在字符串中没找到子字符串,会抛出错误
  >>> a = ‘gwdsr‘
  >>> a.index(‘e‘)
  1
  >>> a.index(‘a‘)
  Traceback (most recent call last):
  File "<input>", line 1, in <module>
  ValueError: substring not found
  12.str.isalnum
  12.str.isalnum #判断字符串中是否至少有一个字符,并且所有字符都是字母或数字
  In [202]: a = ‘asf23234d‘

  In [203]: a.isalnum()
  Out[203]: True

  In [204]: b = ‘asf‘

  In [205]: b.isalnum()
  Out[205]: True

  In [206]: c = ‘a*sf32‘

  In [207]: c.isalnum()
  Out[207]: False

  In [208]: d = ‘‘

  In [209]: d.isalnum()
  Out[209]: False
  13.str.isalpha
  13.str.isalpha #判断字符串中是否至少有一个字符,并且所有字符都是字母
  In [194]: a
  Out[194]: ‘123‘

  In [195]: a.isa
  a.isalnum a.isalpha

  In [195]: a.isalpha()
  Out[195]: False

  In [196]: b
  Out[196]: ‘1a‘

  In [197]: b.isa
  b.isalnum b.isalpha

  In [197]: b.isalpha()
  Out[197]: False

  In [198]: c
  Out[198]: ‘‘

  In [199]: c.isalpha()
  Out[199]: False

  In [200]: d = ‘adsf‘

  In [201]: d.isalpha()
  Out[201]: True
  14.str.isdecimal
  14.str.isdecimal #判断字符串中是否至少有一个字符,并且所有字符都是十进制数字
  In [183]: a = ‘123‘

  In [184]: b = ‘1a‘

  In [185]: c = ‘‘

  In [189]: e = ‘1.3‘

  In [190]: a.isdecimal()
  Out[190]: True

  In [191]: b.isdecimal()
  Out[191]: False

  In [192]: c.isdecimal()
  Out[192]: False

  In [193]: e.isdecimal()
  Out[193]: False
  15.str.isdigit
  15.str.isdigit #判断字符串中是否至少有一个字符,并且所有字符都是数字
  In [183]: a = ‘123‘

  In [184]: b = ‘1a‘

  In [185]: c = ‘‘

  In [186]: a.isdigit()
  Out[186]: True

  In [187]: b.isdigit()
  Out[187]: False

  In [188]: c.isdigit()
  Out[188]: False
  16.str.isidentifier
  16.str.isidentifier #判断字符串中是否是有效标识符
  In [172]: a
  Out[172]: ‘gwdsr‘

  In [173]: b
  Out[173]: ‘ASDF‘

  In [174]: a
  Out[174]: ‘gwdsr‘

  In [175]: b = ‘12‘

  In [176]: c = ‘‘

  In [178]: d = ‘*‘

  In [179]: a.isidentifier()
  Out[179]: True

  In [180]: b.isidentifier()
  Out[180]: False

  In [181]: c.isidentifier()
  Out[181]: False

  In [182]: b.isidentifier()
  Out[182]: False
  17.str.islower
  17.str.islower #判断字符串中是否小字并且至少有一个字符
  In [166]: a = ‘gwdsr‘

  In [167]: b = ‘ASDF‘

  In [168]: c = ‘‘

  In [169]: a.islower()
  Out[169]: True

  In [170]: b.islower()
  Out[170]: False

  In [171]: c.islower()
  Out[171]: False
  18.str.isnumeric
  18.str.isnumeric #判断字符串中是否至少有一个字符,并且所有字符都是数字字符
  In [159]: a = ‘124‘

  In [160]: b = ‘as234‘

  In [161]: c = ‘‘

  In [162]: a.isnumeric()
  Out[162]: True

  In [163]: b.isnumeric()
  Out[163]: False

  In [164]: c.isnumeric()
  Out[164]: False
  19.str.isprintable
  19.str.isprintable #判断字符串的所有字符都是可打印字符或字符串为空
  In [151]: a = "\tPuppy"

  In [152]: b = "PUPPY\a"

  In [153]: c = "puppy"

  In [154]: d = "a puppy\b"

  In [155]: a.isprintable()
  Out[155]: False

  In [156]: b.isprintable()
  Out[156]: False

  In [157]: c.isprintable()
  Out[157]: True

  In [158]: d.isprintable()
  Out[158]: False
  20.str.isspace
  20.str.isspace #判断字符串中是否至少有一个字符,并且所有字符都是空白字符
  In [144]: t2
  Out[144]: ‘‘

  In [145]: t2.isspace()
  Out[145]: False

  In [146]: t3 = ‘ ‘

  In [147]: t3.isspace()
  Out[147]: True

  In [149]: t
  Out[149]: ‘gwdsr is a student‘

  In [150]: t.isspace()
  Out[150]: False
  21.str.istitle
  21.str.istitle #判断字符串中是否至少有一个字符,并且所有字符都是titlecase字符
  In [135]: t = ‘gwdsr is a student‘

  In [137]: t1 = t.title()

  In [138]: t.istitle()
  Out[138]: False

  In [139]: t1.istitle()
  Out[139]: True

  In [142]: t2 = ‘‘

  In [143]: t2.istitle()
  Out[143]: False
  22.str.isupper
  22.str.isupper #判断字符串中是否全部是大写字母,空字符串为false
  In [124]: a = ‘gwdsr‘

  In [125]: a.isupper()
  Out[125]: False

  In [126]: a = ‘gwdsR‘

  In [127]: a.isupper()
  Out[127]: True

  In [128]: b = ‘‘

  In [129]: b.isupper()
  Out[129]: False
  23.str.join
  23.str.join #使用字符串作为分隔符串连多个数据为一个字符串
  In [116]: a
  Out[116]: ‘gwdsr‘

  In [117]: e = [‘a‘,‘b‘,‘c‘,‘d‘]

  In [118]: ‘‘.join(e)
  Out[118]: ‘abcd‘

  In [119]: ‘*‘.join(e)
  Out[119]: ‘a*b*c*d‘

  In [120]: ‘*‘.join(a)
  Out[120]: ‘p*e*t*e*r‘

  In [121]: ‘‘.join(a)
  Out[121]: ‘gwdsr‘
  ### 24.str.ljust

  24.str.ljust #返回指定长度的字符串,字符串内容居左,并使用指定字符填充
  In [88]: a.ljust(10,‘-‘)
  Out[88]: ‘gwdsr-----‘

  In [89]: a.ljust(1,‘-‘)
  Out[89]: ‘gwdsr‘

  In [90]: a.ljust(6,‘0‘)
  Out[90]: ‘gwdsr0‘
  ### 25.str.lower

  25.str.lower #字符串转换成小写
  In [81]: a = ‘gwdsr‘

  In [82]: a.upper()
  Out[82]: ‘gwdsR‘

  In [83]: b = a.upper()

  In [84]: b
  Out[84]: ‘gwdsR‘

  In [85]: b.lower()
  Out[85]: ‘gwdsr‘
  26.str.lstrip
  26.str.lstrip #去掉字符串前面的空格,或参数中的字符
  27. str.translate / str.maketrans
  str.translate #根据table表的映射关系,将字符串中的每个字符转换成另一个#返回一个转换表
  In [32]: m = str.maketrans(‘e‘,‘b‘)

  In [33]: a.translate(m)
  Out[33]: ‘pbtbr‘
  28.str.partition
  28.str.partition #返回包含字符串中分隔符之前、分隔符、分隔符之后的子字符串的tuple
  #指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串
  In [77]: g = ‘www.google.com‘
  In [79]: g.partition(‘.‘)
  Out[79]: (‘www‘, ‘.‘, ‘google.com‘)

  In [80]: g.partition(‘g‘)
  Out[80]: (‘www.‘, ‘g‘, ‘oogle.com‘)
  29.str.replace
  29.str.replace #替换字符串中所有的子字符串old为新的字符串new
  >>> a.replace(‘e‘,‘A‘)
  ‘pAtAr‘
  >>>
  30.str.rfind #返回子字符串在字符串中最后一次出现的位置;如没找到,返回-1
  31.str.rindex #同rfind(),但如果在字符串中没找到子字符串,会抛出错误
  32.str.rjust #返回指定长度的字符串,字符串内容居右,并使用指定字符填充
  33.str.rpartition #从后往前查找,返回包含字符串中分隔符之前、分隔符、分隔符之后
  34.str.rsplit #从后往前拆分字符串,返回一个列表
  35.str.rstrip #去掉字符串后面的空格,或参数中的字符
  36.str.split
  36.str.split #拆分字符串,返回一个列表
  In [58]: a = ‘ gwdsr is a student ‘

  In [59]: a.split()
  Out[59]: [‘gwdsr‘, ‘is‘, ‘a‘, ‘student‘]
  37.str.splitlines
  37.str.splitlines #字符串以换行符为分隔符拆分,去掉换行符;如果keepends
  In [54]: a
  Out[54]: ‘gwdsr\n is \na student‘

  In [55]: print(a)
  gwdsr
  is
  a student

  In [56]: print(a.splitlines())
  [‘gwdsr‘, ‘ is ‘, ‘a student‘]
  )
  38.str.startswith
  38.str.startswith #判断字符串是否以指定的前缀开始
  In [47]: a
  Out[47]: ‘ gwdsr is a student ‘

  In [48]: a.startswith(‘ ‘)
  Out[48]: True

  In [49]: a.startswith(‘a‘)
  Out[49]: False
  39.str.strip
  39.str.strip #去掉字符串前后的空格,或指定的所有字符
  In [41]: a = ‘ gwdsr is a student ‘

  In [42]: a
  Out[42]: ‘ gwdsr is a student ‘

  In [43]: a.strip()
  Out[43]: ‘gwdsr is a student‘
  40.str.swapcase
  40.str.swapcase #大写字符转换成小写字符,小写字符转换成大写字符
  In [38]: b = a.title()

  In [39]: b
  Out[39]: ‘gwdsr Is A Student‘

  In [40]: b.swapcase()
  Out[40]: ‘gwdsR iS a sTUDENT‘
  41.str.title
  41.str.title #每个单词的第一个字符转换成titlecase字符,其他字符转
  In [34]: a = ‘gwdsr is a student‘

  In [35]: a.title()
  Out[35]: ‘gwdsr Is A Student‘
  43.str.upper
  43.str.upper #字符串转换成大写
  In [26]: a.upper()
  Out[26]: ‘gwdsR‘
  44.str.zfill
  44.str.zfill #在字符串的左边填充0,不会截断字符串
  In [24]: a.zfill(10)
  Out[24]: ‘00000gwdsr‘

  In [25]: a.zfill(5)
  Out[25]: ‘gwdsr‘
  builtins Python内置函数
  1.abs #求绝对值
  2.all #判断迭代器中的所有数据是否都为true
  3.any #判断迭代器中的是否有一个数据为true
  4.bin #转换整数为一个二进制字符串
  5.bool #转换一个数据为布尔值
  6.bytearray #将数据转换为字节数组
  7.bytes #将数据转换为字节数组
  8.callable #判断一个对象是否可调用
  9.chr #将整数转成字符
  10.classmethod #得到function的classmethod
  11.compile #编译source为code或AST对象
  12.complex #创建一个复数
  13.delattr #删除指定的属性
  14.dict #创建一个字典dictionary
  15.dir #返回对象的属性列表
  16.divmod #得到两个数字相除的结果和余数
  17.enumerate #得到一个枚举对象
  18.eval #执行一个表达式
  19.exec #动态执行Python代码
  20.filter #过滤数据得到一个迭代器
  21.float #将字符串或数字转为浮点数
  22.format #格式化数据
  23.frozenset #得到新的frozenset对象
  24.getattr #得到对象属性的值
  25.globals #得到当前模块的全局符号表的字典
  26.hasattr #判断对象是否存在属性
  27.hash #得到对象的哈希值
  28.help #显示帮助信息
  29.hex #整数转换为十六进制表示
  30.id #得到对象的id
  31.input #输出提示符,读取用户输入
  32.int #将数字或字符串转为整数
  33.isinstance #判断object是否是classinfo的实例
  34.issubclass #判断一个类是否是另一个类的父类
  35.iter #得到一个迭代器
  36.len #返回对象的长度或集合的数据个数
  37.list #创建一个列表
  38.locals #得到当前符号表字典
  39.map #更改迭代器中的每个数据得到一个新的迭代器
  40.max #得到迭代器中最大的或两个或多个参数中最大的
  41.min #得到迭代器中最小的或两个或多个参数中最小的
  42.next #得到迭代器的下一个数据
  43.object #得到object的实例
  44.oct #整数转换为八进制表示
  45.open #打开文件并返回一个流
  46.ord #得到字符的整数表示
  47.pow #乘方运算
  48.print #输出数据到流
  49.property #得到属性
  50.range #创建一个范围对象
  51.repr #得到对象的字符串表示
  52.reversed #反转序列得到一个迭代器
  53.round #浮点数按小数位数做舍入操作
  54.set #创建一个集合对象
  55.setattr #更改属性的值
  56.slice #得到分片对象
  57.sorted #排序可迭代的数据得到一个列表
  58.staticmethod #得到function的staticmethod
  59.str #得到对象的str版本
  60.sum #计算可迭代数据的合计
  61.tuple #创建一个元组
  62.type #返回对象的类型或创建一个新的类型对象
  63.vars #得到属性信息

  dict() 方法练习
  1.dict.clear
  dict.clear #删除dictionary中的所有key-value对

  >>> a = {‘k1‘:‘v1‘}
  >>> a
  {‘k1‘: ‘v1‘}
  >>> a.clear()
  2.dict.copy
  dict.copy #浅拷贝dictionary

  >>> b = a.copy()
  >>> b
  {‘k1‘: ‘v1‘}
  3.dict.fromkeys
  dict.fromkeys #返回一个新的dictionary,key由iterable的元素组成,value等于value

  >>> a = dict.fromkeys([‘k1‘,‘k2‘,‘k3‘],‘vvv‘)
  >>> a
  {‘k3‘: ‘vvv‘, ‘k2‘: ‘vvv‘, ‘k1‘: ‘vvv‘}
  4.dict.get
  dict.get #返回dictionary中key为指定值k对应的value,

  >>> a.get(‘k3‘)
  ‘vvv‘

  >>> b = {‘k1‘:a}
  >>> b
  {‘k1‘: {‘k3‘: ‘vvv‘, ‘k2‘: ‘vvv‘, ‘k1‘: ‘vvv‘}}
  >>> b.get(‘k1‘)[‘k3‘]
  ‘vvv‘
  >>>
  5.dict.items
  dict.items #返回dictionary所有key-value对组成的集合

  >>> b.items()
  [(‘k1‘, {‘k3‘: ‘vvv‘, ‘k2‘: ‘vvv‘, ‘k1‘: ‘vvv‘})]
  6.dict.keys
  dict.keys #返回dictionary所有key组成的集合

  >>> b.keys()
  [‘k1‘]
  7.dict.pop
  dict.pop #从dictionary中删除指定key,返回指定key对应的value。如果dictionary中不存在指定key,如果指定了d,返回d,否则抛出例外

  >>> c = b.pop(‘k1‘)
  >>> c
  {‘k3‘: ‘vvv‘, ‘k2‘: ‘vvv‘, ‘k1‘: ‘vvv‘}
  >>> b
  {}
  >>>
  8.dict.popitem
  dict.popitem #删除并返回key-value对(key, value)作为2-tuple,如果dictionary为空,抛出例外

  >>> e = c.popitem()
  >>> e
  (‘k3‘, ‘vvv‘)
  >>> c
  {‘k2‘: ‘vvv‘, ‘k1‘: ‘vvv‘}
  >>>
  9.dict.setdefau
  dict.setdefault #如果dictionary中不存在k,设置D[k]=d

  >>> c
  {‘k2‘: ‘vvv‘, ‘k1‘: ‘vvv‘}
  >>> c.setdefault(‘k1‘,‘v1‘)
  ‘vvv‘
  >>> c
  {‘k2‘: ‘vvv‘, ‘k1‘: ‘vvv‘}
  >>> c.setdefault(‘k4‘,‘v4‘)
  ‘v4‘
  >>> c
  {‘k2‘: ‘vvv‘, ‘k1‘: ‘vvv‘, ‘k4‘: ‘v4‘}
  >>>
  10.dict.update
  dict.update #使用E(dict/iterable)和F的数据更新dicti

  >>> a = {‘ak1‘:‘av1‘}
  >>> b = {‘bk1‘:‘bv1‘}
  >>> b.update(a)
  >>> b
  {‘ak1‘: ‘av1‘, ‘bk1‘: ‘bv1‘}
  11.dict.values
  dict.values #返回dictionary所有value组成的集合
  >>> b.values()
  [‘av1‘, ‘bv1‘]
  >>>
  list()
  列表练习

  1.list.append
  list.append #附加一个对象到list

  >>> a = [‘a‘,‘b‘,1,3,4]
  >>> a
  [‘a‘, ‘b‘, 1, 3, 4]
  >>> a.append(‘d‘)
  >>> a
  [‘a‘, ‘b‘, 1, 3, 4, ‘d‘]
  2.list.clear
  list.clear #删除list中的所有元素

  >>> a.clear()
  >>> a
  []
  3.list.copy
  list.copy #浅拷贝list

  >>> a = [‘a‘,‘b‘,1,3,4]
  >>> a
  [‘a‘, ‘b‘, 1, 3, 4]
  >>> b = a.copy()
  >>> b
  [‘a‘, ‘b‘, 1, 3, 4]
  4.list.count
  list.count #返回指定参数在list中出现的次数

  >>> a.count(‘b‘)
  1
  5.list.extend
  list.extend #附加指定iterable中的元素到list

  >>> b = [6,6,5,4,2,4]
  >>> a.extend(b)
  >>> a
  [‘a‘, ‘b‘, 1, 3, 4, ‘d‘, 6, 6, 5, 4, 2, 4]
  >>>
  6.list.index
  list.index #返回指定值在list中第一次出现的位置,如果list上存在指

  >>> a
  [‘a‘, ‘b‘, 1, 3, 4, ‘d‘, 6, 6, 5, 4, 2, 4]
  >>> a.index(1)
  2
  7.list.insert
  list.insert #在list的指定位置index插入object

  >>> a.insert(2,‘sddsds‘)
  >>> a
  [‘a‘, ‘b‘, ‘sddsds‘, 1, 3, 4, ‘d‘, 6, 6, 5, 4, 2, 4]
  8.list.pop
  list.pop #删除并返回指定位置index的元素,默认为最后位置,如果index超过范围或list为空,抛出错误

  >>> a
  [‘a‘, ‘b‘, ‘sddsds‘, 1, 3, 4, ‘d‘, 6, 6, 5, 4, 2, 4]
  >>> b = a.pop()
  >>> b
  4
  >>> a
  [‘a‘, ‘b‘, ‘sddsds‘, 1, 3, 4, ‘d‘, 6, 6, 5, 4, 2]
  >>>
  9.list.remove
  list.remove #从list中删除第一次出现的指定值,如果指定值不存在,抛出例外

  >>> a
  [‘a‘, ‘b‘, ‘sddsds‘, 1, 3, 4, ‘d‘, 6, 6, 5, 4, 2]
  >>> a.remove(‘a‘)
  >>> a
  [‘b‘, ‘sddsds‘, 1, 3, 4, ‘d‘, 6, 6, 5, 4, 2]
  10.list.reverse
  list.reverse #反转list中的元素

  >>> a
  [‘b‘, ‘sddsds‘, 1, 3, 4, ‘d‘, 6, 6, 5, 4, 2]
  >>> a.reverse()
  >>> a
  [2, 4, 5, 6, 6, ‘d‘, 4, 3, 1, ‘sddsds‘, ‘b‘]
  11.list.sort
  list.sort #对list进行排序

  >>> a
  [2, 4, 5, 6, 6, ‘d‘, 4, 3, 1, ‘sddsds‘, ‘b‘]
  >>> a.sort()
  >>> a
  [1, 2, 3, 4, 4, 5, 6, 6, ‘b‘, ‘d‘, ‘sddsds‘]
  >>>
  tuple()
  元组练习

  1.tuple.count
  tuple.count #返回指定参数在tuple中出现的次数

  >>> b
  (1, 2, 3, 4, 4, 5, 6, 6, ‘b‘, ‘d‘, ‘sddsds‘)
  >>> b.count(4)
  2
  2.tuple.index
  tuple.index #返回指定值在tuple中第一次出现的位置,如果tuple中不

  >>> b
  (1, 2, 3, 4, 4, 5, 6, 6, ‘b‘, ‘d‘, ‘sddsds‘)
  >>> b.count(4)
  2
  >>> b.index(2)
  1
  >>> b.index(1)
  0
  >>>

时间: 2024-08-04 08:30:30

Python 基础【二】 下的相关文章

Python基础二:pycharm的安装及简单使用,while循环,格式化输出,运算符, 编码的初识,git

Python基础二: pycharm的安装及简单使用,while循环,格式化输出,运算符, 编码的初识,git 1.pycharm的安装及简单使用 Tab / Shift + Tab 缩进.不缩进当前行 (先选中代码) Shift + 滚轮 放大缩小代码 Ctrl + ? 注释.解注(先选中代码) Ctrl + d 复制上一行代码 Ctrl + z 撤销 Ctrl + Shift + F10 RUN Ctrl + 左键---->点击 int,str 等可查看源码 2.while 循环(重点) w

Python基础二--基本控制语句

基本接触每一种语言,都需要做的:1.print 一个"Hello world!" 2.了解基本的数据类型 3.学习控制语句. 当我们学习控制语句,一般都离不开if,for ,while,switch(case).本文就做一个简单的介绍python的基本控制语句,其中我们用if while来做一个经典的"猜数字游戏",if for来做一个"输出完美数". 在此之前,对于一些没用过python的同学而熟悉c/c++等用{}来做块的要注意了,pytho

进击的Python【第二章】:Python基础(二)

Python基础(二) 本章内容 数据类型 数据运算 列表与元组的基本操作 字典的基本操作 字符编码与转码 模块初探 练习:购物车程序 一.数据类型 Python有五个标准的数据类型: Numbers(数字) String(字符串) List(列表) Tuple(元组) Dictionary(字典) 1. Number(数字) number类型用来专门存储数字数据,他们是不可改变的数据类型,这意味着改变数字数据类型会分配一个新的对象 Python支持四种不同的数字类型: int(有符号整型) l

python——基础二

函数的理解 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 函数作用是你的程序有良好的扩展性.复用性. 同样的功能要是用3次以上的话就建议使用函数. 标注:不能死记, 函数可以理解为一个一个的功能块,你把一个大的功能拆分成一块一块的,用某项功能的时候就去调用某个功能块即可! 函数可以理解为:乐高积木,给你一块一块的,你可以用这些积木块组成你想要的任何,功能! 函数可以调用函数!主函数的作用就是把函数进行串联.调用!函数本身是不能自己执

python基础二(基础数据类型)

一,引子. 1 什么是数据? x=10,10是我们要存储的数据 2 为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3 数据类型 数字 字符串 列表 元组 字典 集合 二基础数据类型. 2.1数字int. 数字主要是用于计算用的,使用方法并不是很多,就记住一种就可以: #bit_length() 当十进制用二进制表示时,最少使用的位数 v = 11 data = v.bit_length() print(data) 2.2布尔值bool. 布尔值就两种:T

python(十二)下:ORM框架SQLAlchemy使用学习

此出处:http://blog.csdn.net/fgf00/article/details/52949973 本节内容 ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 多对多关系 表结构设计作业  一.ORM介绍 如果写程序用pymysql和程序交互,那是不是要写原生sql语句.如果进行复杂的查询,那sql语句就要进行一点一点拼接,而且不太有重用性,扩展不方便.而且写的sql语句可能不高效,导致程序运行也变慢. 为了避免把sql语句写死在代码里,有没有一种方法直

【python 基础二】

1.变量 必须由数字.字母.下划线任意组合,且不能数字开头 不能是python关键字: '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', '

开班前自学—python基础二_基础数据(2019.02.27)

一 基础数据类型 list: [],各种数据类型的数据,大量的数据,便于操作. tuple: 元组.()只读列表. dic: 存储大量的数据,关系型数据. 二 int str bool相互转换 1 int <---> str int --->str int(str) 条件:str必须全部由数字组成. str --->int str(int) age = int (input('how old are you?')) print(age) # 如果输入内容包括非数字,会报错 s1 =

python基础二(if、while、for)

一.if比较运算符 运算符 描述 示例 == 检查两个操作数的值是否相等,如果是则为真,相反为假 如(1 == 1)为True ! = 检查两个操作数的值是否相等,如果不等则为真,等于为假 如(1 != 2)为True > 检查左边的值是否大于右边,如果是则为真,相反为假 如(2 > 1)为True < 检查左边的值是否小于右边,如果是则为真,相反为假 如(5 < 2)为False >= 检查左边的值是否大于等于右边的值,如果是则为真,相反为假 如(2 >= 2)为Tr

python基础二

一.字典类型 *)字典是python中唯一的映射类型 ,key-value(哈希表),字典对象是可变的,但key必须用不可变对象. *)字典的创建和字典值得访问 ##字典的内容在查看时不能通过索引来进行查看 *)内建方法:fromkeys 字典中的key有相同的value值,默认为None *)字典的循环遍历访问 *)字典中key-value的添加 dic[key] = value ##通过字典的添加发现,字典是无序的数据类型 *)字典的删除 **)根据key值删除字典的元素 **)随机删除字典