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. linux 中修改root的密码

    修改root密码 有的时候会出现忘记了root 用户密码的情况,再次我们可以通过进入single(单)用户模式,将root的密码重新设置,然后重启登录即可. 具体流程: 1.先登录root用户(密码已 ...

  2. java实现BitMap

    package bitmap; public class BitMap { private byte[] bytes; public BitMap(byte[] bytes) { super(); t ...

  3. ListView的HeaderView和Footer

    HeaderView介绍 HeaderView用法 属性中添加 ListView中属性listHeader和overScrollHeader区别: android:overScrollHeader=& ...

  4. Hibernate---单条记录的增删改查

    package com.hanqi.test; import static org.junit.Assert.*; import java.util.Date; import org.hibernat ...

  5. Hive_初步见解,安装部署与测试

    一.hive是什么东东 1. 个人理解 hive就是一个基于hdfs运行于MapReduce上的一个java项目, 这个项目封装了jdbc,根据hdfs编写了处理数据库的DDL/DML,自带的 二进制 ...

  6. 解决mysql5.6+在zabbix监控中执行脚本出现密码的错误问题

    1.mysql命令行中授权mysql监控所需的账号和密码(权限select权限即可) 2.通过mysql_config_editor 配置登录问题: [root@back_zabbix_100 scr ...

  7. ZK 父窗口与子窗口消息交互

    父窗口代码: 前台(test.zul) <?page title="" contentType="text/html;charset=UTF-8"?> ...

  8. windows下vim 块模式问题

    VIM: gvim 使用 Ctrl+V 發表於 2005 年 10 月 27 日 由 Tsung vim 要做垂直選取的動作, 就要使用 "Ctrl + v", 但是 gvim 會 ...

  9. java 正则表达式获取匹配和非获取匹配

    package test1; import java.util.regex.Matcher; import java.util.regex.Pattern; public class TestExp ...

  10. 第一课JAVA开发环境配置

    进行JAVA环境安装首先得进行jdk1.7部署,注意应放在没有中文和空格的目录下,然后进行配置环境变量,配置环境变量分为三步: 1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_ ...