`

python安装学习

阅读更多

久仰python大名,在编程排行榜上最近好像赶超php份额了,技多不压身嘛,了解了解总是好的我也是在网上看别人的安装和运行,在我自己电脑本地下载运行代码,后面准备用python写个爬虫(抓取统计网页访问数据);

博客出自:http://blog.csdn.net/hitlion2008/article/details/9285785

首先:安装python,我下载的是python最新的3.4.1的版本 一路点击“下一步”就行了;注意一个问题就是环境变量里需要加入你python安装目录,我安装在C:\Python34\

然后运行:

运行python有两种方式,

一种是:使用Python 自带的IDLE

二种是:编辑.py文件,然后执行

第一种:使用Python 自带的IDLE

在安装目录里打开py.exe 如图:


 您应该看到“>>>”,这被称为“Python解释器提示符”,你可以开始输入的东西。

输入 print('Hello World')   // Hello World

输入exit()可以退出命令提示符

Python是以缩进来表示语句块,同一缩进级别为同一级别的语句块.

缩进最好使用四个空格.而且要注意缩进要一致,使用空格就全都用空格,使用Tab就都使用Tab,混用就可能得到缩进错误

一行当中,从#开始地方就是注释.不会影响下一行

""引号放在文件的开头,函数的开头或者一个类的开头,就是文档注释

如果一行太长了,写不下了,就需要在下一行接着写,这时可以使用\来告诉Python,下一行继续

如果想在一行放多个语句,就需要用;来分隔语句:

 

a = 1; b = 2; c = 3;

list[index] --- 返回第(index+1)个元素,受C语言影响,下标亦是从0开始

list[start:end] --- 返回从start开始,到end-1,也就是list[start], list[start+1].....list[end-1]

list[start:end:step] --- 与上面类似,只不过每隔step取一个

list[:end]  ---- 缺省的开端是0

list[start:] ---- 缺省的结尾是len(list),或者-1

#在这里我只写些调试代码和运行结果

>>> a = [1, 3, 5, 7];  

>>> a[0]  

1  

>>> a[3]  

7  

>>> a[-1]  

7  

>>> a[-2]  

5  

>>> a[0:3]  

[1, 3, 5]  

>>> a[1:3:2]  

[3]  

>>> a[0:3:2]  

[1, 5]  

>>> a[0:-1:2]  

[1, 5]  

>>>  

 

包含关系: in, not in

>>> 3 in a  

True  

>>> 8 in a  

False  

>>> 8 not in a  

True  

>>>  

连接符: +

>>> a + [9, 11]  

[1, 3, 5, 7, 9, 11] 

重复: *

>>> a * 2  

[1, 3, 5, 7, 1, 3, 5, 7]  

>>>  

 

>>> str = 'hello, world'  

>>> str[0:3]  

'hel'  

>>> str[0:3:2]  

'hl'  

>>> str[-1]  

'd'  

>>> str * 2  

'hello, worldhello, world'  

>>> '3' in str  

False  

>>> 'le' in str  

False  

>>> 'el' in str  

True  

>>> 'ell' not in str  

False  

>>>

字串格式化符%

>>> "Int %d, Float %d, String '%s'" % (5, 2.3, 'hello')  

"Int 5, Float 2, String 'hello'"  

>>>  

 

Dictionary字典

用于以Key/Value方式存储的容器.创建方式为{key1: value1, key2: value2, ....}, 更改方式为dict[key] = new_value;

>>> box = {'fruits': ['apple','orange'], 'money': 1993, 'name': 'obama'}  

>>> box['fruits']  

['apple', 'orange']  

>>> box['money']  

1993  

>>> box['money'] = 29393  

>>> box['money']  

29393  

>>> box['nation'] = 'USA'  

>>> box  

{'money': 29393, 'nation': 'USA', 'name': 'obama', 'fruits': ['apple', 'orange']}  

>>> box.keys()  

['money', 'nation', 'name', 'fruits']  

>>> box.values()  

[29393, 'USA', 'obama', ['apple', 'orange']]  

>>>  

 

分支语句

>>> a = 3; b = 4; c = 5;  

>>> if a == b and a != c:  

...     print "Are you sure"   // 这里注意需要4个空格保持缩进 ,否则会报错

... elif (a == c and b == c):  

...     print "All equal"  

... else:  

...     print "I am not sure"  

...   

I am not sure  

>>>  

while循环

>>> i = 0;   

>>> while i < 3:  

...     print "I am repeating";    // 记得缩进

...     i += 1;    // 记得缩进

...   

I am repeating  

I am repeating  

I am repeating  

>>>  

 

for语句

>>> msg = "Hello";  

>>> for c in msg:  

... print c;  //缩进

...   

H  

e  

l  

l  

o  

>>> 

数组推导

>>> a = range(4);  

>>> a  

[0, 1, 2, 3]  

>>> [x*x for x in a if x % 2 == 0]  

[0, 4]  

>>>  

 

函数

如何定义函数

def function_name(args):

      function_body;

调用函数的方式function_name(formal_args):

>>> def power(x):  

... return x*x;  

...   

>>> power(4)  

16  

>>>  

 

匿名函数,或者叫做lambda函数,它没有名字,只有参数和表达式:

>>> d = lambda x: x*x;  

>>> d(2)

 

一些常用的内置函数

print --- 打印输出

print var1, var2, var3  // 打印多个

>>> a  

[0, 1, 2, 3]  

>>> d  

<function <lambda> at 0x7f668c015140>  

>>> print a, d   // 同时输出a 和 d

[0, 1, 2, 3] <function <lambda> at 0x7f668c015140>  // 同时输出a 和 d

>>>  

 

print与%结合更为强大:

>>> print "today is %d, welcome %s" % (2013, 'alex');  

today is 2013, welcome alex  

>>>  

 

len()---返回列表,字串的长度

len('aa') // 2

 

range([start], stop, [step]) --- 生成一个整数列表,从,start开始,到stop结束,以step为步长

>>> range(4)  

[0, 1, 2, 3]  

>>> range(1,4)  

[1, 2, 3]  

>>> range(1,4,2)  

[1, 3]  

>>>  

 

help(func)---获取某个函数的帮助文档.

 

正则表达式

>>> message = 'Welcome to the year of 2013';  

>>> import re;  

>>> p = re.compile('(\d+)');  

>>> m = p.search(message);  

>>> m  

<_sre.SRE_Match object at 0x7f668c015300>  

>>> print m.group(1)  

2013  

 

>>>  

 

第二种是新建testpy.py 然后运行

testpy.py代码(引用别人的):

#!/usr/bin/python

# -*- coding: utf-8 -*-

"""

Function:

【整理】如何在Windows下开发Python(如何运行Python脚本)

 

http://www.crifan.com/how_to_do_python_development_under_windows_environment

 

Author:     Crifan Li

Version:    2012-12-06

"""

  

import platform;

  

pythonVersion = platform.python_version();

uname = platform.uname();

  

print("Just for demo how to do python development under windows:");

print("Current python version info is %s"%(pythonVersion));

print("uname=",uname);

保存到我的安装目录下:C:\Python34\testpy

windows键 + r 打开cmd 进去python安装目录



 运行结果如上图所示;

个人觉得python还不错,简洁不难跟php一样。

 

  • 大小: 12.1 KB
  • 大小: 10.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics