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

效果图:

目录
相关文章
|
4月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
343 1
|
4月前
|
算法 Java Docker
(Python基础)新时代语言!一起学习Python吧!(三):IF条件判断和match匹配;Python中的循环:for...in、while循环;循环操作关键字;Python函数使用方法
IF 条件判断 使用if语句,对条件进行判断 true则执行代码块缩进语句 false则不执行代码块缩进语句,如果有else 或 elif 则进入相应的规则中执行
627 1
|
4月前
|
Java 数据处理 索引
(numpy)Python做数据处理必备框架!(二):ndarray切片的使用与运算;常见的ndarray函数:平方根、正余弦、自然对数、指数、幂等运算;统计函数:方差、均值、极差;比较函数...
ndarray切片 索引从0开始 索引/切片类型 描述/用法 基本索引 通过整数索引直接访问元素。 行/列切片 使用冒号:切片语法选择行或列的子集 连续切片 从起始索引到结束索引按步长切片 使用slice函数 通过slice(start,stop,strp)定义切片规则 布尔索引 通过布尔条件筛选满足条件的元素。支持逻辑运算符 &、|。
293 0
|
5月前
|
设计模式 缓存 监控
Python装饰器:优雅增强函数功能
Python装饰器:优雅增强函数功能
316 101
|
5月前
|
缓存 测试技术 Python
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
258 99
|
5月前
|
存储 缓存 测试技术
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
291 98
|
5月前
|
缓存 Python
Python中的装饰器:优雅地增强函数功能
Python中的装饰器:优雅地增强函数功能
|
6月前
|
Python
Python 函数定义
Python 函数定义
688 155
|
7月前
|
PHP Python
Python format()函数高级字符串格式化详解
在 Python 中,字符串格式化是一个重要的主题,format() 函数作为一种灵活且强大的字符串格式化方法,被广泛应用。format() 函数不仅能实现基本的插入变量,还支持更多高级的格式化功能,包括数字格式、对齐、填充、日期时间格式、嵌套字段等。 今天我们将深入解析 format() 函数的高级用法,帮助你在实际编程中更高效地处理字符串格式化。
677 0
|
7月前
|
编解码 数据安全/隐私保护 Python
抖音批量发布视频工具,自动上传视频作品笔记,python发布软件
这个抖音批量发布工具包含三个主要模块:主上传程序、配置文件和视频预处理工具。主程序

推荐镜像

更多