Python常用知识点

1. for

#!/usr/bin/python

for letter in 'chinese':
	if letter == 'n':
		pass
		print "this is a pass block"
	print 'Current Letter:',letter

for zimu in 'Python':
	if zimu == 'h':
		break
	print "current zimu = ",zimu
	

print "Good bye"

2. while

#!/usr/bin/python

val = 11
while val > 0:
	print "current value = ",val
	val = val - 2
	if val == 3:
		break

var = 11
while var > 0:
	print "value = ",var
	var = var - 2
	if var == 3:
		continue

print "Over end"

3. function

def关键字

#!/usr/bin/python


def printstr(strs):
	"this function is passing into a string"
	print strs;
	return;
printstr("cat and dog")
printstr("I like it")

4. list

#!/usr/bin/python
li=["dog","cat","bull","sheep","pig","chicken","horse"]
print li #prints all elements
print li[0] #print first element
print li[1] #print second element
print li[1:3] #print element which index is 1 and 2

5. dict

#!/usr/bin/python

words = {}
words["yes"] = "1"
words["no"] = "0"
words["age"] = "25"

print words["age"]
print words["no"]

print words

words["yes"] = "dui"
words["name"] = "yanxia"

del words["no"]
print words

6. string

#!/usr/bin/python

sen="this is a book"
qa="book"

if qa == sen:
	print('strings equal')
if qa in sen:
	print(qa + " found in " + sen)
str="In Python,\nyou can use special characters in strings.\nThese special characters can be..."
print(str)

7. tuple

#!/usr/bin/python

x = (2,3,4,5)
x = x + (7,8,1)
print(x)

lis = ["book","cup","pen","eraser","tea"]
tu = tuple(lis) #list->tuple
print(tu)

y = (4,5)
lis = list(y)   #tuple->list
print(lis)

animal = ("cock","rabbit","mouse","hen","fog")
str1 = ' '.join(animal)
print(str1)

per = tuple(sorted(animal))
print(per)

8. class

#!/usr/bin/python

class JustCounter:
	__secretCount = 0

	def count(self):
		self.__secretCount +=1
		print self.__secretCount

counter = JustCounter()
counter.count()
counter.count()


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值