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

目录

一、python开发环境

二、python输入输出

(1)print输出函数

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

(2)input输入函数

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

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

​编辑

三、数据类型、运算符

数据类型

运算符


一、python开发环境

python-3.8.6-amd64

二、python输入输出

(1)print输出函数

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

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

(2)input输入函数

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

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

python 复制代码
input(["请输入:"])
print(type(input("未结束~")))

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

python 复制代码
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)​

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

python 复制代码
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;,此时 fTrue;如果 e0,则 fFalse
  • bool转int : False 转换为 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

运算器代码一

python 复制代码
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("除数不能为零")
                continue
            operations = {
                '+': 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("无效的运算符,请重新输入。")
                continue
            result = operations[operator]
            print(f"结果是: {result}")
            if input("是否进行另一次计算?(y/n): ").lower() != 'y':
                break
        except ValueError:
            print("输入无效,请输入有效的数字或值。")

if __name__ == "__main__":
    calculator()

运算器代码二

python 复制代码
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 + num2
            elif operator == '-':
                result = num1 - num2
            elif operator == '*':
                result = num1 * num2
            elif operator == '/':
                if num2 == 0:
                    print("除数不能为零")
                    continue
                result = num1 / num2
            elif operator == '//':
                if num2 == 0:
                    print("除数不能为零")
                    continue
                result = num1 // num2
            elif operator == '%':
                if num2 == 0:
                    print("除数不能为零")
                    continue
                result = num1 % num2
            elif operator == '**':
                result = num1 ** num2
            # 关系运算符
            elif operator == '>':
                result = num1 > num2
            elif operator == '<':
                result = num1 < num2
            elif operator == '==':
                result = num1 == num2
            elif operator == '!=':
                result = num1 != num2
            elif operator == '>=':
                result = num1 >= num2
            elif operator == '<=':
                result = num1 <= num2
            # 逻辑运算符
            elif operator == 'and':
                result = num1 and num2
            elif operator == 'or':
                result = num1 or num2
            elif operator == 'not':
                result = not num1
            # 身份运算符
            elif operator == 'is':
                result = num1 is num2
            elif operator == 'is not':
                result = num1 is not num2
            else:
                print("无效的运算符,请重新输入。")
                continue

            print(f"结果是: {result}")

            another_calculation = input("是否进行另一次计算?(y/n): ")
            if another_calculation.lower() != 'y':
                break
        except ValueError:
            print("输入无效,请输入有效的数字或值。")


if __name__ == "__main__":
    calculator()
相关推荐
伊织code2 小时前
PyTorch API 5 - 全分片数据并行、流水线并行、概率分布
pytorch·python·ai·api·-·5
风逸hhh2 小时前
python打卡day25@浙大疏锦行
开发语言·python
魔尔助理顾问3 小时前
Flask如何读取配置信息
python·flask·bootstrap
jc_hook4 小时前
Python 接入DeepSeek
python·大模型·deepseek
chicpopoo5 小时前
Python打卡DAY25
开发语言·python
crazyme_65 小时前
深入掌握 Python 切片操作:解锁数据处理的高效密码
开发语言·python
Code_流苏6 小时前
《Python星球日记》 第69天:生成式模型(GPT 系列)
python·gpt·深度学习·机器学习·自然语言处理·transformer·生成式模型
于壮士hoho7 小时前
Python | Dashboard制作
开发语言·python
掘金-我是哪吒7 小时前
分布式微服务系统架构第131集:fastapi-python
分布式·python·微服务·系统架构·fastapi
小猪快跑爱摄影8 小时前
【Folium】使用离线地图
python