Python基础 笔记(三) 标识符、输入输出函数

简介: Python基础 笔记(三) 标识符、输入输出函数

一、标识符

标识符:开发人员在程序中自定义的名称,如 类名 函数名 变量名等

变量的标识符:  变量名,方法名(程序员自己取的名字)
 
命名规范:
        1.由英文字母(大小写)、下划线、数字组成
        2.不能由数字开头
        3.不能和关键字相同
 
常用的三种命名方式:
        1.每个单词的首字母全部大写      MaxValue
        2.从第二个单词开始,首字母大写  maxValue
        3.单词全小写中间由下划线相连   max_value
 
 
# 用英语命名 较为规范
 
        max_value = 123  #最大值
        critical_Value = 123  #临界值

二、输入函数

Python中的输出函数: input()函数

#1:请从键盘输入一个字符串赋值给一个变量
username = input("请输入用户名:")
password = input("请输入密码:")
print(username)
print(password)
 
#所有通过input输入的数据都会当成字符串来处理 有时需要强转
age = input("请输入你的年龄:")
print(type(age))
print("你的年龄是%s" %age)
print(f"你的年龄是{age}")
print(f"十年后,我的年龄是{int(age)+10}") #将age强转为int类型

提醒:


1.  input()的小括号中放入的是,提示信息,用来在获取数据之前给用户的一个简单提示


2.  input()在从键盘获取了数据以后,会存放到等号右边的变量中


3.  input()会把用户输入的任何值都作为字符串来对待


三、输出函数

Python中的输出函数: print()函数

生活中的输出:

程序中的输出:

#1:默认方式,默认情况下 print()输出后会默认换行
a = 123
print(a)
 
print(123)
print('hello')
 
print('中国')
print('中\n国')
print('中\\n国')
# 两个反斜线可以合成一个反斜线 双引号 三引号 对\n无作用
 
 
#2:指定结束符 end更改默认换行方式
#  print('中国') 等价于 print('中国',end='\n')
print('我',end = '')
print('爱你',end = '#')
print('中国',end = '\n')

四、print( )格式化输出

1. #1:格式化输出
#1:格式化输出
#快捷键:ctrl + d 复制   ctrl + x 删除
 
print('我今年22岁!');
 
age = 22
print('我今年%d岁!' %age);
 
age = 22
num = 10
print('我今年%d岁,比我弟弟大%d岁!' %(age,num));
 
#2:常用的格式符号: %s(字符串)  %d(整型)  %f(浮点型)
name = '木易巷'
age = 22
score = 99.9
 
print('我叫%s' %name)
print('我叫' + name)
print('我叫木易巷,我今年22岁,我考了99.9分')
print('我叫%s,我今年%d岁,我考了%f分' %(name,age,score))
# 另一种写法 加f
print(f'我叫{name},我今年{age}岁,我考了{score}分')
 
 
# 3. 格式化(format)输出, 这个是Python3.X的特性.
salary = 1
sid = 6
 
print(f'我叫{name}, 我的年龄是{age}, 我的工资是{salary}, 我的学号是{sid}')
 
# 格式化输出并保留小数位.````
print(f'我叫{name}, 我的年龄是{age}, 我的工资是{round(salary, 2)}, 我的学号是{sid}')
print('-----------------------')
 
#round函数可以实现保留几位小数,并且四舍五入
a = 3.1415926
print(round(a,3))
print(f'圆周率是{a}')

常用的格式符号:

格式符号

转化

%s

字符串(string)

%d

整形(int)

%f

浮点型(float)

五、练习题

1. 定义字符串变量 name ,输出 我的名字叫 ⼩明,请多多关照!


2. 定义整数变量 student_no ,输出 我的学号是 000001


3. 定义⼩数 price、weight、money,输出苹果单价 9.00 元/⽄,购买了 5.00 ⽄,需要⽀付 45.00 元


4. 定义⼀个⼩数 scale ,输出 数据⽐例是 10.00%


有空可以练习一下哦~


今日事,今日毕,有问题及时解决,加油!


福利:


咱们来使用Python画一个小猪佩奇~

话不多说,直接上代码!

import turtle as t
t.pensize(4)
t.hideturtle()
t.colormode(255)
t.color((255, 155, 192), "pink")
t.setup(840, 500)
t.speed(20)
# 鼻子
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
t.begin_fill()
a = 0.4
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a + 0.08
        t.lt(3)  # 向左转3度
        t.fd(a)  # 向前走a的步长
    else:
        a = a - 0.08
        t.lt(3)
        t.fd(a)
t.end_fill()
t.pu()
t.seth(90)
t.fd(25)
t.seth(0)
t.fd(10)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()
t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()
# 头
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300, -30)
t.circle(100, -60)
t.circle(80, -100)
t.circle(150, -20)
t.circle(60, -95)
t.seth(161)
t.circle(-300, 15)
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
a = 0.4
for i in range(60):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a + 0.08
        t.lt(3)  # 向左转3度
        t.fd(a)  # 向前走a的步长
    else:
        a = a - 0.08
        t.lt(3)
        t.fd(a)
