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.应 ...
随机推荐
- new Option()——实现时间联动
//1.动态创建select function createSelect(){ var mySelect = document.createElement("select"); m ...
- Mysql锁机制介绍
Mysql锁机制介绍 一.概况MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎采用的是表级锁(table-level locking ...
- JAVA-2-GetDay
import java.util.*; public class Ch0310 { public static void main(String[] args) { // TODO 自动生成的方法存根 ...
- CentOS 7 之Shell学习笔记
脚本是个永恒的话题,以前Dos下面也有Shell编程这一说的,比如说BAT文件有人写的好的话,也是瞬间速度变高大上.Linux下面这个应该更占比重了.我看到园子里有位园友做了一个Linux Shell ...
- 关于js闭包杂记
闭包:一个函数oneF里return了另一个函数innerF,然后在oneF外面运行了函数innerF,如果innerF里有用到在oneF里定义的变量,则此时依然可以引用到, 但是变量值不是定义函数i ...
- phpcms v9教程 联动搜索在房地产网站开发中的应用
开发简述:使用phpcms v9系统,修改源文件5个,创建模型:楼盘.出售.出租.中介.小区,增加联动菜单:楼盘,增加用户组:房产中介.实现功能:游客发布信息.会员申请中介.楼盘全方位展示.报名团购. ...
- 在线支付接口之PHP支付宝接口开发简单介绍
php100:92:在线支付接口之PHP支付宝接口开发 支付接口一般是第三方提供的代收款.付款的平台,可以通过支付接口帮助企业或个人利用一切可以使用的支付方式.常见支付平台:支付宝.快钱.云网支付.财 ...
- Div在BOdy中居中
<h1 style="position: absolute; width: 500px; height:200px; left:%; top:%; margin-left:-250px ...
- windows7下python环境变量配置
默认情况下,在windows下安装python之后,系统并不会自动添加相应的环境变量.此时不能在命令行或cmd里直接使用python命令. 1. 首先需要在系统中注册python环境变量:假设pyth ...
- BufferedOutputStream
package file; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStr ...