欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > 蓝桥杯python语言基础(1)——编程基础

蓝桥杯python语言基础(1)——编程基础

2025/2/1 6:29:44 来源:https://blog.csdn.net/m0_73631172/article/details/145146730  浏览:    关键词:蓝桥杯python语言基础(1)——编程基础

目录

一、python开发环境

二、python输入输出

(1)print输出函数

print(*object,sep='',end='\n',......)

 (2)input输入函数

input([prompt]), 输入的变量均为str字符串类型!

 input()会读入一整行的信息

​编辑

三、数据类型、运算符

数据类型

运算符


一、python开发环境

python-3.8.6-amd64

二、python输入输出

(1)print输出函数

print(*object,sep='',end='\n',......)

  • *object: 这是一个可变参数,可以传入多个对象,这些对象会被依次打印出来。
  • sep: 指定多个对象之间的分隔符,默认是一个空格。
  • end: 指定打印结束后的字符,默认是换行符 '\n'
print("LanQiao",sep="needs",end="yuan\n")
print("LanQiao","300",sep="needs",end="yuan")

 (2)input输入函数

input([prompt]), 输入的变量均为str字符串类型!

在 Python 的文档和教程中,input([prompt]) 中的 [prompt] 被方括号括起来,表示这是一个可选参数。这种表示方法是一种约定,用于指示该参数是可选的,而不是必须提供的。

input(["请输入:"])
print(type(input("未结束~")))

 input()会读入一整行的信息

a=int(input("请输入:"))
print((a,type(a)))
b=int(input("请输入:")) #
# input() 函数返回的是一个字符串,
# 尝试将这个字符串直接转换为整数。
# 如果输入的内容包含空格或其他非数字字符,
# 转换就会失败并抛出 ValueError 异常。
print(b)

例题1.1

海伦公式用于计算已知三边长的三角形面积。公式如下:

s=p(p−a)(p−b)(p−c)s=p(p−a)(p−b)(p−c)​

$s = \sqrt{p(p - a)(p - b)(p - c)}$

其中,p 是半周长,计算公式为:

p = \frac{a + b + c}{2}

a = int(input("输入"));b = int(input("输入"));c = int(input("输入"))
p = (a+b+c)/2
s=(p*(p-a)*(p-b)*(p-c))**0.5
print(s)

三、数据类型、运算符

数据类型

  • int转float: 直接转换。例如,int a = 5; float b = (float)a;,此时 b 的值为 5.0
  • float转int: 舍弃小数部分。例如,float c = 3.7; int d = (int)c;,此时 d 的值为 3
  • int转bool: 非0转换为 True,0转换为 False。例如,int e = 1; bool f = (bool)e;,此时 f 为 True;如果 e 为 0,则 f 为 False
  • bool转intFalse 转换为 0True 转换为 1。例如,bool g = True; int h = (int)g;,此时 h 的值为 1
  • 转str: 直接转换。例如,int i = 10; string j = (string)i;,此时 j 的值为 "10"

运算符

  • 算术运算符:用于数学计算。

    • +:加法
    • -:减法
    • *:乘法
    • /:除法
    • //:整除(返回商的整数部分)
    • %:求余(返回除法后的余数)
    • **:幂(表示乘方运算)
  • 关系运算符:用于比较两个值。

    • >:大于
    • <:小于
    • ==:等于
    • !=:不等于
    • >=:大于等于
    • <=:小于等于
  • 赋值运算符:用于将值赋给变量。

    • =:赋值
    • +=:加赋值(相当于 x = x + y
    • -=:减赋值(相当于 x = x - y
    • *=:乘赋值(相当于 x = x * y
    • /=:除赋值(相当于 x = x / y
    • %=:求余赋值(相当于 x = x % y
    • //=:整除赋值(相当于 x = x // y
    • **=:幂赋值(相当于 x = x ** y
  • 逻辑运算符:用于逻辑操作。

    • and:逻辑与
    • or:逻辑或
    • not:逻辑非
  • 成员运算符:用于判断一个值是否在序列中。

    • in:在...之中
    • not in:不在...之中
  • 身份运算符:用于比较对象的身份(内存地址)。

    • is:是
    • is not:不是

例题1.2

运算器代码一

def calculator():while True:try:num1 = float(input("请输入第一个数字: "))operator = input("请输入运算符 (+, -, *, /, //, %, **, >, <, ==,!=, >=, <=, and, or, not, is, is not): ")num2 = float(input("请输入第二个数字或值: "))if operator in ('/', '//', '%') and num2 == 0:print("除数不能为零")continueoperations = {'+': num1 + num2, '-': num1 - num2, '*': num1 * num2,'/': num1 / num2 if num2 else None, '//': num1 // num2 if num2 else None,'%': num1 % num2 if num2 else None, '**': num1 ** num2,'>': num1 > num2, '<': num1 < num2, '==': num1 == num2,'!=': num1 != num2, '>=': num1 >= num2, '<=': num1 <= num2,'and': bool(num1) and bool(num2), 'or': bool(num1) or bool(num2),'not': not bool(num1), 'is': num1 == num2, 'is not': num1 != num2}if operator not in operations:print("无效的运算符,请重新输入。")continueresult = operations[operator]print(f"结果是: {result}")if input("是否进行另一次计算?(y/n): ").lower() != 'y':breakexcept ValueError:print("输入无效,请输入有效的数字或值。")if __name__ == "__main__":calculator()

运算器代码二

def calculator():while True:try:num1 = input("请输入第一个数字: ")num1 = float(num1)operator = input("请输入运算符 (+, -, *, /, //, %, **, >, <, ==,!=, >=, <=, and, or, not, is, is not): ")num2 = input("请输入第二个数字或值: ")num2 = float(num2)# 算术运算符if operator == '+':result = num1 + num2elif operator == '-':result = num1 - num2elif operator == '*':result = num1 * num2elif operator == '/':if num2 == 0:print("除数不能为零")continueresult = num1 / num2elif operator == '//':if num2 == 0:print("除数不能为零")continueresult = num1 // num2elif operator == '%':if num2 == 0:print("除数不能为零")continueresult = num1 % num2elif operator == '**':result = num1 ** num2# 关系运算符elif operator == '>':result = num1 > num2elif operator == '<':result = num1 < num2elif operator == '==':result = num1 == num2elif operator == '!=':result = num1 != num2elif operator == '>=':result = num1 >= num2elif operator == '<=':result = num1 <= num2# 逻辑运算符elif operator == 'and':result = num1 and num2elif operator == 'or':result = num1 or num2elif operator == 'not':result = not num1# 身份运算符elif operator == 'is':result = num1 is num2elif operator == 'is not':result = num1 is not num2else:print("无效的运算符,请重新输入。")continueprint(f"结果是: {result}")another_calculation = input("是否进行另一次计算?(y/n): ")if another_calculation.lower() != 'y':breakexcept ValueError:print("输入无效,请输入有效的数字或值。")if __name__ == "__main__":calculator()

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com