t.end_fill()
# 耳朵
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 54)
t.end_fill()
t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 56)
t.end_fill()
# 眼睛
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
# 腮
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()
# 嘴
t.color(239, 69, 19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30, 40)
t.circle(40, 80)
# 身体
t.color("red", (255, 99, 71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100, 10)
t.circle(300, 30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300, 30)
t.circle(100, 3)
t.color((255, 155, 192), (255, 100, 100))
t.seth(-135)
t.circle(-80, 63)
t.circle(-150, 24)
t.end_fill()
# 手
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300, 15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20, 90)
t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300, 15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20, 90)
# 脚
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
# 尾巴
t.pensize(4)
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70, 20)
t.circle(10, 330)
t.circle(70, 30)
t.exitonclick()

效果图:

目录
相关文章
|
1天前
|
开发者 Python
Python入门:8.Python中的函数
### 引言 在编写程序时,函数是一种强大的工具。它们可以将代码逻辑模块化,减少重复代码的编写,并提高程序的可读性和可维护性。无论是初学者还是资深开发者,深入理解函数的使用和设计都是编写高质量代码的基础。本文将从基础概念开始,逐步讲解 Python 中的函数及其高级特性。
Python入门:8.Python中的函数
|
1月前
|
存储 人工智能 Python
[oeasy]python061_如何接收输入_input函数_字符串_str_容器_ 输入输出
本文介绍了Python中如何使用`input()`函数接收用户输入。`input()`函数可以从标准输入流获取字符串,并将其赋值给变量。通过键盘输入的值可以实时赋予变量,实现动态输入。为了更好地理解其用法,文中通过实例演示了如何接收用户输入并存储在变量中,还介绍了`input()`函数的参数`prompt`,用于提供输入提示信息。最后总结了`input()`函数的核心功能及其应用场景。更多内容可参考蓝桥、GitHub和Gitee上的相关教程。
16 0
|
1月前
|
Python
[oeasy]python057_如何删除print函数_dunder_builtins_系统内建模块
本文介绍了如何删除Python中的`print`函数,并探讨了系统内建模块`__builtins__`的作用。主要内容包括: 1. **回忆上次内容**:上次提到使用下划线避免命名冲突。 2. **双下划线变量**:解释了双下划线(如`__name__`、`__doc__`、`__builtins__`)是系统定义的标识符,具有特殊含义。
32 3
|
1月前
|
JSON 监控 安全
深入理解 Python 的 eval() 函数与空全局字典 {}
`eval()` 函数在 Python 中能将字符串解析为代码并执行,但伴随安全风险,尤其在处理不受信任的输入时。传递空全局字典 {} 可限制其访问内置对象,但仍存隐患。建议通过限制函数和变量、使用沙箱环境、避免复杂表达式、验证输入等提高安全性。更推荐使用 `ast.literal_eval()`、自定义解析器或 JSON 解析等替代方案,以确保代码安全性和可靠性。
45 2
|
2月前
|
数据可视化 DataX Python
Seaborn 教程-绘图函数
Seaborn 教程-绘图函数
87 8
|
2月前
|
人工智能 C语言 Python
[oeasy]python051_什么样的变量名能用_标识符_identifier
本文介绍了Python中变量名的命名规则,强调标识符(identifier)必须以字母或下划线开始,后续可包含字母、下划线及数字。通过`isidentifier()`函数可验证字符串是否为合法标识符。文中还探讨了为何数字不能作为标识符的开头,并提供了相关练习与解答,最后提及这些规则源自C语言的影响。
127 69
|
2月前
|
Python
Python中的函数是**一种命名的代码块,用于执行特定任务或计算
Python中的函数是**一种命名的代码块,用于执行特定任务或计算
64 18
|
2月前
|
Python
Python中的函数
Python中的函数
62 8
|
3月前
|
搜索推荐 Python
利用Python内置函数实现的冒泡排序算法
在上述代码中,`bubble_sort` 函数接受一个列表 `arr` 作为输入。通过两层循环,外层循环控制排序的轮数,内层循环用于比较相邻的元素并进行交换。如果前一个元素大于后一个元素,就将它们交换位置。
155 67
|
3月前
|
监控 测试技术 数据库
Python中的装饰器:解锁函数增强的魔法####
本文深入探讨了Python语言中一个既强大又灵活的特性——装饰器(Decorator),它以一种优雅的方式实现了函数功能的扩展与增强。不同于传统的代码复用机制,装饰器通过高阶函数的形式,为开发者提供了在不修改原函数源代码的前提下,动态添加新功能的能力。我们将从装饰器的基本概念入手,逐步解析其工作原理,并通过一系列实例展示如何利用装饰器进行日志记录、性能测试、事务处理等常见任务,最终揭示装饰器在提升代码可读性、维护性和功能性方面的独特价值。 ####

热门文章

最新文章

推荐镜像

更多