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

发布于:2023-01-28 ⋅ 阅读:(575) ⋅ 点赞:(0)

哈喽,大家好!今天来学习Python中的标识符和输入输出函数。

目录

一、标识符

二、输入函数

三、输出函数

四、print( )格式化输出

五、练习题


一、标识符

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

变量的标识符:  变量名,方法名(程序员自己取的名字)

命名规范:
        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:格式化输出
#快捷键: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()

效果图:

<end>

好啦,今天的内容结束啦!咱们下期见!

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到