Python入门之变量的定义、类型以及系统的输入输出

1 Python2和Python3的一些区别

在介绍内容之前,我们先简单介绍一下python2和python3的区别:
1.1 python2和python3的区别1:

python2不用括号 print 'hello world' 
识别不了中文 需要声明 编码格式 _*_coding:utf-8_*_
python3使用括号,识别汉字print('你好')
 不用声明 python3.6 ly.py 

1.2 python2和python3的区别2:

python2里面有长整型概念 python3没有 都是整型
python3不分单精度和双精度之分 都是float

在这里插入图片描述
1.3 python2和python3的区别3:

python2:input('Num:') 函数只支持数字 不支持字符串
     raw_input('Num:')  只接收字符串

python3:input('Num:')  没有raw_input ,默认接受类型为字符串

1.4 python2和python3的区别4:

python3两数相除,除出来是浮点数, 2//2 是整数
python2是整数

在这里插入图片描述

2 变量的定义及类型

2.1 变量命名的方式:

变量命令的方式:见名知意
由字母数字下划线组成 不以数字开头 尽量不和关键字重名
驼峰命名法:
1.大驼峰:FirstName
2.小驼峰:firstName

2.2 变量的类型:
2.2.1 整型(int):

In [1]: age=18                                                                  

In [2]: age                                                                     
Out[2]: 18

In [3]: type(age)                                                               
Out[3]: int

2.2.2 浮点型(float):

In [4]: type(1.1)                                                               
Out[4]: float

In [5]: type(1.111111)                                                          
Out[5]: float

2.2.3 bool型 (true false):

python中true的类型很广,只要不是0和空 都是真
In [6]: a='hello'                                                               

In [7]: bool(a)                                                                 
Out[7]: True

In [8]: bool(0)                                                                 
Out[8]: False

In [9]: b=''                                                                    

In [10]: bool(b)                                                                
Out[10]: False

2.2.4 字符串:

单引号和双引号没有区别,必须成对出现。
""" 
表示多行注释
"""
In [11]: 'hello world'                                                          
Out[11]: 'hello world'

In [12]: "hello world"                                                          
Out[12]: 'hello world'

In [13]: 'let's go'                                                             
  File "<ipython-input-13-58d8c8a129df>", line 1
    'let's go'
         ^
SyntaxError: invalid syntax


In [14]: "let's go"                                                             
Out[14]: "let's go"

In [15]: 'let\'s go'                                                            
Out[15]: "let's go"

In [16]: """               
    ...: hello world 
    ...: hello world 
    ...: """                                                                    
Out[16]: '\nhello world\nhello world\n'

In [17]: 'hello \ 
    ...: world'                                                                 
Out[17]: 'hello world'

In [18]: 'hello \n world'                                                       
Out[18]: 'hello \n world'

In [19]: print('\nhello world\nhello world\n')                                  

hello world
hello world


In [20]: print('hello \n world')                                                
hello 
 world

In [21]: print('c:\natasha\test')                                               
c:
atasha	est

In [22]: print('c:\\natasha\\test')                                             
c:\natasha\test

In [23]: print(r'c:\natasha\test')                                              
c:\natasha\test

2.2.5 类型转化:

>>> a = 1
>>> float(a)
1.0
>>> b = 2.3
>>> int(b)
2
>>> float(b)
2.3
>>> str(b)
'2.3'
>>> str = 'westos'
>>> float(str)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'westos'

2.2.6 删除内存中的变量:

>>> a = 1.2
>>> a
1.2
>>> del a
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

备注:第一次出现为定义变量,以后都是使用变量

3 系统的输入输出

3.1 系统的输入:

[root@foundation72 ~]# python3.6
Python 3.6.6 (default, Jun 26 2019, 00:26:02) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> num=input()            ##input用于接收用户输入
12
>>> num                    ##输出num
'12'                       ##注意:会将所有的接收当作字符串处理
>>> num=int(input())       ##用户输入时进行类型转换
34
>>> num
34
>>> 

3.2 系统的格式化输出:

%s 字符串
%d 整型
%.3d == %03d
%f浮点数
%.2f 保留两位小数
%%: 输出%

In [1]: a = 1                                                                   

In [2]: print('%.3d' %a)                                                        
001

In [3]: print('%03d' %a)                                                        
001


In [1]: name = 'yan'                                                            

In [2]: money = 18888.888                                                        

In [3]: month = 8                                                               

In [4]: print('%s在第%d个月的工资为%.2f' %(name,month,money))                   
yan在第8个月的工资为18888.89

定义一个小数scale 输出:数据比例是10.00%
In [5]: scale = 0.1                                                             
In [6]: print('数据比例是%.2f%%' %(scale * 100))                                
数据比例是10.00%

3.3 系统的格式化输出方法之一:居中

>>> a = 'hello'
>>> a.center(40)
'                 hello                  '
>>> a.center(40,'*')
'*****************hello******************'
>>> print("学生管理系统".center(50,'-'))
----------------------学生管理系统----------------------
>>> print("学生管理系统".center(50,'*'))
**********************学生管理系统**********************
注意: 建议使用ipython,或者pycharm

3.4 系统的格式化输出练习:
要求:
输入学生学号
依次输入学生的三门科目成绩
计算该学生的平均成绩,并打印
平均成绩保留两位小数点
计算该学生语文成绩占总成绩的百分之多少?并打印

ID= input("学生ID:")
Chinese= float(input("语文成绩:"))
English= float(input("英语成绩:"))
Math= float(input("数学成绩:"))

#总成绩
SumScore= Chinese + English + Math

#平均成绩
AvgScore= SumScore / 3

#百分比
ChinesePercent = ( Chinese / SumScore ) * 100

print("%s的平均成绩为%.2f" %(ID,AvgScore))
print("语文成绩占总成绩的%.2f%%" %ChinesePercent)

在这里插入图片描述
#################################The End#########################

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值