Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347611
  • 博文数量: 45
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 885
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-06 21:07
个人简介

做好自己,不卑不亢,持之以恒!!

文章分类

全部博文(45)

分类: Python/Ruby

2015-08-04 17:44:16

在绝大多数linuxUNIX系统安装中(包括Mac OS X,Python的解释器就已经存在了。我们可以在提示符下输入python命令进行验证(作者环境redhat-7.0)

Ok ! 如果敲入python命令出现以上信息时,表明我们已经安装了python 环境。

 如果没有安装,在redhat  linux ,那么可以用下面的命令来安装Python:
# yum install python

ubuntu /Debin linux ,那么可以用下面的命令来安装Python:
$ apt-get install python

在学习一门语言可能大家最犯困的就是看一系列语法的干条条,我本人时这样感觉的,所以我想通过写程序来和大家一起学习,在程序中去解释其中的语法和运用规则,真正体验写代码的乐趣
键入python 进入交互式解释器,
组合键Ctrl + d 退出解释器

1:“hello, world” 程序:
>>> print "hello, world"
hello, world
 
#在python中基本的输出语句:print

2:基本输入输出程序
>>> mystring = raw_input('please input your string:')
please input your string:hello,world
>>> print mystring
hello,world

#Python中的等号(=)是主要的赋值操作符,Python属于动态类型语言,也就是说不需要预先声明其变量的类型,变量的类型和值在赋值那一刻被初始化。
#从用户那里得到数据输入的最容易的方法就是使用raw_input()内建函数,她读取标准输入,并将读取的数据赋值给指定的变量。

3:标准输入输出
>>> print 'the string you input is : %s' % (mystring)
the string you input is : hello,world

#Python的print语句,与字符串格式操作符(%)结合使用,可实现字符替换功能,这一点和C语言中的printf()函数十分相似
#%s表示有一个字符串来替换,而%d表示有一个整型来替换,另外一个很常用的就是%f,它表示由一个浮点型来替换

利用这3个知识点便就可以编写一个个人信息的基本输入输出了
创建一个以.py结束的文件名便就在文本中编辑Python代码了
$ vim test.py

#!/usr/bin/python
name = raw_input('please  input your name:')
age = int(raw_input('how old are your ?'))
sex = raw_input("please input your sex:")
dep = raw_input("input your job:")

message = '''Information of the company staff:
        Name:%s
        Age:%d
        Sex:%s
        Job:%s
        '''% (name,age,sex,dep)
print message

#!/usr/bin/python     
用来指定用什么解释器运行脚本以及解释器所在的位置
''' '''可以用来注释多行
运行结果
$ python test.py
please  input your name:Tommy
how old are your ?21
please input your sex:M  
input your job:IT
Information of the company staff:
        Name:Tommy
        Age:21
        Sex:M
        Job:IT

接下来我们就利用这个程序开始学习Python的其他内容

如果我们的用户输入为空呢?程序还能继续向下运行吗?
$ python test.py
please  input your name:
how old are your ?21
please input your sex:M
input your job:IT
Information of the company staff:
        Name:
        Age:21
        Sex:M
        Job:IT
貌似是可以的,但这并不是我们想要的,这样修改:
#!/usr/bin/python
while True:
    name = raw_input('please  input your name:').strip()
    if len(name) == 0:
        print "Empty name,Try again!!"
        continue
    break
age = int(raw_input('how old are your ?'))
sex = raw_input("please input your sex:")
dep = raw_input("input your job:")

message = '''Information of the company staff:
        Name:%s
        Age:%d
        Sex:%s
        Job:%s
        '''% (name,age,sex,dep)
print message

运行结果:
[redhat@server0 python]$ python test.py
please  input your name:
Empty name,Try again!!
please  input your name:
Empty name,Try again!!
please  input your name:
Empty name,Try again!!
please  input your name:
Empty name,Try again!!
please  input your name:Tommy
how old are your ?21
please input your sex:M
input your job:IT
Information of the company staff:
        Name:Tommy
        Age:21
        Sex:M
        Job:IT

4:If语句
#Python 代码是通过缩进对齐表达代码逻辑,而不是使用大括号。因为没有了额外的字符,程序的可读性更高,而且缩进完全能清楚的表达一个语句属于那个模块,
标准if条件语句如下。
if expression:
    if_suite

#如果表达式的值非0或者为布尔值True,则代码组if_suite被执行,否则就去执行下一条语句,代码组(suite)是一个Python术语,它由一条或者多条语句组成,表示一个代码块,Python与其他语言不同,条件表达式并不需要括号括起来
#Python 当然也支持else语句,语法如下。
if expression:
    if_suite
else:
    else_suite

Python还支持elif(else if )语句,语法如下:
if expression:
    if_suite
elif expression:
    elif_suite
else:
    else_suite


5:While循环

标准while条件循环语句的语法类似if,再说一次,要使用缩进来分割每个字代码模块
while expression:
    while_suite
语句while_suite会被不断的循环执行,直到表达式的值变成0或者False,接着Python会接着执行下一条语句,类似if语句,Python中的while语句中的条件表达式也不用括号括起来

声明:s为字符串,rm为要删除的字符序列

s.strip(rm)        删除s字符串中开头、结尾处,位于 rm删除序列的字符

1. 当rm为空时,默认删除空白符(包括'\n', '\r',  '\t',  ' ')


在Python解释器中大家可能习惯用tab健像Linux那样自动补全命令,可是并未如愿,我们祥达到这种效果该怎么办呢?
编写这样一段代码:

[redhat@server0 python]$ vim startup.py
#!/usr/bin/python
# python startup file

import sys
import readline
import rlcompleter
import atexit
import os
# tab completion
readline.parse_and_bind('tab: complete')
# history file
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try:
    readline.read_history_file(histfile)
except IOError:
    pass
atexit.register(readline.write_history_file, histfile)

del os, histfile, readline, rlcompleter

2、查看python默认的模块存放路径。



thel7.0  :/usr/lib64/python2.7
拷贝功能脚本到默认模块存放路径。
[redhat@server0 python]$ cp startup.py /usr/lib64/python2.7

使用方法:

输入的时候调用下startup即可

import startup 如下图:



便可以使用tab键自动补全,降低错误率了

阅读(4111) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~