首页 / PYTHON / Python之变量--初识练习
Python之变量--初识练习
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python之变量--初识练习,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3357字,纯文字阅读大概需要5分钟。
内容图文
![Python之变量--初识练习](/upload/InfoBanner/zyjiaocheng/789/fdde51adbbd8477daa424a0854bb7a62.jpg)
变量命名:见名知意
由字母、数字、下划线组成
不能以数字开头
不能和关键字重名
驼峰命名法:
1.大驼峰:FirstName
2.小驼峰:firstName
输入输出练习
In [1]: 1 ##无定义,输入等于输出
Out[1]: 1
In [2]: 2
Out[2]: 2
In [3]: type(1) ##定义输出1的类型
Out[3]: int ##整数型
In [4]: type(2)
Out[4]: int
In [5]: type(111111111111111111111111111111111111)
Out[5]: int
In [6]: type(-1)
Out[6]: int
In [7]: type(1.1) ## 定义输出1.1的类型
Out[7]: float ##浮点数
In [8]: type(1.111111111111111111111111)
Out[8]: float
#python2.x
Type "help", "copyright", "credits" or "license" for more information.
>>> input('Num:')
Num:a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'a' is not defined
>>> input('Num:')
Num:2
2
>>> raw_input('Num:')
Num:a
'a'
>>> raw_input('Num:')
Num:1
'1'
>>>
#python3.x
>>> input('Num:)
File "<stdin>", line 1
input('Num:)
^
SyntaxError: EOL while scanning string literal
>>> input('Num:')
Num:a
'a'
>>> input('Num:')
Num:2
'2'
>>> num = input()
12
>>> num
'12'
>>> num > 19
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'
>>> int(num) > 19
False
>>> int(num) < 19
True
>>>
>>> name = 'li'
>>> age = '20'
>>> print('%s的年龄是%d岁' %(name,age))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str
>>> name = 'li'
>>> age = 20
>>> print('%s的年龄是%d岁' %(name,age))
li的年龄是20岁
>>> ship = 168.999999
>>> print('%s的奖学金是%f' %(name,ship))
li的奖学金是168.999999
>>> ship=700
>>> print('%s本月的奖学金为%f' %(name,ship))
File "<stdin>", line 1
print('%s本月的奖学金为%f' %(name,ship))
^
IndentationError: unexpected indent
>>> print('%s本月的奖学金为%f' %(name,ship))
li本月的奖学金为700.000000
>>> print('%s本月的奖学金为%2f' %(name,ship))
li本月的奖学金为700.000000
>>> print('%s本月的奖学金为%.2f' %(name,ship))
li本月的奖学金为700.00
>>> print('%s本月的奖学金为%.3f' %(name,ship))
li本月的奖学金为700.000
>>>
>>> studentid=1
>>> studentid=9
File "<stdin>", line 1
studentid=9
^
IndentationError: unexpected indent
>>> studentid=9
>>> print('%s的学号为130%d' %(name,studentid))
li的学号为1309
>>> print('%s的学号为130%.3d' %(name,studentid))
li的学号为130009
>>> scale = 0.2
>>> print('比例是 %.2f' %scale)
比例是 0.20
>>> print('比例是 %.2f' %(scale * 100))
比例是 20.00
>>> print('比例是 %.2f%%' %(scale * 100))
比例是 20.00%
>>>
#
#- 输入学生学号;
#- 依次输入学生的三门科目成绩;
#- 计算该学生的平均成绩, 并打印;
#- 平均成绩保留两位小数点;
#- 计算该学生语文成绩占总成绩的百分之多少?并打印。
ID = input("学生ID:")
Chinese = float(input("语文成绩:"))
Math = float(input("数学成绩:"))
English = float(input("英语成绩:"))
#总成绩
SumScore = Chinese + Math + English
#平均成绩
AvgScore = SumScore / 3
#百分比
ChinesePercent = ( Chinese / SumScore ) * 100
print("%s的平均成绩为%.2f" %(ID,AvgScore))
print("语文成绩占总成绩的%.2f%%" %ChinesePercent)
内容总结
以上是互联网集市为您收集整理的Python之变量--初识练习全部内容,希望文章能够帮你解决Python之变量--初识练习所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。