变量、常量、input、if语句

2023-01-31 00:01:24 语句 变量 常量

Chapter2 Python基础

2.1 变量命名规则

  • 字母、数字、下划线组成
  • 不能数字开头
  • 不能使用python中的关键字
  • 变量名具有意义
  • 推荐驼峰(ArvinGood)、或者下划线方式 (arvin_good) 命名

标准示例:

your_name = 'Jack Ma'

2.2 常量

  • 全部大写的变量名就是常量 ARVIN = 99

示例:

ARVIN_AGE = 18

2.3 注释

  • 三种方式:
    1. “#”号单行注释
    2. ‘’‘三个单引号和三个双引号”“”进行多行注释

例:

# The idle boy is almost invariably poor and anxious. 单行注释

'''
这是多行注释示范
'''

"""
多行注释示范
"""

2.4 输入输出

  • 用户输入 input
    1. 注意:input获取到的内容都是字符串类型
  • 用户输出 print
    1. print打印出内容
your_id = input("input your id identifier: ")
your_pwd = input("input your passWord: ")
print("Your id is {}, Your password is {}".fORMat(your_id, your_pwd))

运行结果:
input your id identifier: Jack
input your password: default
Your id is Jack, Your password is default

2.5 五种if条件判断语句

  • 单if条件判断

    if 条件: #条件判断 > < ==

    ​ print(‘真实’) #条件成立则执行

    if 10 > 8:
        print("You are right.")
  • if else条件判断

    if 10 > 20:
      print("10 is bigger than 2.")
    else:
      print("10 is smaller than 2.")
  • if elif elif else 条件判断

    a = "jack"
    if 10 > 50:
      print("10 is bigger than 5.")
    elif type(a) == "str":
      print("a is a string.")
    elif 20 == 20:
      print("20 is equal 20.")
    else:
        print("All of them are false.")
    
    运行结果:
    20 is equal 20.
  • 多个同级if语句

    a = "jack"
    if 10 == 20:
      print("10 is equal 20.")
    if a == "jack":
      print(a ,"is the same as jack.")
    if 10 == 10:
      print("10 is equal 10.")
    
    运行结果:
    jack is the same as jack.
    10 is equal 10.
  • if与if语句之间的嵌套

    id = int(input("input your id number: "))
    pwd = input("input your passwrd: ")
    if id == 9:
      print("GO ON.")
      if pwd == "default":
          print("You logged in successfully.")
      else:
          print("Your password is incorrect.")
    else:
      print("Your id was typed incorrectly.")

相关文章