zoukankan      html  css  js  c++  java
  • python入门

    python脚本默认py结尾

    #encoding:utf-8
    # 从#开始到本行结束都是注释
    print "hello world"
    print "number is %d, string is %s" % ( 10, "demo")

    # len(obj)返回对象长度
    # int(obj)对象转换为整形
    # str(obj)对象转换为字符串
    # type(obj)对象的类型(返回type对象)

    #用户输入
    name = raw_input ( 'please enter name:')
    print "you enter is ", name
    name = raw_input ()
    print "you enter is [%s]" % name
    num = raw_input ( 'please num:' )
    print "num * 2 is %d" % (int(num) * 2)
    # + - * /(地板除) //(浮点除法) %(模/取余) **(乘方)
    print ' 5 / 3 is %d' % (5/3)
    print ' 5 mod 3 is %d' % (5%3)

    # 逻辑操作 and or not

    # 比较操作符 < <= > >= == != 值是 True False

    # Python变量大小写敏感(怎么会这样-_-!...)
    # 变量类型在赋值的那一刻确定

    # 字符串
    # python支持成对的单引号或双引号,三引号(三个连续的单/双引号,可以用来标识特殊字符)
    # 使用索引操作符[]或切片操作符[:]来得到子串, 第一个字符的索引为0, 最后一个为-1
    # + 用于连接字符串, * 用来重复字符串
    str = 'Python'
    print str[0]     # 'P'
    print str[2:4]     # th
    print str[:2]     # Py
    print str[1:]     # ython
    print str[-1]     # n
    print 'str1' + ' str2' # str1 str2
    str1 = 'hhh'
    str2 = 'mmm'
    print str1 + str2 # hhhmmm
    print 'm' * 3 # mmm

    # 列表、元组(只读的列表)
    list = [1, 2, 3, 4]
    print list # [1,2,3,4]
    print list[1:3] # [2,3]
    list[1] = 10;
    print list # [1,10,3,4]

    # 字典(hash)
    dic = { "host" : "127.0.0.1", "name":"kk"} #初始化
    dic['port'] = '5432'#添加新值
    print dic
    print dic.keys()
    print dic["host"]
    for key in dic:
        print key, dic[key]
        
    # if 语句
    num = raw_input('if num:')
    if (int(num)) == 1 :
        print 'balabala 1'
        print 'balabala 2'
        print 'balabala 3'
    elif ( 2 == (int(num)) ) :
        print 'balabala 4'
        print 'balabala 5'
        print 'balabala 6'
    else :
        print 'balabala 7'
        print 'balabala 8'
        print 'balabala 9'

    # while 循环
    count = 0
    while count < 10 :
        print 'count is %d' % count
        count += 1;
        
    # for 循环
    for item in ['str1', 'str2', 'str3'] :
        print item, # 加逗号就不会默认换行
    for int in range (10) : # range( [start,]stop[,step])
        print int
    for ch in 'string' :
        print ch,
    print ' '
    str = 'hello!'
    for i in range (len(str)):
        print str[i],

    # 列表解析
    list = [ x for x in range(8) if not x % 2]
    print list

    # 文件和内建函数 open() file
    file = open ('log.log', 'w')
    file.write ('hello')
    file.close()

    # 异常
    try:
        file = open ('log.log', 'r')
        for line in file :
            print line
        file.close()
    except IOError, e:
        print 'file open error"', e

    # 函数
    def func_0 ():
        print 'fun is func_0'
    def func_1 (x):
        print 'fun is func_1'
        x *= 2
        return x
    def func_2 (x, y):
        print 'fun is func_2'
        return x + y
    func_0()
    m = func_1(10)
    print m
    m = func_2(10, 20)
    print m
    # 默认参数
    def func_3 (x, string = 'hello') :
        print string
        return x;
    func_3(10);
    func_3(10, 'mmhhdd')

    # 类
    # 可以提供一个可选的父类或基类,如果没有,就用object作为基类
    class DemoCls(object):
        version = 0.1
    # 所有名字开始和结束有两个下划线的方法都是特殊方法
    # 默认对象创建后第一个执行的函数
        def __init__(self, nm='my name'):
            self.my_name = nm #self相当于this
            print 'created a class instance for:', nm
        def showMeVer(self):
            print self.version
        def showMe(self):
            print self.my_name
        def add (self, x, y) :#self必须存在
            return x + y
    # 使用类的方法
    cls = DemoCls('john')
    cls.showMeVer()
    cls.showMe()
    print cls.add(100, 50)

    # 导入模块
    import module_me
    # 可以通过.来访问模块的属性(函数和变量)
    module_me.func_mod();

    module_me.py

    #encoding:utf-8
    def func_mod() :
        print 'called func of module_me!'

  • 相关阅读:
    BZOJ 3132: 上帝造题的七分钟 树状数组+差分
    PAT Advanced 1006 Sign In and Sign Out (25 分)
    PAT Advanced 1011 World Cup Betting (20 分)
    PAT Basic 1032 挖掘机技术哪家强 (20 分)
    PAT Basic 1028 人口普查 (20 分)
    PAT Basic 1004 成绩排名 (20 分)
    大数据数据库HBase(二)——搭建与JavaAPI
    PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)
    PAT Advanced 1002 A+B for Polynomials (25 分)(隐藏条件,多项式的系数不能为0)
    PAT Basic 1041 考试座位号 (15 分)
  • 原文地址:https://www.cnblogs.com/mmix2009/p/3211013.html
Copyright © 2011-2022 走看看