Python的安装及文件类型、变量

一、为什么学习python

  • 服务于大数据、人工智能、自动化运维。

  • 简单易学

  • 代码简洁

  • 薪资高

  • 近几年越来越火

二、Python的安装

linux 系统默认安装,

CentOS7 默认安装了python2.7

安装ipython

yum install -y epel-release
yum install -y python-pip
pip install ipython==5.3.0

进入ipython

image.png

windows 系统中下载软件,安装环境变量,测试启动。

下载地址: https://www.python.org/downloads/windows/

windows中工具pycharm中的快捷键

alt + shift +f10   执行

ctrl + /       注释

ctrl + d     复制上行生成一行或多行

ctrl + c     复制一行

ctrl + x     剪切

ctrl + v   粘贴

ctrl + a	全选

.ctrl + shift + n  :在多文件中项目中快速查找文件

ctrl + alt + l: ctrl+a 选中,ctrl + alt + l   快速调整格式。

alt + shift    看虚拟处错误,处理

sys.argv[0]  参数设置

sys.argv[1]  参数设置

alt +回车   点中虚线   自动import

tab           光绪行后走shift + tab   光标行前走shift + enter  自动跳到一下行,

ctrt + enter   往上开新行,

三、Python的文件类型

  1. 文件类型:

(1)源代码(.py):

vim test.py
#!/usr/bin/python
print ('hello world!')

运行方法1:

 python test.py

运行方法2:

chmod +x test.py
./test.py

(2)字节代码(.pyc):

python源文件编译后为扩展名字为.pyc

删除源码,编译后的二进制文件可以独立执行。

编译方法:

vim 2.py
#!/usr/bin/python
import py_compile

py_compile.compile('test.py')

[root@localhost python]# python 2.py
[root@localhost python]# ls
2.py  test.py test.pyc

[root@localhost python]#
[root@localhost python]# python test.pyc
hello world!
[root@localhost python]#

(3)优化的代码(.pyo):

python -O -m py_compile test.py
[root@localhost python]# python -O -m py_compile test.py
[root@localhost python]# ls
2.py  test.py  test.pyc test.pyo
[root@localhost python]# python test.pyo
hello world!
[root@localhost python]#

四、Python的变量

Python的变量

变量是计算机内存中的一块区域,变量可以存储规定范围内的值,而且值可以改变结构。

python下变量是对一个数据的引用

(1)变量的命名:

变量名的长度不受限制,但其中的字符必须是字母、数字、或者下划线(_),而不能使用空格、连字符、标点符号、引号或其他字符。

变量名的第一个字符不能是数字,而必须是字母或下划线。

Python区分大小写。

不能将Python关键字用作变量名。

例如: a a1 _a

(2)变量的赋值:

是变量的声明和定义的过程。

a = 123

(3)运算符和表达式:

赋值运算符

算术运算符

关系运算符

逻辑运算符

表达式:

将不同的数据(包括变量、函数)用运算符号按一定的规则连接起来的一种式子。

1)赋值运算符

a = 3
a+=3
a-=4
a*=3
a/=2
a%=3

2)算术运算符

In [82]: 1 + 2
Out[82]: 3
In [83]: 2 - 1
Out[83]: 1
In [84]: 2 * 2
Out[84]: 4
In [85]: 6 / 2
Out[85]: 3
In [86]: 6 % 2
Out[86]: 0
In [88]: 3.999999 / 2
Out[88]: 1.9999995
In [89]: 3.999999 // 2
Out[89]: 1.0
In [90]: 3 ** 2
Out[90]: 9
In [91]:

3)关系运算符:

1 > 2
2 < 3
2 >= 1O
3 <= 56
3 == 3
2 != 34

4)逻辑运算符:

In [97]: 1 < 2 and 2 > 0Out[97]: TrueIn [98]: 1 == 1 and 2 < 1Out[98]: FalseIn [99]: 1 == 1 or 2 < 1Out[99]: TrueIn [100]: not 1 > 2Out[100]: True

5)各种运算符的优先级:

往右越高 上到下越高,

lambda 匿名函数。

image.png

练习:

写一个四则运算器:

要求从键盘读取数字。

input()与raw_input()

查看帮助:help(input)

raw_input()都当然成字符串处理

%s 格式化字符串。

vim jisuanqi.py
#!/usr/bin/python
num1 = input("Please input a number: ") 
num2 = input("Please input a number: ") 
print ("%s + %s = %s") % (num1, num2,num1+num2)
print ("%s - %s = %s") % (num1, num2,num1-num2)
print ("%s * %s = %s") % (num1, num2,num1*num2)
print ("%s / %s = %s") % (num1, num2,num1/num2)

[root@localhost python]# python jisuanqi.py
Please input: 3
Please input: 5
3 + 5 = 8
3 -  5 = -2
3 * 5 = 15
3 / 5 = 0
[root@localhost python]#


猜你喜欢

转载自blog.51cto.com/fengyunshan911/2412976
今日推荐