zoukankan      html  css  js  c++  java
  • python基础语法

    https://www.cnblogs.com/happyframework/p/3255962.html

     #for循环类似C#的foreach,注意for后面是没有括号的,python真是能简洁就尽量简洁。
     2 for x in range(1, 10):
     3     print(x)
     4 
     5 for key in {"x":"xxx"}:
     6     print(key)
     7 
     8 for key, value in {"x":"xxx"}.items():
     9     print(key, value)
    10 
    11 for x, y, z in [["a", 1, "A"],["b", 2, "B"]]:
    12     print(x, y, z)



     #带索引的遍历
     2 for index, value in enumerate(range(0, 10)):
     3     print(index, value)
     4 
     5 #好用的zip方法
     6 for x, y in zip(range(1, 10), range(1, 10)):
     7     print(x, y)
     8 
     9 #循环中的的else子句
    10 from math import sqrt
    11 for item in range(99, 1, -1):
    12     root = sqrt(item)
    13     if(root == int(root)):
    14         print(item)
    15         break
    16 else:
    17     print("没有执行break语句。")



    1 #pass、exec、eval
    2 if(1 == 1):
    3     pass
    4 
    5 exec('print(x)', {"x": "abc"})
    6 print(eval('x*2', {"x": 5}))
    # 基本函数定义。
     2 def func():
     3     print("func")
     4 
     5 func()
     6 
     7 # 带返回值的函数。
     8 def func_with_return():
     9     return ("func_with_return")
    10 
    11 print(func_with_return())
    12 
    13 # 带多个返回值的函数。
    14 def func_with_muti_return():
    15     return ("func_with_muti_return", "func_with_muti_return")
    16 
    17 print(func_with_muti_return())
    18 
    19 # 位置参数
    20 def func_with_parameters(x, y):
    21     print(x, y)
    22 
    23 func_with_parameters(1, 2)
    24 
    25 # 收集多余的位置参数
    26 def func_with_collection_rest_parameters(x, y, *rest):
    27     print(x, y)
    28     print(rest)
    29 
    30 func_with_collection_rest_parameters(1, 2, 3, 4, 5)
    31 
    32 #命名参数
    33 def func_with_named_parameters(x, y, z):
    34     print(x, y, z)
    35 
    36 func_with_named_parameters(z = 1, y = 2, x = 3)
    37 
    38 #默认值参数
    39 def func_with_default_value_parameters(x, y, z = 3):
    40     print(x, y, z)
    41 
    42 func_with_default_value_parameters(y = 2, x = 1)
    43 
    44 #收集命名参数
    45 def func_with_collection_rest_naned_parameters(*args, **named_agrs):
    46     print(args)
    47     print(named_agrs)
    48 
    49 func_with_collection_rest_naned_parameters(1, 2, 3, x = 4, y = 5, z = 6)
    50 
    51 #集合扁平化
    52 func_with_collection_rest_naned_parameters([1, 2, 3], {"x": 4, "y": 4, "z": 6}) #这会导致args[0]指向第一个实参,args[1]指向第二个实参。
    53 func_with_collection_rest_naned_parameters(*[1, 2, 3], **{"x": 4, "y": 4, "z": 6}) #这里的执行相当于func_with_collection_rest_naned_parameters(1, 2, 3, x = 4, y = 5, z = 6)。




  • 相关阅读:
    delphi 控制音量 静音的类
    delphi java 日期 转换 获取Unix时间戳
    UI颜色值
    ios10 no route to host
    VMWare MAC系统调整磁盘
    手机传文字到电脑
    Delphi JCL JEDI使用 jclDebug
    PS 使用首记 修改png图片的颜色
    delphi Style TBitmapLink
    delphi IOS 获取电池信息
  • 原文地址:https://www.cnblogs.com/CWQPHP/p/9803972.html
Copyright © 2011-2022 走看看