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

效果图:

目录
相关文章
|
5天前
|
Python
python函数进阶
python函数进阶
|
5天前
|
安全 Python
Python量化炒股的获取数据函数—get_industry()
Python量化炒股的获取数据函数—get_industry()
13 3
|
5天前
|
Python
Python sorted() 函数和sort()函数对比分析
Python sorted() 函数和sort()函数对比分析
|
5天前
|
Python
Python量化炒股的获取数据函数—get_security_info()
Python量化炒股的获取数据函数—get_security_info()
12 1
|
9天前
|
Python
python标识符 | 10
python标识符 | 10
|
8天前
|
数据库 开发者 Python
实战指南:用Python协程与异步函数优化高性能Web应用
在快速发展的Web开发领域,高性能与高效响应是衡量应用质量的重要标准。随着Python在Web开发中的广泛应用,如何利用Python的协程(Coroutine)与异步函数(Async Functions)特性来优化Web应用的性能,成为了许多开发者关注的焦点。本文将从实战角度出发,通过具体案例展示如何运用这些技术来提升Web应用的响应速度和吞吐量。
12 1
|
8天前
|
调度 Python
揭秘Python并发编程核心:深入理解协程与异步函数的工作原理
在Python异步编程领域,协程与异步函数成为处理并发任务的关键工具。协程(微线程)比操作系统线程更轻量级,通过`async def`定义并在遇到`await`表达式时暂停执行。异步函数利用`await`实现任务间的切换。事件循环作为异步编程的核心,负责调度任务;`asyncio`库提供了事件循环的管理。Future对象则优雅地处理异步结果。掌握这些概念,可使代码更高效、简洁且易于维护。
11 1
|
12天前
|
Python
[oeasy]python035_根据序号得到字符_chr函数_字符_character_
本文介绍了Python中的`ord()`和`chr()`函数。`ord()`函数通过字符找到对应的序号,而`chr()`函数则根据序号找到对应的字符。两者互为逆运算,可以相互转换。文章还探讨了单双引号在字符串中的作用,并解释了中文字符和emoji也有对应的序号。最后总结了`ord()`和`chr()`函数的特点,并提供了学习资源链接。
17 4
|
15天前
|
Java Python
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
【9月更文挑战第18天】在 Python 中,虽无明确的 `interface` 关键字,但可通过约定实现类似功能。接口主要规定了需实现的方法,不提供具体实现。抽象基类(ABC)则通过 `@abstractmethod` 装饰器定义抽象方法,子类必须实现这些方法。使用抽象基类可使继承结构更清晰、规范,并确保子类遵循指定的方法实现。然而,其使用应根据实际需求决定,避免过度设计导致代码复杂。
|
18天前
|
Python
全网最适合入门的面向对象编程教程:Python函数方法与接口-函数与方法的区别和lamda匿名函数
【9月更文挑战第15天】在 Python 中,函数与方法有所区别:函数是独立的代码块,可通过函数名直接调用,不依赖特定类或对象;方法则是与类或对象关联的函数,通常在类内部定义并通过对象调用。Lambda 函数是一种简洁的匿名函数定义方式,常用于简单的操作或作为其他函数的参数。根据需求,可选择使用函数、方法或 lambda 函数来实现代码逻辑。
下一篇
无影云桌面