一、python 环境
python 安装
windows:
1、下载安装包
https://www.python.org/downloads/
2、安装
默认安装路径:C:\python27
3、配置环境变量
右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【Python安装目录追加到变值值中,用 ; 分割】
如:原来的值;C:\python27,切记前面有分号.
linux:
无需安装,原装Python环境
ps:如果自带2.6,请更新至2.7
二、Python 入门
1、第一句Python代码
在 /home/dev/ 目录下创建 hello.py 文件,内容如下:
print ("hello,world")
执行 hello.py 文件,即: python /home/dev/hello.py
python内部执行过程如下:
2、python解释器
上一步中执行 python /home/dev/hello.py时,明确的指出hello.py脚本同python解释器来执行。
如果想要类似于执行shell脚本一样执行python脚本,例:./hello.py,那么就需要在hello.py文件的头部指定解释器,如下:
#!/usr/bin/env python
print("hello world!")
如此一来,执行:./hello.py 即可。
执行前需要给予hello.py执行权限 ,chmod 755 hello.py
3、注释
单行注释:# 被注释的内容
多行注释:"""被注释的内容"""
4、变量
变量定义规则:
1、变量名只能是字母、数字、下划线的任意组合
2、变量的第一个字符不能是数字
3、以下关键字不能声明为变量名
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
5、基础数据类型
字符串
n1 = "alex" n2 = 'root' n3 = """eric""" n4='''tony'''
加法:
n1 = "alex"
n2 = "nb"
n3 = "db"
n4 = "n1 + n2 +n3"
# "alexnbdb"
乘法:
n1 = "alex"
n2 = n1 * 10
# "alexalexalexalexalexalexalexalexalexalex"
数字
age=21 weight = 64 fight = 5
n1 = 9
n2 = 2
n3 = n1 + n2
n3 = n1 - n2
n3 = n1 * n2
n3 = n1 / n2
n3 = n1 % n2
n3 = n1 ** n2
布尔值
真:True
假:False
6、条件语句
if...else... 语法结构
语法格式一:
if "alex" == "alex":
n2 = input('>>>')
if n2 == "确认":
print('alex SB')
else:
print('alex DB')
else:
print('error')
语法格式二:
if 条件1:
pass
elif 条件2:
pass
elif 条件3:
pass
else:
pass
print('end')
语法格式三:
if n1 == "alex" or n2 == "alex!23":
print('OK')
else:
print('Error')
7、循环语句
while 循环语句输出 {1 2 3 ... 10}
#!/usr/bin/env python
# -*- coding:utf8 -*-
n = 1
while n < 11:
print(n)
n = n + 1
print("---end---")
while...else循环语句
count = 0
while count < 10:
print(count)
count = count + 1
else:
print("else")
print("---end---")
死循环
where 1==1:
print("ok")
8、continue、break
continue 终止当前循环,开始下一次循环
count = 0
while count < 10:
count = count + 1
print(count)
continue
print("123456")
print("end")
break 终止所有循环
count = 0
while count < 10:
count = count + 1
print(count)
break
print("123456")
print("end")
9、运算符
算术运算:
比较运算
赋值运算
逻辑运算
成员运算
身份运算
位运算