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. Arcgis server - ' packaging failed '

    我在使用ARCCatalog发布地图服务时,报这个错:packaging failed 然后我从头试,发现它提示说我的目录'C:\Users\Administrator\AppData\Local\E ...

  2. Swift - 39 - 枚举类型关联默认值

    //: Playground - noun: a place where people can play import UIKit enum Month: Int { // 这么定义, 后面的Feb, ...

  3. DeDe缩略图路径的修改

    今天在使用dedecms的时候,遇到的一点小问题,移站的时候缩略图不显示.然后就去百度搜了一下,出来的都是千篇一律. 因为一个网站里面缩略图太多,手动比较慢而已费时间,  俗话说的好“时间就是生命” ...

  4. 关于latex的使用随笔

    1.the context after paragraph topic to new line /paragraph{xxxx}~{} 2.关于表格中文本过长自动换行问题的解决 P.S.:直接贴一段写 ...

  5. 文成小盆友python-num13 整个堡垒机

    本节主要内容: 1.pymsql的使用 2.SQLAchemy使用 3.Paramiko 4.通过ORM功能使用和Paramiko的使用实现一个简单的堡垒机模型. 一.pymsql的使用 pymsql ...

  6. 向null地址copy数据和不断改变指针指向

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...

  7. IRP派遣操作

    IRPTrace工具跟踪IRP 派遣函数(Dispathc Funtion)是windows驱动中的重要概念.驱动程序的主要功能是负责处理I/O请求,其中大部分I/O请求是在派遣函数中处理的.用户模式 ...

  8. 【转】Nginx配置文件详细说明

    Nginx配置文件详细说明 在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户user www-data;    #启动进程,通常设置成和cpu的数量相等 ...

  9. Android监听事件

    ListView事件监听: setOnItemSelectedListener 鼠标滚动时触发 setOnItemClickListener 点击时触发 EditText事件监听: setOnKeyL ...

  10. 单片机设计与KeilC编程总结

    1基本原则    质量是关键.没有人会对很差的工作感到满足.当完成高质量的工作时,你会为此而感到骄傲.不管你是否知道,你都会因为你的高质量工作而得到信誉.因此,要想为自己所做的事感到骄傲,就需要建立个 ...