一、变量命名
1.rhl8中Python3可以直接使用,建议安装Python2
2.变量定义
二、数据类型
int, 整型 float, 浮点型 bool, 波尔型 complex, str 字符串
三、运算符
+ 加法 - 减法 * 乘法 ** 次方 / 除法 // 除法取整 % 除法取余 and,or,not 逻辑与,或,非
四、输入与输出
input和print
五、格式化字符串
六、if语句、while循环和for循环
- 单分支语句
- 双分支语句
- 多分支语句
- 三元运算符
#!/usr/bin/python3 #1 单分支语句 name = input("Name:") if name == "root": print("user is root") #2 双分支语句 age = int(input("Age:")) if age >= 18: print("adult") else: print("no adult") #3 多分支语句 pc_type = input("please input computer type(L,W,M):") if pc_type == 'L': print("Linux pc_type") elif pc_type == 'W': print("Windows pc_type") elif pc_type == 'M': print("Mac pc_type") else: print("Not know pc_type") #4 三元运算符 a = 1; b = 2 max = a if a > b else b print(max)
2.while循环实现用户登录小于三次,第三次失败给予警告
#!/usr/bin/python3 try_count = 0 while try_count < 3: print("**************") name = input("Name:") password = input("Passwd:") # if name == 'root' and password == 'westos': print("user is ok") break try_count += 1 print("try %d count" %(try_count)) else: print("try count is more, please wait try")
3.for循环计算10的阶乘
版权声明:本文为Horizon_carry原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。










