zoukankan      html  css  js  c++  java
  • Python基础(一)

    python是根据格式来判断代码块的

    //经典helloworld,python2.0+版本
    #!/usr/bin/python
    print "Hello,World!"
    
    //python 3.0+版本,print改为内置函数
    #!/usr/bin/python3
    print("Hellow,World!")
    
    //等待用户输入
    a=raw_input("
    
    Press the enter key to exit.")
    
    //在同一行使用多条语句
    import sys; x='bp';sys.stdout.write(x+'
    ')
    
    //不换行输出
    print 5,
    print 9
    输出 5 9
    
    //if结构
    if expression :
       suite 
    elif expression :  
       suite  
    else :  
       suite
    
    //对象的取消,也就是删除一个变量
    var=1
    var1=2
    del var
    del var1
    
    //元组和列表
    tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 )
    list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]
    tuple[2] = 1000    # 元组中是非法应用
    list[2] = 1000     # 列表中是合法应用
    
    //字典用{}标识,字典索引key和它对应的value组成
    dict = {}
    dict['one'] = "This is one"
    dict[2] = "This is two"
    tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
    print dict['one']          # 输出键为'one' 的值
    print dict[2]              # 输出键为 2 的值
    print tinydict             # 输出完整的字典
    print tinydict.keys()      # 输出所有键
    print tinydict.values()    # 输出所有值
    输出为
    This is one
    This is two
    {'dept': 'sales', 'code': 6734, 'name': 'john'}
    ['dept', 'code', 'name']
    ['sales', 6734, 'john']
    
    //数据转换
    int(x [,base])
    将x转换为一个整数
    long(x [,base] )
    将x转换为一个长整数
    float(x)
    将x转换到一个浮点数
    complex(real [,imag])
    创建一个复数
    str(x)
    将对象 x 转换为字符串
    repr(x)
    将对象 x 转换为表达式字符串
    eval(str)
    用来计算在字符串中的有效Python表达式,并返回一个对象
    tuple(s)
    将序列 s 转换为一个元组
    list(s)
    将序列 s 转换为一个列表
    set(s)
    转换为可变集合
    dict(d)
    创建一个字典。d 必须是一个序列 (key,value)元组。
    frozenset(s)
    转换为不可变集合
    chr(x)
    将一个整数转换为一个字符
    unichr(x)
    将一个整数转换为Unicode字符
    ord(x)
    将一个字符转换为它的整数值
    hex(x)
    将一个整数转换为一个十六进制字符串
    oct(x)
    将一个整数转换为一个八进制字符串
    
    a**b a的b次幂
    **是幂方
    
    
  • 相关阅读:
    用C#一次匹配HTML代码中A的链接和文字的方法
    去黑头的7个必胜秘方
    用C#写外挂或辅助工具必须要的WindowsAPI
    用C#把HTML内容转为UBB的方法
    Windows Server 2008 Standard, Enterprise, and Datacenter with Service Pack 2
    xmpp协议分析
    查看Win7的真实版本号方法
    什么是游戏NP?如何在NP下读写游戏内存及如何进入NP进程
    C#正则表达式扫盲 保证 10分钟入门 30分钟精通[绝对不可错过的文章]
    可扩展消息出席协议(XMPP):核心 RFC 3920
  • 原文地址:https://www.cnblogs.com/biaopei/p/7730583.html
Copyright © 2011-2022 走看看