安装python
python 安装包可以去官网下载 www.python.org 安装过程就不说了 winodws需要配置环境变量,这点和JAVA一样。Winodws 下个人推荐使用PyCharm。Linux下自带python2.x 3.0的版本需要自己安装,2.x 和 3.x 有些包和方法不一样,python官方宣称 2.x的版本只支持到 2020年 所以现在学习的小伙伴可以考虑重点学习3.x
第一个python程序,helloworld!
linux 编辑一个hello.py 文件
vim hello.py #!/usr/bin/env python print("Hello,world!")
通过 python hello.py 执行 linux 执行一个python程序就这么简单,和执行shell差不多。
字符集的问题:python 3.0 以上的版本默认就是 utf-8的 而2.x 的版本则需要先声明字符集 格式如下:
#_*_coding:utf-8_*_
python的变量
命名方式和其他语言基本一样,头变量名只能是:字母、数字、下划线,且只能以字母和下划线开头
变量的命名规范写法推荐2种:驼峰法和下划线法
1.下划线
例如: sonoftwinsbrotherage = 2
2.驼峰体
例如: NameOfTwinsGF = "FengJie"
声明一个变量:
name = "Koala"
python注释规范写法
1.单行注释
res = map(lambda x:x**x, range(10)) #this code to mutiply .....x
2.多行注释
python 开发规范,每一行不应该超过80个字符
三个引号,单双都可以
''' this code to mutiply .....x this code to mutiply .....x this code to mutiply .....x '''
或者:
""" this code to mutiply .....x this code to mutiply .....x this code to mutiply .....x """
简单程序编写
1.读取用户输入
3.x写法
name = input ("input your name:") print("user input msg:", user_input)
2.7
user_input = raw_input =raw_input("your name:") print(user_input)
2.7里 如果你输入是个字符串,系统会认为是个变量,系统会去找变量,变量需要事先声明,没有声明所以会报错。但是数字不会有这个问题。
[root@linux-node1 ~]# python Python 2.7.5 (default, Aug 18 2016, 15:58:25) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> user_input = input("you name:") you name:koala Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'koala' is not defined >>> user_input = input("you name:") you name:5 >>> name = "Jack" >>> user_input = input("you name:") you name:Jack Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'Jack' is not defined >>> user_input = input("you name:") you name:name >>> >>> >>> print(user_input) Jack >>>
格式化输入
3.0 input 默认输入的是string类型的数据,如果要输入数据类型为数字需要进行数据类型转换。例如:
age = int(input ("input your age:"))
# -*- coding:utf-8 -*- #Author:Koala W name = input ("input your name:") age = int(input ("input your age:")) job = input ("input your job:") msg = ''' Information of user %s ------------------- Name: %s Age: %d jop: %s -------------------- '''% (name,name,age,job) print(msg)
执行结果:
D:\python34\python.exe E:/PycharmProjects/s13/day1/hello.py input your name:koala input your age:21 input your job:engineer Information of user koala ------------------- Name: koala Age: 21 jop: engineer -------------------- Process finished with exit code 0
%d 整型
%f 浮点
%s string 字符串
加载系统库
在程序开始的时候用 import 加载 和 JAVA 的方式一样
import getpass username = input("username:") password = getpass.getpass("password:") print(username,password)’
加载system 库 python 执行LINUX 命令
import os
os.system()方法执行linux 命令
>>> import os >>> os.system("df -h") Filesystem Size Used Avail Use% Mounted on /dev/sda333G 3.8G 30G 12% / devtmpfs903M 0 903M 0% /dev tmpfs 912M 28K 912M 1% /dev/shm tmpfs 912M 17M 896M 2% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 1014M 170M 845M 17% /boot tmpfs 183M 0 183M 0% /run/user/0 0 <---返回值 和 shell脚本一样 成功为0 不成功为大于零的整数
命令执行结果保存到变量
os.popen("df -h").read()
直接赋值给变量只能保存返回值0
>>> cmd_res = os.system("df -h") Filesystem Size Used Avail Use% Mounted on /dev/sda333G 3.8G 30G 12% / devtmpfs903M 0 903M 0% /dev tmpfs 912M 28K 912M 1% /dev/shm tmpfs 912M 17M 896M 2% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 1014M 170M 845M 17% /boot tmpfs 183M 0 183M 0% /run/user/0 >>> print(cmd_res) 0
保存返回的结果到变量
>>> cmd_res = os.popen("df -h").read() >>> print cmd_res Filesystem Size Used Avail Use% Mounted on /dev/sda333G 3.8G 30G 12% / devtmpfs903M 0 903M 0% /dev tmpfs 912M 28K 912M 1% /dev/shm tmpfs 912M 17M 896M 2% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 1014M 170M 845M 17% /boot tmpfs 183M 0 183M 0% /run/user/0
python 模块简介
每个python脚本文件就是一个python模块 import 加载就可以使用
sys 模块 python 全局环境变量
>>> import sys >>> print(sys.path) ['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']
自己写的模块放到 /usr/lib/python2.7/site-packages 下
python会根据 sys.path 列表中的路径从头往后查找直到找到位置,因此,如果当前目录中有同名的包,则会优先加载当前目录下的包。
基流程语句 if else
if 表达式A: 表达式B elif 表达式C: 表达式D else: 表达式E
注意:条件判断表达式后面以:结尾。python通过缩进来表示从属关系,官方建议缩进四个空格
# -*- coding:utf-8 -*- #Author:Koala W user = 'alex' passwd = 'alex3714' username = input("username:") password = input("password:") if user == username and passwd == password: print("Welcome login") else: print("Invalid username or password..")
用条件判断表达式编写一个猜年龄游戏
# -*- coding:utf-8 -*- #Author:Koala W age = 22 guess_num = int(input("input your guess num:")) if guess_num == age : print("Congratulations! you got it.") elif guess_num >age: print("Think smaller!") else: print("Think biger!")
循环语句
for 循环
语法:
for i in range(10): ****表达式A <--切记前面*号位置需要缩进
注意: 循环中是把 range(10)中的值拿出来一个一个的赋给i来执行循环,因此在循环中给i重新复制无法改变循环的次数,循环会在下一轮循环开始的时候重新给i赋值下次循环的值。
循环10次打印变量i
>>> for i in range(10): ... print(i) ... 0 1 2 3 4 5 6 7 8 9 >>>
小技巧:批量缩进选定要缩进的所有行 然后按tab键,回退 shift+tab
利用循环优化猜年龄游戏
# -*- coding:utf-8 -*- #Author:Koala W age = 22 counter = 0 for i in range(10): print('-->counter:',counter) if counter <3: guess_num = int(input("input your guess num:")) if guess_num == age : print("Congratulations! you got it.") break #终止跳出整个loop elif guess_num >age: print("Think smaller!") else: print("Think biger!") else: continue_confirm = input("Do you want to contiune because you are stupid:") if continue_confirm == 'y': counter = 0 continue #跳出当次循环 #pass 占位符 else: print("Bye!") counter+=1