zoukankan      html  css  js  c++  java
  • day1 -- Python变量、注释、格式化输出字符串、input、if、while、for

    1.python变量

      不需要声明类型,直接 变量名 = 变量值,如 : name = "hahaha"

    2.注释:

      单行注释,前面加 #,如  # print(info) 

      多行注释,用三组单引号或者三组双引号包围起来,如   '''print(info)'''   """print(info)""" 

      注意:三组单引号包围起来的变量可以直接格式化输出,看下面的3的第一个案例

    3.格式化输出字符串

    1 info = '''name is %s and age is %s''' % (name, age)
    2 info2 = "name2 is {n} and age2 is {a}".format(n=name, a=age)
    3 info3 = "name3 is {0} and age3 is {1}".format(name, age)

     4.input()方法默认输入存储的类型是str,如果需要转换类型可以使用类型转换函数如int(),如:

    age = int(input("input your age:"))

    5.if、elif、else:注意顶格对齐

    my_age = int(input("guess my age:"))
    if my_age > 60:
        print("you guess error")
    elif my_age == 60:
        print("you guess right")
    else:
        print("hhh")

    6.while

    count = 0
    while count < 3:
        age = int(input("guess my age:"))
        if age < 50:
            print("sorry, your answer is error")
        elif age == 50:
            print("{a} is right".format(a=age))
            break
        else:
            print("hhh")
        count += 1
    # while条件不符合,则走else里面的
    else:
        print("you have tried too many times!!!")

    7.for

    for i in range(3):
        age = int(input("guess my age:"))
        if age < 50:
            print("sorry, your answer is error")
        elif age == 50:
            print("{a} is right".format(a=age))
            break
        else:
            print("hhh")
    # for正常结束,则走else里面的,
    # 如果for被break,则不走else里面的
    else:
        print("you have tried too many times!!!")

      tip:range方法看下面

    8.range方法

    # 起始,终点,步长,输出0,2,4,6,8
    for i in range(0, 10, 2):
        print("i = ", i)
    
    # 默认步长为 1,即输出1-99
    for j in range(99):
        print(i)
  • 相关阅读:
    mysql命令集锦
    linux 删除文件名带括号的文件
    linux下的cron定时任务
    struts2文件下载的实现
    贴一贴自己写的文件监控代码python
    Service Unavailable on IIS6 Win2003 x64
    'style.cssText' is null or not an object
    "the current fsmo could not be contacted" when change rid role
    远程激活程序
    新浪图片病毒
  • 原文地址:https://www.cnblogs.com/convict/p/10252722.html
Copyright © 2011-2022 走看看