zoukankan      html  css  js  c++  java
  • Python学习笔记

    s='alice'
    
    print(s,type(s),id(s)) 

    打印结果: s <class 'str' 32165416>

    打印: 字符串内容,type(s):变量s对应的数据类型,id(s):变量s对应的内存地址
    == / is
    s1='alice;
    s2='alice'
    print(s1==s2) true , == 比较的是两个变量的值
    print(s1 is s2)  true , is 比较的是内存地址,是不是同一个对象

    listA = [1,2,3]
    listB = [1,2,3]
    print(listA == listB) true
    print(listA is listB) false
    以上 列表 字典 元组,set 类似
    函数 
    计算0到len的和
    def getFun(len):
        int sum= 0
        for i in range(0 ,len):
             sum+=i
        return sum
    
    print(getFun(10))

    //默认参数
    def showInfo(name,sex='男'):
      print('姓名:%s,性别:%s'%(name,sex))

    showInfo('张三')
    showInfo('王五')
    showInfo('小美','女')
    姓名:张三,性别:男
    姓名:王五,性别:男
    姓名:小美,性别:女

    #返回多个结果
    
    import math
    
    def move(x, y, step, angle=0):
        nx = x + step * math.cos(angle)
        ny = y - step * math.sin(angle)
        return nx, ny
    
    >>> x, y = move(100, 100, 60, math.pi / 6)
    >>> print(x, y)
    151.96152422706632 70.0
    #可变参数
    
    def calc(*numbers):
        sum = 0
        for n in numbers:
            sum = sum + n * n
        return sum
    
    >>> calc(1, 2)
    5
    >>> calc()
    0
    关键字参数
    
    可变参数允许你传入0个或任意个参数,这些可变参数在函数调用时自动组装为一个tuple。而关键字参数允许你传入0个或任意个含参数名的参数,这些关键字参数在函数内部自动组装为一个dict。请看示例:
    
    def person(name, age, **kw):
        print('name:', name, 'age:', age, 'other:', kw)
    函数person除了必选参数name和age外,还接受关键字参数kw。在调用该函数时,可以只传入必选参数:
    
    >>> person('Michael', 30)
    name: Michael age: 30 other: {}
    也可以传入任意个数的关键字参数:
    
    >>> person('Bob', 35, city='Beijing')
    name: Bob age: 35 other: {'city': 'Beijing'}
    >>> person('Adam', 45, gender='M', job='Engineer')
    name: Adam age: 45 other: {'gender': 'M', 'job': 'Engineer'}
    关键字参数有什么用?它可以扩展函数的功能。比如,在person函数里,我们保证能接收到name和age这两个参数,但是,如果调用者愿意提供更多的参数,我们也能收到。试想你正在做一个用户注册的功能,除了用户名和年龄是必填项外,其他都是可选项,利用关键字参数来定义这个函数就能满足注册的需求。
  • 相关阅读:
    POJ 1953 World Cup Noise
    POJ 1995 Raising Modulo Numbers (快速幂取余)
    poj 1256 Anagram
    POJ 1218 THE DRUNK JAILER
    POJ 1316 Self Numbers
    POJ 1663 Number Steps
    POJ 1664 放苹果
    如何查看DIV被设置什么CSS样式
    独行DIV自适应宽度布局CSS实例与扩大应用范围
    python 从入门到精通教程一:[1]Hello,world!
  • 原文地址:https://www.cnblogs.com/xqxacm/p/9795009.html
Copyright © 2011-2022 走看看