python常用知识点_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"

a1d8fd9fd74b388ea8f32a380c18fad1.png

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"

6830abf87987faaf6897372306dd5907.png

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")

4e6fedc2f8384eb0aa3b3fdc8e4f57e6.png

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

eefdd9e19be08615890ea2e4ae3a0a50.png

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

361761a9e251654ad98d1182090d98ff.png

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)

9619e7504679ad8dc7ca41da193d801b.png

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)

99d1c2cb85b35835b55bf6fd078774a0.png

8. class

#!/usr/bin/python

class JustCounter:

__secretCount = 0

def count(self):

self.__secretCount +=1

print self.__secretCount

counter = JustCounter()

counter.count()

counter.count()

70cd43a536ba9c91c4671f869841a716.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值