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

news/2025/2/2 21:06:36 标签: python

目录

python%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83-toc" name="tableOfContents" style="margin-left:0px">一、python开发环境

python%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA-toc" name="tableOfContents" style="margin-left:0px">二、python输入输出

(1)print输出函数

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

 (2)input输入函数

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

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

​编辑

三、数据类型、运算符

数据类型

运算符


python%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83" name="%E4%B8%80%E3%80%81python%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83">一、python开发环境

python-3.8.6-amd64

python%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA" name="%E4%BA%8C%E3%80%81python%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA">二、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)​

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

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

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

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;,此时 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

运算器代码一

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()


http://www.niftyadmin.cn/n/5840296.html

相关文章

Scratch 《像素战场》系列综合游戏:像素战场游戏Ⅰ~Ⅲ 介绍

资源下载 Scratch《像素战场》系列综合游戏合集&#xff1a;像素战场游戏Ⅰ~Ⅲ压缩包 https://download.csdn.net/download/leyang0910/90332765 游戏操作介绍 Scratch 《像素战场Ⅰ》操作规则&#xff1a; 这是一款与朋友一起玩的 1v1 游戏。先赢得6轮胜利&#xff01; WA…

如何编写一个MyBatis插件?

大家好&#xff0c;我是锋哥。今天分享关于【Redis为什么这么快?】面试题。希望对大家有帮助&#xff1b; 如何编写一个MyBatis插件&#xff1f; 1000道 互联网大厂Java工程师 精选面试题-Java资源分享网 编写 MyBatis 插件需要使用 MyBatis 提供的插件接口&#xff0c;MyBa…

CSS 基础:层叠、优先级与继承

CSS 基础&#xff1a;层叠、优先级与继承 一、层叠&#xff08;Cascade&#xff09;示例&#xff1a;层叠的顺序 二、优先级&#xff08;Specificity&#xff09;优先级规则示例&#xff1a;优先级的比较 三、继承&#xff08;Inheritance&#xff09;哪些属性会被继承&#xf…

C++哈希(链地址法)(二)详解

文章目录 1.开放地址法1.1key不能取模的问题1.1.1将字符串转为整型1.1.2将日期类转为整型 2.哈希函数2.1乘法散列法&#xff08;了解&#xff09;2.2全域散列法&#xff08;了解&#xff09; 3.处理哈希冲突3.1线性探测&#xff08;挨着找&#xff09;3.2二次探测&#xff08;跳…

HTB:Administrator[WriteUP]

目录 连接至HTB服务器并启动靶机 信息收集 使用rustscan对靶机TCP端口进行开放扫描 将靶机TCP开放端口号提取并保存 使用nmap对靶机TCP开放端口进行脚本、服务扫描 使用nmap对靶机TCP开放端口进行漏洞、系统扫描 使用nmap对靶机常用UDP端口进行开放扫描 使用nmap对靶机…

11.网络编程的基础知识

11.网络编程的基础知识 **1. OSI模型与TCP/IP模型****2. IP地址分类****3. Socket编程****4. TCP三次握手与四次挥手****5. 常用网络测试工具****6. 练习与作业****7. 总结** 1. OSI模型与TCP/IP模型 OSI模型&#xff08;开放系统互联模型&#xff09;&#xff1a; 7层结构&am…

第十章:大内存的申请和释放

目录 第一节&#xff1a;函数修改 1-1.ConcurrentAlloc.h 1-2.Common.h 1-3.PageCache.cpp 第二节&#xff1a;测试 第三节&#xff1a;结语 大内存的思路是将其以一页为对齐数&#xff0c;申请一个为切分的span&#xff0c;这种span在pc就有&#xff0c;所以直接到pc中申请…

一文讲解Java中的ArrayList和LinkedList

ArrayList和LinkedList有什么区别&#xff1f; ArrayList 是基于数组实现的&#xff0c;LinkedList 是基于链表实现的。 二者用途有什么不同&#xff1f; 多数情况下&#xff0c;ArrayList更利于查找&#xff0c;LinkedList更利于增删 由于 ArrayList 是基于数组实现的&#…