Python之基础(二)
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之基础(二)的更多相关文章
- 【python之旅】python的基础二
一.集合的操作 1.什么是集合? 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重:把一个列表变成集合,就自动去重 关系测试:测试两组数据之前的交集,差集,并集等关系 2.常用 ...
- python开发基础(二)运算符以及数据类型之bool(布尔值))
# encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...
- python开发基础(二)运算符以及数据类型之dict(字典)
# encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...
- python开发基础(二)运算符以及数据类型之tuple(元组)
# encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...
- python开发基础(二)运算符以及数据类型之float(浮点类型)
# encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...
- python开发基础(二)运算符以及数据类型之list(列表)
# encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...
- python开发基础(二)运算符以及数据类型之str(字符串)
# encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...
- python开发基础(二)运算符以及数据类型之int(数字)
# encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...
- python django基础二URL路由系统
URL配置 基本格式 from django.conf.urls import url #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数执行,就不再往下循环了,并给函数 ...
- python计算机基础(二)
1. 操作系统有什么用? #1外部指令转化成0和1:#2.翻译所写的字符从繁(高低电压)至简(想做什么就做什么) :#3把一些硬件的复杂操作简化成一个一个接口. 2. 计算机由哪三大部分组成? 1.应 ...
随机推荐
- mysql_fetch_assoc() ,mysql_fetch_array() , mysql_fetch_row()的区别
1. mysql_fetch_assoc() 函数从结果集中取得一行作为关联数组. 返回根据从结果集取得的行生成的关联数组,如果没有更多行,则返回 false. 输出: Array ( [LastNa ...
- android获取在res文件下的图片资源
//得到该图片的id(name 是该图片的名字,"drawable" 是该图片存放的目录,getPackageName()是应用程序的包) int resID = getResou ...
- HTTP Content-type 对照表
文件扩展名 Content-Type(Mime-Type) 文件扩展名 Content-Type(Mime-Type) .*( 二进制流) application/octet-stream .tif ...
- c#之线程入门
C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为“主线程”)自动创建的,并具有多线程创建额 ...
- Python文件之----CSV
# -*- coding:utf-8 -*- ''' Created on 2015年4月20日 @author: liuxue ''' import csv import sys reload(sy ...
- 武汉科技大学ACM :1006: 华科版C语言程序设计教程(第二版)习题7.15
Problem Description 输入n个字符串(n<=100),输出其中最长的串,如果有多个则取最先找到的那一个. Input 多组测试数据. 每组测试数据第一行包含一个整数n,表示一共 ...
- Max Min Middle
/*三者中的中间数*/#define Max(a,b) (a>b?a:b) #define Min(a,b) (a<b?a:b) int MiddleOfThree(int a, int ...
- rsync指令详解
rsync指令详解(更详细的看官方文档http://rsync.samba.org/ftp/rsync/rsync.html) [root@Centos epel]# rsync --help rsy ...
- Oracle数据库之创建表结构
Oracle数据库之创建表结构 主键与外键 主键:关系型数据库中的一条记录中有若干个属性,若其中的某一个属性组(可以是一个属性,也可以是多个)能唯一标识一条记录,那么该属性组就是主键. 外键:关系型数 ...
- php加密解密实用类
一个加解密类.如果你想在用户忘记密码时为他或她找回原来的密码,那么这个类是个好用的工具 用户注册的密码一般不会明文保存,总得加个密先.最简单的当然是在数据库sql语句中调用md5函数加密用户密码.这里 ...