美文网首页ITS·黑客
【Python】语法学习3

【Python】语法学习3

作者: 奋斗的小萌狗 | 来源:发表于2017-04-18 12:51 被阅读0次

一、Dict和Set类型

Dict

1.形式:

d={
      'Adam':95,
      'Lisa':85,
      'Bart':59
    }

2.查找(dict本身提供了一个方法,查不到时,返回None。)

>>>print   d.get('Bart')
59
>>>print  d.get('paul')
None

3.Dict 的特点(查找速度快、储存的元素是无序的、元素不可变。)
4.添加元素
d['Pual']=72

Set

1.形式:
s=set(['A','B','C'])
2.索引set

>>>'A'  in  s
True

3.set的特点:set 存储的元素没有顺序。列:

months=set(['Jan','Feb','Mar','Apr','May','Jun','Aug','Sep','Oct','Now','Dec'])
x1='Feb'
x='Sun'
if   x1  in months:
    print  'x1:ok'
else:
    print  'x1:error'
if  x2 in months:
    print  'x2:ok'
else:
    print  'x2:error'

4.增加元素

>>>s=set([1,2,3])
>>>s.add(3)
>>>print  s
set([1,2,3])

5.删除元素

>>>s=set([1,2,3,4])
>>>s.remove(4)
>>>print s
set ([1,2,3])

列:

s=set(['Adam','Lisa','Paul'])
L=['Adam','Lisa','Bart','Paul']
for  x  in L:
    if  x  in s:
        s.remove(x)
    else:
        s.add(x)
print s

函数

1.从Python中调用函数
从Python的官方网站查看文档:(abs)
http://docs.python.org/2/library/functions.html#abs
或者
在交互命令 help(bas)
abs函数是求绝对值

>>>abs(-2)
2

cmp(x,y)比较函数,如果x<y,返回-1,如果x==y,返回0,如果x>y,返回1
int()函数,把其他类型转换为整数

>>>int('123')
123
>>>int(12,34)
12

str()把其他类型转换成str。

>>>str(123)
'123'
>>>str(1.23)
'1.23'

相关文章

  • Python00

    Python学习第一天 python 2 和 python 3的比较 基本语法

  • Python3(14) Python 网络编程

    本系列主要学习Python的基本使用和语法知识,后续可能会围绕着AI学习展开。Python3 (1) Python...

  • Python3 (1) Python语言简介

    本系列主要学习Python的基本使用和语法知识,后续可能会围绕着AI学习展开。Python3 (1) Python...

  • Python3(3) Python 函数

    本系列主要学习Python的基本使用和语法知识,后续可能会围绕着AI学习展开。Python3 (1) Python...

  • Python3(6) Python 模块

    本系列主要学习Python的基本使用和语法知识,后续可能会围绕着AI学习展开。Python3 (1) Python...

  • Python3(12) Python 常用内建模块

    本系列主要学习Python的基本使用和语法知识,后续可能会围绕着AI学习展开。Python3 (1) Python...

  • Python3(11) Python 进程和线程

    本系列主要学习Python的基本使用和语法知识,后续可能会围绕着AI学习展开。Python3 (1) Python...

  • Python3(2) Python语法基础

    本系列主要学习Python的基本使用和语法知识,后续可能会围绕着AI学习展开。Python3 (1) Python...

  • Python3(4) Python 高级特性

    本系列主要学习Python的基本使用和语法知识,后续可能会围绕着AI学习展开。Python3 (1) Python...

  • Python3(7) Python 面向对象编程

    本系列主要学习Python的基本使用和语法知识,后续可能会围绕着AI学习展开。Python3 (1) Python...

网友评论

    本文标题:【Python】语法学习3

    本文链接:https://www.haomeiwen.com/subject/emyfzttx.html