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)
  • 相关阅读:
    用Python作GIS:择菜篇
    【嵌入式开发技术之串口】Linux下串口主机程序
    1033,2052 是什么意思?
    Telnet协议详解
    生活情景英语:英语国家生存英语精选
    windows下搭建iphone开发环境
    开公司的流程
    解决某物流企业二维码打印问题
    数字黑洞
    InnoSetup 打包代码 检测.netFramework
  • 原文地址:https://www.cnblogs.com/convict/p/10252722.html
Copyright © 2011-2022 走看看