Python基础(变量、数据类型、输入与输出、格式化输出、算术运算)

1. Python简介

  • Python意为蟒蛇,创始人是吉多.范罗苏姆;Python语言是以C为基础的,用C语言来实现,并能够调用C语言的库文件,可扩展性较强。
  • 计算机在执行程序时,需要编译器来将其他语言翻译成计算机可以识别的机器语言。编译器翻译的方式有两种:(1)编译(2)解释
  • (1)编译型语言: 使用编译器的语言叫编译型语言。工作模式为 统一编译,一次执行,效率较高但跨平台性较差;使用某一编译器最终生成的文件只能在该操作系统上运行,不能跨平台运行。
  • (2)解释型语言:使用解释器的语言叫解释型语言。工作模式为 读一行翻译一行,翻译一行执行一行;效率较低但跨平台性高。只要在不同的操作系统上安装不同的解释器就可以应用解释型程序。

2. Python2与Python3的区别

Python书写需要注意的语法:

1)代码后不用以分号结尾(编码规范 PEP8)
2)严格按照缩进的语言

(1)使用编码方式不同

  • python2.x:默认使用ASCII编码
  • python3.x:默认使用UTF-8编码
  • 在使用时需要在开头添加:_*_coding:utf-8_*_ ,以便不同版本的python翻译器翻译

(2)input输入输出不同(详见下文)

  • python2 中input()只支持正确的数值类型;输入什么类型的数据,就是什么类型变量
    raw_input:支持任意类型
  • python3 中支持任意类型;不论你输入什么类型的数据,最后都会被定义为字符串类型变量

(3)运算取结果不同 (详见下文)

  • Python2中整型的除法的结果如果为浮点型,会直接取整;如果想要浮点类型的结果,需要将除数或被除数写为浮点类型
  • Python3中整数的除法如果结果为浮点型不会直接取整

3. Python中的变量

(1)与c、java、shell相似,变量就是对内存地址空间的一个引用

a = 1

(2)变量的类型:整型、浮点型、字符串型、bool型
整型:

>>> a = 1
>>> print(a)
1

浮点型:

>>> b = 1.2
>>> print(b)
1.2
>>> type(b)
<class 'float'>
>>> c = westos
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'westos' is not defined

字符串型:

>>> c = 'westos'
>>> print(c)
westos
>>> c = "what's"
>>> print(c)
what's
>>> c = 'what's'
  File "<stdin>", line 1
    c = 'what's'
              ^
SyntaxError: invalid syntax
>>> c = 'what\'s'
>>> print(c)
what's

bool型(只有两个值:True False 非0即真):

>>> a = 1
>>> bool(a)
True
>>> bool(0)
False
>>> bool('')
False
>>> bool(' ')
True
>>> bool('redhat')
True

在这里插入图片描述

(3)变量类型的查看

type(a)查看变量的类型

>>> type(a)
<class 'int'>

在这里插入图片描述
(4)变量类型的转换

整型转换int(a)
浮点类型转换float(a)
字符串类型转换str(a)

