20190817 Python 练习记录

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> def f(x):
    return x*x

>>> r =map(f,list(range(1,11)))
>>> r
<map object at 0x0000000002BA3B38>
>>> list(r)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> from functools import reduce
>>> def add(x,y):
    return x+y

>>> reduce (add,list(range(1,11)))
55
>>> reduce (add,list(range(1,100)))
4950
>>> reduce (add,list(range(1,101)))
5050
>>> sum(list(range(1,101)))
5050
>>> def fn(x,y):
    return x*10+y

>>> reduce(fn,[5,7,8,5,6,8,9])
5785689
>>> def is_odd(n):
    return n%2==1

>>> list(filter(is_odd,list(range(1,100))))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
>>> sorted([36,7,-12,9,-76])
[-76, -12, 7, 9, 36]
>>> sorted([36,7,-12,9,-76],key=abs)
[7, 9, -12, 36, -76]
>>> key
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    key
NameError: name 'key' is not defined
>>> list(key)
Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    list(key)
NameError: name 'key' is not defined
>>> sorted(key)
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    sorted(key)
NameError: name 'key' is not defined
>>> def lazy_sum(*args):
    def sum():
        ax =0
        for n in args:
            ax = ax=n
        return ax
    return sum

>>> f=lazy_sum(1,2,3,4,5,6)
>>> f
<function lazy_sum.<locals>.sum at 0x0000000002BDF6A8>
>>> f()
6
>>> def count():
    fs=[]
    for i in range(1,4):
        def f():
            return i*i
        fs.append(f)
    return fs

>>> f1,f2,f3=count()
>>> f1()
9
>>> f2()
9
>>> f3()
9
>>> def count():
    def f(j):
        def  g():
            return j*j
        return g
    fs=[]
    for i in range(1,4)
    
SyntaxError: invalid syntax
>>> def count():
    def f(j):
        def  g():
            return j*j
        return g
    fs=[]
    for i in range(1,4):
        fs.append(f(i))
    return fs

>>> f1,f2,f3=count()
>>> f1()
1
>>> f2()
4
>>> f3()
9
>>> def lazy_sum(*args):
    def sum():
        ax = 0
        for n in args:
            ax = ax + n
        return sum
    return sum

>>> f=lazy([1,1,1,1,1])
Traceback (most recent call last):
  File "<pyshell#67>", line 1, in <module>
    f=lazy([1,1,1,1,1])
NameError: name 'lazy' is not defined
>>> def lazy_sum(*args):
    def sum():
        ax = 0
        for n in args:
            ax = ax + n
    return sum

>>> def lazy_sum(*args):
    def sum():
        ax = 0
        for n in args:
            ax = ax + n
        return sum
    return sum

>>> f=lazy_sum([1,2,1,1,1])
>>> f
<function lazy_sum.<locals>.sum at 0x0000000002BDF840>
>>> f()
Traceback (most recent call last):
  File "<pyshell#74>", line 1, in <module>
    f()
  File "<pyshell#71>", line 5, in sum
    ax = ax + n
TypeError: unsupported operand type(s) for +: 'int' and 'list'
>>> def lazy_sum(*args):
    def sum():
        ax = 0
        for n in args:
            ax = ax + n      
    return sum

>>> f=lazy_sum([1,1,1,1,1])
>>> f
<function lazy_sum.<locals>.sum at 0x0000000002BDF8C8>
>>> f()
Traceback (most recent call last):
  File "<pyshell#79>", line 1, in <module>
    f()
  File "<pyshell#76>", line 5, in sum
    ax = ax + n
TypeError: unsupported operand type(s) for +: 'int' and 'list'
>>> list(map(lambda x:x*x,[1,2,3,4,5,6]))
[1, 4, 9, 16, 25, 36]
>>> f=lambda x:x*x

>>> f
<function <lambda> at 0x0000000002BDFC80>
>>> f(5)
25

>>> def now():
    print('484613')

    
>>> f=now

>>> f.__name__
'now'
>>> now.__nmae__
Traceback (most recent call last):
  File "<pyshell#89>", line 1, in <module>
    now.__nmae__
AttributeError: 'function' object has no attribute '__nmae__'
>>> now.__name__
'now'
>>> def log(func):
    def wr(*args,**kw):
        print('call %s:'%func.__name__)
        return func(*args,**kw)
    return wr

>>> @log
def now():
    print('541123')

    
>>> now()
call now:
541123
>>> def now():
    print('541123')

    
>>> log(now)
<function log.<locals>.wr at 0x0000000002BF0400>
>>> f=log(now)
>>> f
<function log.<locals>.wr at 0x0000000002BF0048>
>>> now=log(now)
>>> now
<function log.<locals>.wr at 0x0000000002BDF8C8>
>>> now()
call now:
541123
>>> int('546156')
546156
>>> int('16',base=8)
14
>>> int('16',base =16)
22
>>> int('16',base=2)
Traceback (most recent call last):
  File "<pyshell#113>", line 1, in <module>
    int('16',base=2)
ValueError: invalid literal for int() with base 2: '16'
>>> int('101010',base=2)
42
>>> int('10000',base=2)
16
>>> import functool
Traceback (most recent call last):
  File "<pyshell#116>", line 1, in <module>
    import functool
ModuleNotFoundError: No module named 'functool'
>>> import functools
>>> int2=functools.
SyntaxError: invalid syntax
>>> int2=functools.partial(int,base=2)
>>> int('10000')
10000
>>> int2('52')
Traceback (most recent call last):
  File "<pyshell#121>", line 1, in <module>
    int2('52')
ValueError: invalid literal for int() with base 2: '52'
>>> int2('10000')
16
>>> #!/usr/bin/env python3
# -*- coding: utf-8 -*-

' a test module '

__author__ = 'Michael Liao'

import sys

def test():
    args = sys.argv
    if len(args)==1:
        print('Hello, world!')
    elif len(args)==2:
        print('Hello, %s!' % args[1])
    else:
        print('Too many arguments!')

if __name__=='__main__':

 
SyntaxError: multiple statements found while compiling a single statement
>>> #!/usr/bin/env python3
# -*- coding: utf-8 -*-

' a test module '

__author__ = 'Michael Liao'

import sys

def test():
    args = sys.argv
    if len(args)==1:
        print('Hello, world!')
    elif len(args)==2:
        print('Hello, %s!' % args[1])
    else:
        print('Too many arguments!')

if __name__=='__main__':
    test()
    
SyntaxError: multiple statements found while compiling a single statement
>>> import hello
Traceback (most recent call last):
  File "<pyshell#125>", line 1, in <module>
    import hello
ModuleNotFoundError: No module named 'hello'
>>> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值