美文网首页
Python初步

Python初步

作者: imkobedroid | 来源:发表于2019-12-26 14:44 被阅读0次
    # -*- coding: utf-8 -*
    # -*- coding: cp936 -*-
    
    # 检验编码格式
    import sys
    print(sys.stdout.encoding)
    
    # 字符串
    print "hello world"
    print "my name is %s" %"dongshihong"
    name="dongshihong"
    print "my name is %s"%(name)
    print "my name is",name
    print len("hello")
    
    a="AbcDefg"
    print a[2:5]
    print a
    print a.lower()
    
    
    
    
    # 函数
    def print_name(name):
        print "your name is ",name
        return ("%s is good studt" %name)
    
    print print_name("zhangsan")
    print "==================="
    print print_name("lisi")
    
    
    # 常用的数据结构
    
    # java中的数据结构:hashmap array  set
    # python中与java的对比: 
    # hashmap->dict(字典)->{}
    # array->list->[]
    # set->set->set()
    
    
    # dict
    color={"red":0.2,"green":0.4,"blue":0.4}
    print color
    print color["red"]
    
    
    # list
    color_list=["red","green","yellow","green"]
    print color_list
    print color_list[0]
    
    #set (内部不能有相同的元素出现)
    a_set=set()
    a_set.add("111")
    a_set.add("222")
    print a_set
    
    
    # 条件判断语句
    # if
    
    a=1
    
    if a>0: 
        print "a gt 0"
    elif a==0:
        print "a eq 0"
    else:
        print "a lt 0"
    
    
    
    # for
    
    for value in xrange(1,10):
        print value
    
    
    a=0
    for value in xrange(1,10):
        a+=value
    print a
    
    
    缩进的测试
    a=0
    for value in xrange(1,10):
     a= value+a
     print a
    
    
    
    
    
    
    a_list=[]
    a_list.append("111")
    a_list.append("333")
    a_list.append("222")
    a_list.append("444")
    
    for value in a_list:
        print value
    
    
    
    b_dic={}
    b_dic["aaa"]=1
    b_dic["bbb"]=2
    b_dic["ccc"]=3
    b_dic["ddd"]=4
    b_dic["eee"]=5
    
    
    for key ,value in b_dic.items():
        print key,value
    
    
    for key ,value in b_dic.items():
        print key+"====>"+str(value)
    
    c_set=set()
    c_set.add("aaa")
    c_set.add("bbb")
    c_set.add("ccc")
    c_set.add("ddd")
    c_set.add("eee")
    
    for value in c_set:
        print value
    
    
    
    
    
    while
    count =2
    while count>0:
        print "i love python"
        count-=1
    
    
    i=0
    while i<10:
        i+=1
        if i % 2 > 0:
            continue
        print i
    
    
    
    
    # 异常的处理
    
    try:
        a=6
        b=a/0
    except Exception as e:
        print Exception ,":", e
    else:
        pass
    finally:
        pass
    
    
    try:
        print "1111111"
        fh=open('testFile.txt','r')
        print "2222222"
    except IOError as e:
        print "3333333"
    else:
        print "4444444"
        fh.close()
    finally:
        pass
    
    
    # 算法
    
    import math
    
    
    # 求2的3次方
    print math.pow(2,3)
    print math.floor(4.9)
    print round(4.9)
    
    
    #随机数
    import random
    
    items=[1,2,3,4,5,6]
    random.shuffle(items)
    print items
    
    a=random.randint(1,10)
    print a
    
    s_list=random.sample("afdasfafa",2)
    print s_list
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:Python初步

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