1、内建函数enumerate

friends = ['john', 'pat', 'gary', 'michael']
for i, name in enumerate(friends):
print "iteration {iteration} is {name}".format(iteration=i, name=name)

  其中 i 表示:0, 1, 2, 3;

  name表示:friends下标为i的内容,即friends[i]

2、print()格式化输出

parents, babies = (1, 1)
while babies < 100:
print("This generation has %s babies" % (babies))
parents, babies = (babies, parents + babies)

3、函数定义并调用

def greet(name):
print("Hello, my name is %s." % name)
#print("Hello, my name is %s." % (name))
greet('huabo')
greet('yanyan')

4、import模块,字符串匹配

import re
for test_string in ['555-1212', 'ILL-EGAL']:
if re.match(r'^\d{3}-\d{4}$', test_string):
print("%s is a valid UI local phone number" % test_string)
else:
print("%s rejected" % test_string)

  输出:

555-1212 is a valid UI local phone number
ILL-EGAL rejected

5、Dictionaries, generator expressions

prices = {'apple': 0.40, 'banana': 0.50}
my_purchase = {
'apple': 1, 'banana': 6}
grocery_bill = sum(prices[fruit] * my_purchase[fruit]
for fruit in my_purchase)
print('I owe the grocer $%.2f' % grocery_bill)

  0.4*1+0.5*6 = 3.4

6、 Opening files and output all the content.

#indent your python code to put into an email
import glob #out put all the Python files in the current Directory.
python_files = glob.glob('*.py')
for file_name in sorted(python_files):
print(" ------%s" % file_name) with open(file_name) as f:
for line in f:
print(line.rstrip()) print()

7、时间,条件,输出

from time import localtime
activities = {8: 'Sleeping',
9: 'Commuting',
17: 'Working',
18: 'Commuting',
20: 'Eating',
22: 'Resting' } time_now = localtime()
print('Current Time: %sYear %sMonth %sDay %sHour %sMin' % (time_now.tm_year, time_now.tm_mon, time_now.tm_mday, time_now.tm_hour, time_now.tm_min)) hour = time_now.tm_hour for activity_time in sorted(activities.keys()):
if hour < activity_time:
print(activities[activity_time])
break
else:
print('Unknown, AFK or sleeping!')

8、三单引号字符串

REFRAIN = '''
%d bottles of beer on the wall,
%d bottles of beer,
take one down, pass it around,
%d bottles of beer on the wall!
''' bottles_of_beer = 8
while bottles_of_beer > 1:
print(REFRAIN % (bottles_of_beer, bottles_of_beer, bottles_of_beer - 1))
bottles_of_beer -= 1

  注意:三单引号字符串可以使得字符串可以换行,很方便。

Python之基础(二)的更多相关文章

  1. 【python之旅】python的基础二

    一.集合的操作 1.什么是集合?     集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重:把一个列表变成集合,就自动去重 关系测试:测试两组数据之前的交集,差集,并集等关系   2.常用 ...

  2. python开发基础(二)运算符以及数据类型之bool(布尔值))

    # encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...

  3. python开发基础(二)运算符以及数据类型之dict(字典)

    # encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...

  4. python开发基础(二)运算符以及数据类型之tuple(元组)

    # encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...

  5. python开发基础(二)运算符以及数据类型之float(浮点类型)

    # encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...

  6. python开发基础(二)运算符以及数据类型之list(列表)

    # encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...

  7. python开发基础(二)运算符以及数据类型之str(字符串)

    # encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...

  8. python开发基础(二)运算符以及数据类型之int(数字)

    # encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...

  9. python django基础二URL路由系统

    URL配置 基本格式 from django.conf.urls import url #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数执行,就不再往下循环了,并给函数 ...

  10. python计算机基础(二)

    1. 操作系统有什么用? #1外部指令转化成0和1:#2.翻译所写的字符从繁(高低电压)至简(想做什么就做什么) :#3把一些硬件的复杂操作简化成一个一个接口. 2. 计算机由哪三大部分组成? 1.应 ...

随机推荐

  1. python zip文件密码爆破

    #!/usr/bin/env # coding=UTF-8 import zipfile import threading import os import sys class CrackZip: d ...

  2. LR:Code - 60990,Code - 10343 问题解决

    Code - 60990 Error: Two Way Communication Error: Function two_way_comm_post_message /two_way_comm_po ...

  3. Debian、Ubuntu常用命令大全

    注:本人是用的Debian,个别命令可能有问题. 原文:http://www.jb51.net/os/Ubuntu/56362.html 一.文件/文件夹管理 ls 列出当前目录文件(不包括隐含文件) ...

  4. 1.1HTML的基本概念

    1.WWW有3个基本的组成部分,分别是URL(统一资源定位器),HTTP(超文本传输协议),HTML(本文本标记语言). 2.一个HTML文件是由一系列的元素和标签组成的,元素不区分大小写. 3.&l ...

  5. Symfony2 EventDispatcher组件

            一个插件系统中,A插件在不影响其它插件的前提下,添加新的方法,或者在一个方法运行前做一些准备工作,通过继承来实现扩展是很不容易的,由于插件之间的关联关系,A插件的改变也会使得关联的插件 ...

  6. php curl 基本用法

    <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.baidu.com"); curl_se ...

  7. 0X0000124

     求教卡饭网友,都快疯掉了.      最近搞设计,电脑频发出现蓝屏,今晚都出现三次了,新装的win7 64位系统,都是安装的原版光驱.     错误代码基本上都是:0x00000124 (0x000 ...

  8. js前台与后台数据交互

    客户端脚本一般都在前台,这里讲的是(1)在后台调用前台定义的脚本(2)在后台如何注册客户端脚本 用途 何时使用服务器代码向页中添加客户端脚本: u 当客户端脚本的内容依赖于直到运行时才可用的信息时 u ...

  9. 一些不太常用的Linux命令

    ACCTCOM 查看所有用户执行过的进程 acctcom | tail - 查看指定用户执行过的进程/命令 acctcom -u <username> | tail - 使用一个正则表达式 ...

  10. 利用FbinstTool+大白菜u盘工具,制作多系统启动U盘【转】

    一般制作多系统启动盘的教程都会要用到rub4dos+grubinst+ultraiso+msgdiyerl等等工具,一大串的工具列表让人望而生畏.其实大白菜里已经对这些工具做了非常好的封装,利用大白菜 ...