>>> a = 1
>>> type(a)
<class 'int'>
>>> float(a)
1.0
>>> type(a)
<class 'int'>
>>> b = float(a)
>>> b
1.0
>>> b = 2.0
>>> int(b)
2
>>> c = 'redhat'
>>> int(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'redhat'
>>> b = 123
>>> str(b)
'123'
>>> c = '123'
>>> int(c)
123
>>> a
1
>>> b
123
>>> c
'123'

在这里插入图片描述
如果接收到的数值要进行比较的时候,一定要转换为同一种类型

 >>> age = input('age:')
 age:19
 >>> age
 '19'
 >>> age > 18
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 TypeError: '>' not supported between instances of 'str' and 'int'
 >>> age = int(age)
 >>> age
 19
 >>> age > 18
 True

(5)在内存中删除一个变量

使用关键字del删除:del a

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

在这里插入图片描述

4. Python的输入与输出

(1)python3.x
input():接收任意数据类型
python3.x中没有raw_input()

 >>> input('Num:')
 Num:2
 '2'
 >>> input('Num:')
 Num:abc
 'abc'

在这里插入图片描述
(2)python2.x
input()只支持正确的数值类型;输入什么类型的数据,就是什么类型变量

 >>> input('Num:')
 Num:2
 2
 >>> input('Num:')
 Num:1.2
 1.2
 >>> input('Num:')
 Num:redhat
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "<string>", line 1, in <module>
 NameError: name 'redhat' is not defined
 >>> raw_input('Num:')
 Num:2
 '2'
 >>> raw_input('Num:')
 Num:1,2
 '1,2'
 >>> raw_input('Num:')
 Num:redhat
 'redhat'
 >>> input('Passwd:')
 Passwd:123
 '123'

在这里插入图片描述
在这里插入图片描述
(3)输入内容不回显

 >>> import getpass
 >>> num = getpass.getpass('请输入密码:')
 请输入密码:
 >>> print(num)
 123

在这里插入图片描述
(4)变量类型的转换与输入接收

int(input())整型转换接收

 >>> age = int(input('age:'))
 age:18
 >>> age
 18
 deployment.extensions/httpd rolled back

在这里插入图片描述

5. Python格式化输出

  • 格式化输出是指使用占位符来表示变量的输出方式;使输出更具灵活性和便利性

(1)%s:字符串型

 >>> name = 'westos'
 >>> name
 'westos'
 >>> age = 12
 >>> print('%s的年龄是%d' %(name,age))
 westos的年龄是12
 >>> age = 18
 >>> print('%s的年龄是%d' %(name,age))
 westos的年龄是18
 >>> age = '19'
 >>> 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
 >>> age = 19.5
 >>> print('%s的年龄是%d' %(name,age))
 westos的年龄是19

(2)%d 整数的占位:不够的位数 前面补0

 >>> sid = 1
 >>> name = 'lily'
 >>> print('%s的学号为000%d' %(name,sid))
 lily的学号为0001
 >>> sid = 2
 >>> print('%s的学号为000%d' %(name,sid))
 lily的学号为0002
 >>> sid = 10
 >>> print('%s的学号为000%d' %(name,sid))
 lily的学号为00010
 >>> print('%s的学号为%.5d' %(name,sid))
 lily的学号为00010
 >>> sid = 1
 >>> print('%s的学号为%.5d' %(name,sid))
 lily的学号为00001
 >>> sid = 20
 >>> sid = 100
 >>> print('%s的学号为%.5d' %(name,sid))
 lily的学号为00100

(3)%f:浮点型

1)浮点型默认小数点后保留6位小数

 >>> money=23121312.32314432
 >>> name = 'Tom'
 >>> print('%s的工资为%f' %(name,money))
 Tom的工资为23121312.323144
 >>> money=60000
 >>> print('%s的工资为%f' %(name,money))
 Tom的工资为60000.000000
 >>> money=60000.99
 >>> print('%s的工资为%f' %(name,money))
 Tom的工资为60000.990000

2)小数点后保留位数的设定

 >>> print('%s的工资为%.2f' %(name,money))
 Tom的工资为60000.99
 >>> money=60000
 >>> print('%s的工资为%.3f' %(name,money))
 Tom的工资为60000.000

(4)百分号的实现 %%

 >>> scale = 0.1
 >>> print('数据的比例是:%.2f' %(scale))
 数据的比例是:0.10
 >>> print('数据的比例是:%.2f' %(scale * 100))
 数据的比例是:10.00
 >>> print('数据的比例是:%.2f%' %(scale * 100))
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 ValueError: incomplete format
 >>> print('数据的比例是:%.2f%%' %(scale * 100))
 数据的比例是:10.00%

6. Python中的算术运算

(1)除 /
python2.x:

>>> 5/2
2
>>> 100/300
0
>>> 5.0/2
2.5
>>> 100/300.0
0.3333333333333333

在这里插入图片描述
python3.x:

>>> 5/2
2.5
>>> 100/300
0.3333333333333333
>>> 

在这里插入图片描述
(2)取余 %

>>> 5%2
1

(3)取整 //

>>> 5//2
2

>>> a = 1
>>> a = a+1
>>> a
2
>>> a += 1
>>> a
3

7. 练习

要求
#求平均成绩(python3解释器)
#- 输入学生姓名;
#- 依次输入学生的三门科目成绩;(语文 数学 英语)
#- 计算该学生的平均成绩, 并打印;
#- 平均成绩保留一位小数点;
#- 计算该学生语文成绩占总成绩的百分之多少?并打印。eg: 78%;

name = input("请输入你的姓名: ")
num1=int(input("请输入你的语文成绩: "))
num2=int(input("请输入你的数学成绩: "))
num3=int(input("请输入你的英语成绩: "))
num4 = num1+num2+num3
num=num4/3
yuwen=num1/num4
print("%s的平均成绩为:%.1f;语文占总成绩的%.1f%%" %(name,num,yuwen * 100))
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值