zoukankan      html  css  js  c++  java
  • 变量及基本语句

    Linux里需要声明解释器

    #!/usr/bin/env python
    • 变量:

    声明变量

    name="q1ang"#字符串
    print("My name is",name)#再次调用

    变量定义的规则:

      1. 变量名只能是 字母、数字或下划线的任意组合
      2. 变量名的第一个字符不能是数字
      3. 以下关键字不能声明为变量名
        关键字['and'(与), 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
      4. 变量命名需要有含义,方便认知(不要写拼音、不要太长)
        [GFofQ1ang,GF_of_q1ang]

    变量命名习惯:

    1. 驼峰体:AgeOfQ1ang,HeightOfQ1ang
    2. 下划线:age_of_q1ang,height_of_q1ang(官方推荐)

    python不支持常量,使用全部大写的变量名代表常量

    PIE=3.14#使用大写代表常量  
    •  读取用户输入输出:
    print('hello world !')
    a=input('>>>')
    • 格式化输出

    %s(字符型),%d(整型),%f(浮点型),%.xf(保留x位字符)
    强制转换格式int()、str()...

    print("xxx%d %s ",%(xx,xx))
    aaa='''xxx{xxx}'''.format{xxx=x1}
    aaa='''xxx{0}xxx{1}'''.format{xxx,xxx2}
    •  注释

    单行注释:#代码内容
    多行注释:"""代码内容"""

    •  流程控制

    单分支、双分支、多分支

    if xx:
        xxx
    elif xxx:
        xxx
    else
        xxx
    • while循环
    while xxx:
    xxx
    else:
    xxx
    • for循环

    for i in rang(10):
         print(i)
    else:
        xxx

    • range()

    range(0,10,2)

    range(起始,终止,步长)

    • continue 跳出本次循环,继续下一次循环
    • break 结束当前循环

     

     
  • 相关阅读:
    LeetCode 326. Power of Three
    LeetCode 324. Wiggle Sort II
    LeetCode 322. Coin Change
    LeetCode 321. Create Maximum Number
    LeetCode 319. Bulb Switcher
    LeetCode 318. Maximum Product of Word Lengths
    LeetCode 310. Minimum Height Trees (DFS)
    个人站点大开发!--起始篇
    LeetCode 313. Super Ugly Number
    LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (DP)
  • 原文地址:https://www.cnblogs.com/q1ang/p/8732841.html
Copyright © 2011-2022 走看看