当前位置: 移动技术网 > IT编程>脚本编程>Python > 变量、常量、input、if语句

变量、常量、input、if语句

2019年06月04日  | 移动技术网IT编程  | 我要评论

山渣油,sns网站有哪些,搜源码

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.")

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网