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. sql server 导出

    http://ssat.codeplex.com/SourceControl/latest 用于连接sql server

  2. 以下是jQuery和JavaScript实现相同操作的等价代码。

    选择元素  Javascript代码 1.// jQuery   2.var els = $('.el');   3.  4.// 原生方法   5.var els = document.queryS ...

  3. Uncaught TypeError: Cannot set property 'innerHTML' of null

    学习Chrome插件时,要在弹出页面中显示当前时间,结果怎样也显示不出来 看了 http://www.cnblogs.com/mfryf/p/3701801.html 这篇文章后感悟颇深 通过调试发现 ...

  4. js中数组去重的几种方法

    js中数组去重的几种方法         1.遍历数组,一一比较,比较到相同的就删除后面的                 function unique(arr){                 ...

  5. js中运算符的优先级

    不确定下面表达式的运算顺序? a>b?c:d+e a&&b==c 看看下表就清楚了,下表按优先级从最高到最低的列出,具有相同优先级按从左至右的顺序求值. 运算符 描述 . [] ...

  6. winform中相对路径和绝对路径的获取

    例如: Path.GetFullPath(Path.Combin(@"C:\a\b\c","..\b.text")); ..代表上级目录 .代表当前目录 结果: ...

  7. Properties配置文件

    package file; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; ...

  8. Core开发-后台任务利器Hangfire使用

    Core开发-后台任务利器Hangfire使用 ASP.NET Core开发系列之后台任务利器Hangfire 使用. Hangfire 是一款强大的.NET开源后台任务利器,无需Windows服务/ ...

  9. Lua----注意事项

    前言:Lua相对一般的语言相对简单,有c基础看一遍就差不多了.一般的代码都能够看懂.但是Lua也有一些自己的特点,区别与其他语言,这里需要注意一下. 1.数组下标 在Lua中数组下标是从1开始计数的. ...

  10. Swift—使用try?和try!区别-备

    在使用try进行错误处理的时候,经常会看到try后面跟有问号(?)或感叹号(!),他们有什么区别呢? 1.使用try?  try?会将错误转换为可选值,当调用try?+函数或方法语句时候,如果函数或方 ...