Linux中rhl8下Python3的部分用法 ---变量命名、数据类型、运算符、输入与输出、格式化字符串、if语句、while循环和for循环

一、变量命名

1.rhl8中Python3可以直接使用,建议安装Python2

2.变量定义

在这里插入图片描述

二、数据类型

int,        整型 
float,      浮点型
bool,       波尔型
complex,     
str         字符串

三、运算符

+             加法
-             减法
*             乘法
**            次方
/             除法
//            除法取整
%             除法取余
and,or,not    逻辑与,或,非

在这里插入图片描述

四、输入与输出

在这里插入图片描述
在这里插入图片描述

五、格式化字符串

在这里插入图片描述

六、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的阶乘

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值