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)
  • 相关阅读:
    轮子来袭 vJine.Core Orm 之 03_架构分析
    轮子来袭 vJine.Core 之 AppConfig<T>
    C# 数据类型映射 (SQLite,MySQL,MSSQL,Oracle)
    炫酷 2048 完全免费 色彩无线 流畅如飞
    DebugLog 打印方法执行时间
    [转] charles使用教程指南
    Macbook Pro配置PHP开发环境
    基于ubuntu 14搭建nginx+php+mysql环境
    Android Studio -修改LogCat的颜色
    [转]
  • 原文地址:https://www.cnblogs.com/convict/p/10252722.html
Copyright © 2011-2022 走看看