Python语言入门-概要

Python入门-概要

Python 是一种轻量的脚本型语言

特点:

  • 发音【paisong】
  • python 英文是蟒蛇,巨蛇的意思。
  • 是由荷兰人Guido van Rossum最早发明的。
  • Perl, PHP, Python 合称为P语言。
  • 和其他语言的{...}以及 begin ... end的代码块相比,Python是通过空格缩进来区分代码块。
  • 和Ruby一样,整数(int)字符串(str)等都被作为对象来处理。

安装:

◆ Windows

http://www.python.org/ 

◆ Linux(Red Hat / CentOS)

Linux

# Python 2.7
$ sudo yum install -y python

# Python 3.6 (from EPEL)
$ sudo yum install -y epel-release
$ sudo yum install python36

# Python 3.6 (from IUS)
$ sudo yum install -y https://centos7.iuscommunity.org/ius-release.rpm
$ sudo yum install -y python36u

 

◆ Linux(Ubuntu / Debian)

Linux

$ sudo apt-get install python2.7

◆ Linux源代码安装

CentOS 7.0 に Python 2.7.9 安装

Linux

# yum -y install wget gcc zlib-devel gdbm-devel readline-devel
# yum -y install sqlite-devel openssl-devel tk-devel bzip2-devel
$ wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
$ tar zxvf Python-2.7.9.tgz
$ cd Python-2.7.9
$ ./configure --with-threads --enable-shared --prefix=/usr/local
$ make
$ sudo make altinstall

Python运行方法

◆ Windows

全局变量 Path 里指定目录。Windows下,[控制面板]-[系统]-[系统详细设定]-[环境变量]里加入python.exe的所在目录(例如: C:\Python27)。

DOS命令

C:\>set
Path=C:\WINDOWS\system32;C:\WINDOWS;...(略)...;C:\Python27
C:\>python -V
Python 2.7.9
C:\>type hello.py
print "Hello world!"
C:\>python hello.py
Hello world!
C:\>

◆ Linux

环境变量PATH里加入 python文件的目录(例如:/usr/local/python/bin)。如果安装再/usr/bin下,是不需要指定PATH的。

Linux

$ export PATH=$PATH:/usr/local/python/bin
$ python -V
Python 2.7.9
$ cat hello.py
print "Hello world!"
$ python hello.py
Hello world!
$对话

对话模式

Python具有对话模式。

Linux

$ python
Python 2.7.9 (default, Dec 26 2014, 02:00:03)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 5+8
13
>>> 'Hello world!'
'Hello world!'
>>> a = 5
>>> b = 8
>>> a + b
13
>>>             # Linux:Ctrl-D, Windows:Ctrl-Z Enter结束




对话模式下输入help(),可以查看到各种帮助信息。

Linux

$ python
>>> help()
Welcome to Python 2.7!  This is the online help utility.
   :
>>> import sys
>>> help(sys)
Help on built-in module sys:

NAME
    sys

FILE
    (built-in)

MODULE DOCS
    http://docs.python.org/library/sys

DESCRIPTION
    This module provides access to some objects used or maintained by the
    interpreter and to functions that interact strongly with the interpreter.
       

关于Python 3

2008年Python 3.0出现、Python 2.x没有完整的兼容性、现在Python 2使用的人比较多。与Python 2不同的点主要有以下几点。

  • print命令变为函数。在Python 2中,可以写为print "..."的格式,在3.0中变为了print("...")。在Python 2中也可以写为 print("...")的形式。但是如果print如下输入的话,print("AAA", "BBB") ,结果为「("AAA", "BBB")」。
  • Python 2里,"..."代表字节类型字符、u"..." 代表Unicode字符串,在Python 3.0~3.2中,"..."默认为Unicode字符串,b"..."为字节型字符串,输入u"..."会报(SyntaxError异常)。但是Python 3.3之后 u"..."的写法又恢复了,"..."也作为Unicode字符串来处理。
  • 整数 int 和 long 被统一为 int类型。也许性能上会有点差别,但是32bit或者64bit 的长整数,也不会丢失精度。123L中的 L写法会导致(SyntaxError异常),sys.maxint也会显示(AttributeError异常)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值