python 基础笔记

源代码->字节码->机器语言
    解释器

4种类型的数--整数,长整数,浮点数,复数
#常见的类 :str
#if
if guess == number:
	print 'do sth', #在print语句的结尾使用了一个 逗号 来消除每个print语句自动打印的换行符
elif guess < number:
	print 'do sth'
elif True:
	print 'do sth'
else:
	print 'do sth'
没有switch 有时候使用数据结构 字典 会更快捷

#while ...else
while running:
	...
else:
	print '可选的else从句'
#for ...in
for i in range(1,5): ==> in [1,	2, 3, 4] #任何种类的由任何对象组成的序列,range 还可以设置步长
	print i		#用来替代for(int i = 0; i<5 ; i++)
	continue
	break
else:
	print '可选的else'

布尔类型并不是真实的01

#函数定义
def funname(apple, guess = 1 ,times = 2):#列表末尾才能默认参数赋值
	print aplle * times #会打印出几次apple的字符串
	global x #表明这里使用的x是全局的,这只是个声明作用
	....函数体
	return y'可选,没有的 默认等价于 'return None (None 特殊类型) 可以 print funname()打印出值
调用
funname(times=3, apple = 2, 8)#关键参数 这里的8到底赋值给了谁?
'''
关键字参数与字典。如果换一个角度看待你在函数中使用的关键字参数的话,你已经使用了字典了!只需想一下——你在函数定义的参数列表中使用的键/值对。当你在函数中使用变量的时候,它只不过是使用一个字典的键(这在编译器设计的术语中被称作 符号表 )。
'''
pass #空的语句块
DocStrings
def  fun()
若干代码....
	'''
	Instruction Of The Code

	.......
	'''
print fun.__doc__ #打印函数中的 '''之间的部分 (fun函数的 文档字符串 属性)

模块
import sys
print sys.path#它在sys.path变量中所列目录中寻找sys.py模块。如果找到了这个文件,这个模块的主块中的语句将被运行,然后这个模块将能够被你 使用 。注意,初始化过程仅在我们 第一次 输入模块的时候进行????
.pyc 文件(字节编译文件)
from sys import argv #部分插入
from sys import *  #一般应该避免这种写法 以免名称的冲突
模块的__name__
if __name__ == '__main__':''' 是这个则被用户单独运行'''
    print 'This program is being run by itself'
else:
    print 'I am being imported from another module' '''如import 文件名(没有py)这般调用 '''
dir(sys) 列出模块定义的标识符 和 dir(); del用于删除变量/名称 可以是数据结构 
del shoplist[0]

数据结构
三种内建的数据结构——列表、元组和字典

#列表list
shoplist =  ['apple', 'pear', 'banana']
help(list)得到列表对象的所有方法

#元组tuple(不可变) 用于给人提供安全的值 类似于const
zoo = ('wolf', 'elephant', 'penguin')
print 'Number of animals in the zoo is', len(zoo)

new_zoo = ('monkey', 'dolphin', zoo)
print 'Number of animals in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2] #new_zoo[2]是zoo 然后再zoo[2]
singleton = (2 ,)#单个元素的元组,加逗号用于区分 和表达式 (2)的区别
singleton =  ()#空元组

age = 22
name = 'Swaroop'
print '%s is %d years old' % (name, age)# 最后的%是定制格式
print 'Why is %s playing with that python?' % name #使用了一个定制,后面跟着%符号后的单个项目——没有圆括号。这只在字符串中只有一个定制的时候有效。

#字典dict
ab = {       'Swaroop'   : 'swaroopch@byteofpython.info',
             'Larry'     : 'larry@wall.org',
             'Matsumoto' : 'matz@ruby-lang.org',
             'Spammer'   : 'spammer@hotmail.com'
     }

for name, address in ab.items():
    print 'Contact %s at %s' % (name, address)


if 'Guido' in ab: # OR ab.has_key('Guido')
    print "\nGuido's address is %s" % ab['Guido']
键/值 是没有顺序的,若需要,用前记得排序

列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引操作符[]和切片[:]操作符

shoplist = ['apple', 'mango', 'carrot', 'banana']

# Indexing or 'Subscription' operation
print 'Item 0 is', shoplist[-1]#banana
# Slicing on a string
name = 'swaroop'
print 'characters 1 to 3 is', name[1:3]#wa
print 'characters 2 to end is', name[2:]#aroop
print 'characters 1 to -1 is', name[1:-1]#waroo
print 'characters start to end is', name[:]#swaroop
序列的神奇之处在于你可以用相同的方法访问元组、列表和字符串
mylist = shoplist # mylist is just another name pointing to the same object!
#若要copy则用切片操作

常见的类和方法
delimiter = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']
print delimiter.join(mylist) #Brazil_*_Russia_*_India_*_China

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值