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

ops/2025/2/1 8:23:39/

目录

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("除数不能为零")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()

运算器代码二

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


http://www.ppmy.cn/ops/154692.html

相关文章

JVM_程序计数器的作用、特点、线程私有、本地方法的概述

①. 程序计数器 ①. 作用 (是用来存储指向下一条指令的地址,也即将要执行的指令代码。由执行引擎读取下一条指令) ②. 特点(是线程私有的 、不会存在内存溢出) ③. 注意:在物理上实现程序计数器是在寄存器实现的,整个cpu中最快的一个执行单元 ④. 它是唯一一个在java虚拟机规…

axios如何利用promise无痛刷新token

目录 需求 需求解析 实现思路 方法一&#xff1a; 方法二&#xff1a; 两种方法对比 实现 封装axios基本骨架 instance.interceptors.response.use拦截实现 问题和优化 如何防止多次刷新token 同时发起两个或以上的请求时&#xff0c;其他接口如何重试 最后完整代…

EXCEL教程:如何打开Excel隐藏部分?

在Excel文件中&#xff0c;数据表格的制作是日常工作中不可或缺的一部分。然而&#xff0c;有时候&#xff0c;我们可能会遇到一些数据不方便直接显示但又不能删除的情况。这时&#xff0c;隐藏数据就成为了一个常见的解决方案。但隐藏之后&#xff0c;如何再次打开隐藏部分&am…

socket编程短平快

创建文件描述符 #include<sys/socket.h> int socket_fd socket(AF_INET,SOCK_STREAM,0); //AF_INET address family internet 地址族&#xff0c; 代表ipv4 //SOCK_STREAM TCP协议 //最后的参数是默认配置。文件描述符配置 int opt 1; setsockopt(socket_fd,SOL_SOCK…

SpringBoot 配置文件

目录 一. 配置文件相关概念 二. 配置文件快速上手 1. 配置文件的格式 2. properties 配置文件 (1) properties 基本语法 (2) 读取配置文件内容 (3) properties 缺点分析 3. yml配置文件 (1) yml 基本语法 (2) 读取配置文件内容 (3) yml 配置对象 (4) yml 配置集合 …

反向代理模块。。

1 概念 1.1 反向代理概念 反向代理是指以代理服务器来接收客户端的请求&#xff0c;然后将请求转发给内部网络上的服务器&#xff0c;将从服务器上得到的结果返回给客户端&#xff0c;此时代理服务器对外表现为一个反向代理服务器。 对于客户端来说&#xff0c;反向代理就相当于…

基于PyQt设计的智能停车管理系统

文章目录 一、前言1.1 项目介绍【1】项目开发背景【2】设计实现的功能【3】设计意义【4】国内外研究现状【6】摘要1.2 设计思路1.3 系统功能总结1.4 开发工具的选择【1】VSCODE【2】python【3】ptqt【4】HyperLPR31.5 参考文献二、安装Python环境1.1 环境介绍**1.2 Python版本介…

黑马点评 - 商铺类型缓存练习题(Redis List实现)

首先明确返回值是一个 List<ShopType> 类型那么我们修改此函数并在 TypeService 中声明 queryTypeList 方法&#xff0c;并在其实现类中实现此方法 GetMapping("list")public Result queryTypeList() {return typeService.queryTypeList();}实现此方法首先需要…