1.raw_input的使用

从键盘读取信息,返回字符串

例:

hrs = raw_input("Enter Hours:")
pay=raw_input("Enter Pay:")
print float(hrs)*float(pay)

2.try: except:  类似c中的try throw catch

3.strings

几个有用的string处理函数:

1)len(string)某字符串的长度

2)string.startswith('str')某字符串是否以str开头

3)lstrip('x') rstrip('x')  strip('x')删除字符串左面 右面 全部的x

4)string.find('a','b')从b开始寻找第一个a所在的位置(python中的计数方式从0开始)

5) string.split('x') 以x分割字符

注意:string[n:m]不包括第m个元素

4.python关于文件的读取

示例程序 找出文件中

X-DSPAM-Confidence:    0.8475    这样的行,并计算平均值

fname = raw_input("Enter file name: ")  
fh = open(fname)                                                                  #fh为文件的句柄
total=0
count=0
for line in fh:
  if not line.startswith("X-DSPAM-Confidence:") :

    continue
  h=line.find(':')
  i=line.find('/n',h)
  num=line[h+1:i]
  num=float(num)
  total=total+num
  count=count+1
average=total/count
print "Average spam confidence:",average

5.list

ls=list()构建一个空的list

ls.append()向ls中添加一个元素

list.sort() 排序

max(list) min(list)  sum len...

示例:

Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in alphabetical order.

代码:

fname = raw_input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
    line.strip('\n')
    words=line.split()
    for word in words:
        if word not in lst:
            lst.append(word)
            lst.sort()
print lst

6.dictionary

字典是使用哈希存储的一种数据结构,所以输出字典是随机的 这点与list不同

字典中每个key对应一个value

例:

>>>dic={'tom':1,'Sam':2,'Peter':3}

>>>print list(dic)

['Tom','Sam','Peter']

>>>print dic.keys()

['Tom','Sam','Peter']

>>>print dic.values()

[1,2,3]

>>>print dic.items()

[('tom',1),(),()]

dictionary有个一非常有意思的函数get()

dic[a]=dic.get(a,0)+1

如果字典中没有a的话将其加入,如果有a的话将其+1

7.sort()与sorted()的比较

sort方法仅被定义在list中,而sorted对所有可迭代序列都有效

只需要调用sorted()方法。它返回一个新的list,新的list的元素基于小于运算符(__lt__)来排序。

你也可以使用list.sort()方法来排序,此时list本身将被修改。通常此方法不如sorted()方便,但是如果你不需要保留原来的list,此方法将更有效。

8.tuples

tuples和list类似,但是tuples一但确定了就不能更改

>>>tup=(1,2,3)注意这里是(),而list的初始化用的是[]。

例:Write a program to read through the mbox-short.txt and figure out the distribution by hour of the day for each of the messages. You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon.

From stephen.marquard@uct.ac.za Sat Jan  5 09:14:16 2008

Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below.

代码:

name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
dic=dict()
for line in handle:
     if not line.startswith('From '):
       continue
  words=line.split()
  word=words[5]
  time=word.split(':')
  hour=time[0]
  dic[hour]=dic.get(hour,0)+1
tupl=sorted(dic.items())
for hour,times in sorted(tupl):
  print hour,times

8.c={'a':10,'b':1,'c':5}

print sorted([(v,k)] for k,v in c.items())

链表推导式

链表推导式提供了一个创建链表的简单途径,无需使用 map(), filter() 以及 lambda。返回链表的定义通常
要比创建这些链表更清晰。每一个链表推导式包括在一个for语句之后的表达式,零或多个for或if语句。返回
值是由for或if子句之后的表达式得到的元素组成的链表。如果想要得到一个元组,必须要加上括号。

