Python第四课:数据类型与转换


🧵 一、字符串操作

1. 字符串加法(拼接)

  • 使用 + 将两个字符串连接在一起
  • 注意:拼接时不会自动添加空格
python 复制代码
print('hello' + 'world')  # 输出:helloworld

2. 字符串乘法(重复)

  • 使用 * 重复字符串多次
python 复制代码
print('a' * 10)  # 输出:aaaaaaaaaa

🔢 二、数据类型

主要数据类型:

  • 整型 int:如 123
  • 浮点型 float:如 123.0
  • 字符串 str:如 'hello'
  • 布尔型 boolTrueFalse

🔍 三、type() 函数

  • 用于查看变量的数据类型
python 复制代码
a = '123'
print(type(a))  # 输出:<class 'str'>

🔄 四、类型转换

1. str() - 转换为字符串

python 复制代码
string = str(123)  # 整数123转换为字符串"123"

2. int() - 转换为整数

  • 注意:字符串必须只包含数字
python 复制代码
number = int('123')  # 字符串"123"转换为整数123

3. float() - 转换为浮点数

python 复制代码
number = float('123')  # 输出:123.0

🖨️ 五、print() 方法

1. 基本用法

python 复制代码
print('hello world')  # 输出字符串
a = 9
print(a)  # 输出变量值

2. 同时输出多个值

  • 默认用空格分隔
python 复制代码
x, y = 20, 30
print(x, y)  # 输出:20 30

3. 控制换行

  • end 参数控制结尾字符
python 复制代码
print('hello', end=' ')  # 以空格结尾,不换行
print('world')  # 输出:hello world

🎨 六、turtle 模块中的文本功能

1. write() 函数

  • 在画笔位置显示文本
python 复制代码
import turtle
turtle.write('欢迎来到编玩边学', align='center', font=('楷体', 20))
  • 参数:align(对齐方式)、font(字体,大小)

2. textinput() 函数

  • 弹出输入框获取用户输入
python 复制代码
author = turtle.textinput('作者', '请输入你的姓名')
  • 返回值是字符串类型

💡 七、拓展知识

1. 转义字符

  • \n:换行符
  • print()end 参数中使用

2. 占位符 %

可以通过在%后面加( )添加多个变量

%d - 整数占位符
python 复制代码
a = 70
print("我考了%d分" % a)  # 输出:我考了70分
%f - 浮点数占位符
  • %.xf 精确到小数点后x位
python 复制代码
a = 70.562
print("我卡里有%.2f元钱" % a)  # 输出:我卡里有70.56元钱
%s - 字符串占位符
python 复制代码
a = "小林"
print("大家好,我叫%s,很高兴认识你们!" % a)  # 输出:大家好,我叫小林,很高兴认识你们!

📌 八、重要注意事项

  1. 字符串拼接:不会自动添加空格
  2. 类型转换
    • int() 只能转换纯数字字符串
    • float() 转换后会带小数点
  3. print() 多个值:默认用空格分隔
  4. textinput():返回值总是字符串类型

🏃九、代码演示

python 复制代码
import turtle
# turtle.pencolor("red")
R = 140

turtle.fillcolor("pink")  # 要填充的颜色
turtle.begin_fill()  # 填充起点
turtle.circle(R) # 要填充的对象(非实心)
turtle.end_fill()  # 填充终点

x= 70
y = 160
r= 70

turtle.fillcolor("white")  # 要填充的颜色
turtle.begin_fill()  # 填充起点
turtle.penup()
turtle.goto(-x,y)
turtle.pendown()
turtle.circle(r)  # 画一个半径70 的圆
turtle.end_fill()  # 填充终点

turtle.fillcolor("white")  # 要填充的颜色
turtle.begin_fill()  # 填充起点
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
turtle.circle(r)  # 画一个半径70 的圆
turtle.end_fill()  # 填充终点


turtle.fillcolor("pink")  # 要填充的颜色
turtle.begin_fill()  # 填充起点
turtle.penup()
turtle.goto(-30,220)
turtle.pendown()
turtle.dot(15,"red") # 填充 实心圆

turtle.penup()
turtle.goto(30,220)
turtle.pendown()
turtle.dot(15,"red") # 填充 实心圆
turtle.speed(0)

turtle.penup()
turtle.goto(0,-50)
turtle.pendown()

turtle.write('欢迎来到编玩边学', align='center', font=('楷体',20))

turtle.penup()
turtle.goto(0,-100)
turtle.pendown()

author = turtle.textinput("作者", "请输入你的姓名:")

turtle.write('你好啊,%s !' % author, align='right', font=('楷体',20))

🎯 学习目标回顾

  • 掌握字符串的创建和基本操作
  • 了解不同数据类型及其转换
  • 学会使用 type() 查看数据类型
  • 掌握 print()write() 输出信息
  • 会用 textinput() 获取用户输入
相关推荐
AI算法沐枫2 分钟前
大一学生如何入门机器学习,深度学习,学习顺序如何?
人工智能·python·深度学习·学习·线性代数·算法·机器学习
用户6757049885026 分钟前
Python 统一大业:uv 如何整合 Pip、Pyenv 和 Venv?
后端·python
basketball61611 分钟前
C++ iostream 完全指南:从 cin/cout 到流式编程的奥秘
开发语言·c++
SilentSamsara17 分钟前
运算符重载:让自定义对象支持 +、[]、in 操作
开发语言·python·算法·青少年编程·pycharm
wuxinyan12318 分钟前
工业级大模型学习之路020:LangChain零基础入门教程(第三篇):提示词工程与提示模板系统
人工智能·python·学习·langchain
threelab18 分钟前
Three.js 3D 热力图效果 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
U盘失踪了31 分钟前
【笔记】pycharm 安装Jupyter失败
笔记·python
Royzst34 分钟前
图书管理案例
java·开发语言
我的世界洛天依36 分钟前
胡桃讲编程 | 外挂的另一种方法与防御 —— 对象(JS ES262)
开发语言·javascript·ecmascript
langzaibeijing1 小时前
AI应用哪家性价比高
大数据·人工智能·python