Python 编程——Python 基础

一、了解 Python

二、基础知识

1.规范

01.没有分号(规范编码)
02.严格按照缩进的语言
03.python2不支持中文
解决办法:
# _*_coding:utf-8_*_
04.注释:

"""
块注释
qq_passwd=45678
print(qq_passwd)

"""

2.变量

什么是变量?
a=1
1存在于内存开辟出来的一块地址上,当调用时,a会引用内存地址里的1

[root@localhost ~]# vim file.py
qq_num = 1234
a = qq_num
print(a)
print(qq_num)
[root@localhost ~]# python3 file.py
[root@localhost ~]# python3 file.py
1234
1234

3.数据类型

01.整型 int

>>> a = 1  
>>> print(a)
1
>>> type(a)      ##type()查看变量的数据类型 
<class 'int'>

02.浮点型 float

>>> b = 1.2
>>> print(b)
1.2
>>> type(b)
<class 'float'>

03.字符串型 str

>>> c = westos                  ##字符串要用单引号引用
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'westos' is not defined
>>> c = 'westos'
>>> print(c)
westos
>>> type(c)
<class 'str'>

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

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

4.数据类型的转换

注意:只是对当前行的翻译进行数据类型的转换

整型到浮点型的转换
>>> a = 1
>>> type(a)
<class 'int'>
>>> float(a)
1.0
>>> type(a)
<class 'int'>
字符串到整型的转换
>>> c = '123'
>>> int(c)
123
>>> b = 345
>>> str(b)
'345'
>>> e = 12.6
>>> str(e)
'12.6'
>>> c = 'linux'        ##不可以转换
>>> int(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'linux'

5.内存中变量的删除

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

6.输入输出

01.输入

在 python2.x 里:

-input:只支持数值类型
-raw_input:支持数值类型和字符串类型

[root@localhost ~]# python
Python 2.7.5 (default, Feb 11 2014, 07:46:25) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> input('Num:')
Num:123
123
>>> input('Num:')
Num:abc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'abc' is not defined
>>> raw_input('Num:')
Num:123
'123'
>>> raw_input('Num:')
Num:abc
'abc'

希望输入密码不回显

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

在 python3.x 里:

没有raw_input()
input():接收任意数据类型

[root@localhost ~]# python3
Python 3.6.4 (default, Mar 12 2019, 16:31:36) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> num = input('Num:')
Num:123
>>> num = input('Num:')
Num:redhat
>>> num
'redhat'
>>> num = raw_input('Num:')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined

02.输出

格式化输出
%s:代表字符串型
%d:整型

>>> name = 'westos'
>>> age = 11
>>> print('%s的年龄为%d' %(name,age))
westos的年龄为11
>>> name = 'redhat'
>>> print('%s的年龄为%d' %(name,age))
redhat的年龄为11
>>> 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
>>> age = 20
>>> name = 123
>>> print('%s的年龄为%d' %(name,age))
123的年龄为20

%f: 代表浮点型
%.xf(x=2 3 4 5) 保留小数点后多少位

>>> money = 6666.666666
>>> name = 'tom'
>>> print('%s本月的工资为%f' %(name,money))
tom本月的工资为6666.666666
>>> print('%s本月的工资为%.2f' %(name,money))
tom本月的工资为6666.67
>>> print('%s本月的工资为%.3f' %(name,money))
tom本月的工资为6666.667
>>> print('%s本月的工资为%.1f' %(name,money))
tom本月的工资为6666.7

整型总占位,不够位数的前面补0

>>> sid = 1
>>> name = 'yang'
>>> print('%s的学号为%d' %(name,sid))
yang的学号为1
>>> print('%s的学号为130%d' %(name,sid))
yang的学号为1301
>>> print('%s的学号为%0.5d' %(name,sid))
yang的学号为00001
>>> print('%s的学号为%.5d' %(name,sid))
yang的学号为00001
>>> sid = 123
>>> print('%s的学号为%.5d' %(name,sid))
yang的学号为00123

百分数的实现

>>> scale = 0.1
>>> print('数据的比例是:%.2f' %(scale * 100))
数据的比例是:10.00
>>> print('数据的比例是:%.2f%%' %(scale * 100))
数据的比例是:10.00%

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

[root@localhost ~]# vim test.py
Name = input('请输入学生姓名:')
Chinese = float(input('请输入语文成绩:'))
Math = float(input('请输入数学成绩:'))
English = float(input('请输入英文成绩:'))
Num = Chinese+Math+English
print('%s的平均成绩为:%.1f' %(Name,Num/3))
print('%s语文成绩占总成绩的%.1f%%' %(Name,Chinese/Num*100))
[root@localhost ~]# python3 test.py 
请输入学生姓名:yang
请输入语文成绩:100
请输入数学成绩:60
请输入英文成绩:90
yang的平均成绩为:83.3
yang语文成绩占总成绩的40.0%

7.数值的比较

注意:如果接收到的数值要进行比较的时候,一定要转换为同种数据类型

>>> age = raw_input('请输入年龄:')
请输入年龄:18
>>> age
'18'
>>> type(age)
<type 'str'>
>>> age > 19
True
>>> int(age) > 19
False

8.Python 中的算术运算符

python2.x里:

[root@localhost ~]# python2
Python 2.7.5 (default, Feb 11 2014, 07:46:25) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 5 / 2
2
>>> 100 / 300
0
>>> 5 / 2.0
2.5
>>> 5.0 / 2
2.5
>>> from __future__ import division
>>> 5 / 2
2.5
>>> 100 / 300
0.3333333333333333

python3.x 里:

[root@localhost ~]# python3
Python 3.6.4 (default, Mar 12 2019, 16:31:36) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 5 / 2
2.5
>>> 100 / 300
0.3333333333333333
>>> a = 1
>>> a += 1
>>> print(a)
2
[root@localhost ~]# python3
Python 3.6.4 (default, Mar 12 2019, 16:31:36) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 5 / 2
2.5
>>> 100 / 300
0.3333333333333333
>>> a = 1
>>> a += 1             ##a=a+1 
>>> print(a)
2

猜你喜欢

转载自blog.csdn.net/weixin_44209804/article/details/88430226