十分钟学会python的更多相关文章

  1. 快速入门:十分钟学会Python

    初试牛刀 假设你希望学习Python这门语言,却苦于找不到一个简短而全面的入门教程.那么本教程将花费十分钟的时间带你走入Python的大门.本文的内容介于教程(Toturial)和速查手册(Cheat ...

  2. 快速入门:十分钟学会Python(转)

    初试牛刀 假设你希望学习Python这门语言,却苦于找不到一个简短而全面的入门教程.那么本教程将花费十分钟的时间带你走入Python的大门.本文的内容介于教程(Toturial)和速查手册(Cheat ...

  3. 高速入门:十分钟学会Python

    初试牛刀 如果你希望学习Python这门语言.却苦于找不到一个简短而全面的新手教程.那么本教程将花费十分钟的时间带你走入Python的大门.本文的内容介于教程(Toturial)和速查手冊(Cheat ...

  4. 大数据处理之道(十分钟学会Python)

    一:python 简介 (1)Python的由来 Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个 ...

  5. PHP学习过程_Symfony_(3)_整理_十分钟学会Symfony

    这篇文章主要介绍了Symfony学习十分钟入门教程,详细介绍了Symfony的安装配置,项目初始化,建立Bundle,设计实体,添加约束,增删改查等基本操作技巧,需要的朋友可以参考下 (此文章已被多人 ...

  6. Python十分钟学会

    初试牛刀 假设你希望学习Python这门语言,却苦于找不到一个简短而全面的入门教程.那么本教程将花费十分钟的时间带你走入Python的大门.本文的内容介于教程(Toturial)和速查手册(Cheat ...

  7. 十分钟学会 tmux

    tmux 是一款终端复用命令行工具,一般用于 Terminal 的窗口管理.在 macOS 下,使用 iTerm2 能应付绝大多数窗口管理的需求. 如上图所示,iTerm2 能新建多个标签页(快捷键 ...

  8. 快速入门:十分钟学会PythonTutorial - Learn Python in 10 minutes

    This tutorial is available as a short ebook. The e-book features extra content from follow-up posts ...

  9. python第八篇:十分钟学会Flask

    什么是Flask Flask是一个基于Python并且依赖于Jinja2模板引擎和Werkzeug WSGI服务的一个微型框架 Flask中包含一个轻量级的web 服务器主要用于在开发阶段测试使用 F ...

随机推荐

  1. Android --RatingBar的使用

    1.效果图

  2. strace命令跟踪进程

    在实际系统维护过程中,常常需要知道一个进程在做哪些动作,比如想判断一个进程是否hang,我们可以使用strace命令,此命令式用来跟踪一个进程在调用哪些系统函数和信号 通过跟踪xinetd进程演示st ...

  3. How to create a project with Oracle Policy Modeling

    This blog is about how to create a project with Oracle Policy Modeling. You can do it successfully i ...

  4. SolrCloud 5.x 集群部署方法

    CentOS下安装Solr5.3    http://www.centoscn.com/image-text/install/2015/0918/6190.html solr5.3.1 集群服务搭建 ...

  5. Python中的socket 模块

    Python 提供了两个基本的 socket 模块.第一个是 Socket,它提供了标准的 BSD Sockets API.第二个是 SocketServer, 它提供了服务器中心类,可以简化网络服务 ...

  6. 李洪强iOS面试题之-iOS选择题

    1.及时聊天app不会采用的网络传输方式是 DA UDP B TCP C Http D FTP 2.下列技术不属于多线程的是 AA Block B NSThread C NSOperation D G ...

  7. 关于JS的算法

    一.快速排序 function qSort(arr) { if(arr.length === 0) { return []; } var left = []; var right = []; var ...

  8. css重新认识(2)

    1)行内元素可以用margin-left与margin-right调整位置,但用margin-top与margin-bottom来调整位置不会有效果,只有具有block属性值后才能像块级元素般被外边距 ...

  9. html5学习小结,float练习。

    经过两天的H5学习之后,做了一下float属性的练习,要做出来的效果为: 下面为代码部分,所用到的知识不多,不过才现在刚开始,以后要学的东西还有很多,大家继续加油! <!DOCTYPE html ...

  10. 示例在同一台机器上使用RMAN克隆数据库

    1.查看主库ZDJS并使用RMAM进行备份 [oracle@std ~]$ sqlplus '/as sysdba' SQL*Plus: Release - Production on Wed Jan ...