• 变量


    import string
    s='abc'
    #去除空格
    s = ' asd f f s '
    print(s.strip())
    print(s.lstrip())
    print(s.rstrip())
    print(s)

    #字符串连接
    s1 = 'abc'
    s2 = 'dfe'
    print(s1+ " " + "kkk" + s2)

    #大小写
    s = 'cdv HWk KKK'
    print(s.upper())
    print(s.upper().lower())
    print(s.capitalize())

    #位置比较
    s1 = 'abcdefgh'
    s2 = 'abcdeeeeee'
    print(s1.index('de'))
    print(s2.index('a'))
    #python3移除了cmp函数
    print(s1==s2)
    print(s1>s2)
    print(s1<s2)

    print(len('abc'))
    print(' ')

    s = ''
    if s is None:
    print('None')

    if not s:
    print('Empty')

    ================== RESTART: D:/ruanjianml/Python35/test.py ==================
    asd f f s
    asd f f s
    asd f f s
    asd f f s
    abc
    kkkdfe
    CDV HWK KKK
    cdv hwk kkk
    Cdv hwk kkk
    3
    0
    False
    True
    False
    3

    Empty

    >>> print(type(123))
    <class 'int'>
    >>> print(type(123.))
    <class 'float'>
    >>> print(type(1.2345))
    <class 'float'>

    >>> print(type('123'))
    <class 'str'>
    >>> print(type([1,2,3,'a','b']))
    <class 'list'>

    >>> print(type((1,2,3,'abc')))
    <class 'tuple'>
    >>> print(type(set(['a',2,'c'])))
    <class 'set'>
    >>> print(type({'a':1,'b':2}))
    <class 'dict'>
    >>>
    >>> def func(a,b,c):
    print(a,b,c)


    >>> print(type(func))
    <class 'function'>

    >>> a=func
    >>> print(type(a))
    <class 'function'>
    >>>
    >>> import string
    >>> print(type(type(string)))
    <class 'type'>
    >>>
    >>> print(type(string))
    <class 'module'>
    >>>
    >>> class CStudent(object):
    pass

    >>> print(type(CStudent))
    <class 'type'>
    >>>
    >>> test=CStudent()
    >>> print(type(test))
    <class '__main__.CStudent'>

    >>> try:
    print(x)
    except NameError:
    print("error")


    error

    import string
    #字符串的分割和连接
    s1 = 'ab,c,d,f'
    s2 = 'a,,bc,,b'
    splitted1 = s1.split(',')
    print(type(splitted1))
    print(splitted1)
    print(s2.split(',,'))

    <class 'list'>
    ['ab', 'c', 'd', 'f']
    ['a', 'bc', 'b']

    >>> s = """ac
    vb
    fwsv
    s"""
    >>> print(s.split(' '))
    ['ac', 'vb', 'fwsv', 's']
    >>> print(s.splitlines())
    ['ac', 'vb', 'fwsv', 's']
    >>>
    >>> s = ['abc', 'def', 'fg']
    >>> print(''.join(s))
    abcdeffg
    >>> print('-'.join(s))
    abc-def-fg
    >>> print(' '.join(s))
    abc
    def
    fg

    >>> s='abcdef'

    >>> print('abcd1234'.isalnum())
    True
    >>> print(' 1234sad'.isalnum())
    False
    >>> print('asdf'.isalpha())
    True
    >>> print('1234'.isalnum())
    True
    >>> print('1234'.isdigit())
    True
    >>> print(' '.isspace())
    True
    >>> print(''.isspace())
    False
    >>> print('ads12'.islower())
    True
    >>> print('A12'.isupper())
    True
    >>> print('As'.istitle())
    True

    >>> print(str(5.))
    5.0
    >>> print(str(4))
    4
    >>> print(str(-1.2324))
    -1.2324
    >>> print(int('123'))
    123
    >>> print(int(123.34))
    123
    >>> print(int('123.24'))
    Traceback (most recent call last):
    File "<pyshell#42>", line 1, in <module>
    print(int('123.24'))
    ValueError: invalid literal for int() with base 10: '123.24'
    >>> print(int('1010',2))
    10
    >>> print(int('ffff',16))
    65535

    >>> s='asdfg'
    >>> l = list(s)
    >>> print(l)
    ['a', 's', 'd', 'f', 'g']

    import string
    #if
    a=100
    b=200
    c=300

    if c==a:
    print(a)
    elif c==b:
    print(b)
    else:
    print(c)

    x=None
    if x is None:
    print('Test1,None')
    else:
    print('not None')

    if x==None:
    print('Test2,None')

    if not x:
    print('Test3,not None')

    for i in range(0,30,5):
    print(i)

    0
    5
    10
    15
    20
    25

    i = 0;
    s =0;
    while i<=100:
    s += i;
    i +=1;
    print(s)

    for i in range(0,100):
    if i<10:
    pass
    if i<30:
    continue;
    if i<35:
    print(i)
    else:
    break;

    def func(arg1,arg2):
    print(arg1,arg2)
    return arg1,arg2

    r = func(1,2)
    print(type(r))
    print (r[0],r[1])

    def func(x,y=100):
    return x+y

    print(func(100,200))
    print(func(100))

    def func(x,y=100):
    print('x=',x)
    print('y=',y)
    return x+y

    print(func(y=100,x=200))
    print(func(x=100))

    #可变参数
    def func(name,*numbers):
    print(type(numbers))
    print(numbers)
    return 'Done'
    func('Tom',1,2,3,4,'add')

  • 相关阅读:
    HBase
    linux配置环境变量
    ubuntu17.04安装flash
    WebService服务及客户端 编程
    eclipse
    设计模式:简单工厂
    设计模式:工厂方法
    C#加载dll 创建类对象
    C#线程
    Opencv 3入门(毛星云)摘要
  • 原文地址:https://www.cnblogs.com/crazybird123/p/7078621.html
Copyright © 2020-2023  润新知