- Python语言非常注重格式要求,if语句判断的后面要有缩进。
- 变量命名规则:
- 字母数字下划线组成
- 不能以数字开头,不能含有特殊字符
- 不能以关键字命名
- 不能以汉字命名
- 定义的变量名应该有意义
- 驼峰式命名,下划线分割单词
- 变量名区分大小写
- 赋值运算符:= ,+=,-=,*=,/=,%=,//=,**=
- num=5
- num **=2相当于num=num**2 = 25
- 逻辑运算符:not, and, or
- and 且,并且,只有两个条件全部为True(正确),结果才为真
- 条件1 and 条件2
- or 或,或者 只要有一个条件为True,结果为真
- not 不
- 注释:(1)单行注释 # (2)多行注释 ''' ... ''' 或 """ """
- 简单的用户输入输出:
name = input("name:")
password=input("password:") #input接受的所有数据都是字符串,即便你输入的数字,也会被当成字符串来用
print("姓名:")
- int 整数类型 把字符串转成int,用int(被转的数据)
- str 字符串类型 把数据转成字符串用str(被转的数据
一:if语句实现猜年龄
age = 56
guess_age=int(input("age:"))
if guess_age==age:
print("yes,you got it...")
elif guess_age>age:
print("should guess smaller..")
else:
print("should bigger")
二:多分支if语句判断学生分数等级:
score=int(input("score:"))
if score>90:
print("A")
elif score>80:
print("B")
elif score>70:
print("C")
elif score>50:
print("D")
else:
print("滚")