没有合适的资源?快使用搜索试试~ 我知道了~
python cheat sheet
需积分: 9 6 下载量 97 浏览量
2013-03-12
07:31:05
上传
评论
收藏 58KB PDF 举报
温馨提示
试读
3页
python cheat sheet include lots of command quick look
资源推荐
资源详情
资源评论
Python Basics
Whitespace matters! Your code will not run correctly if you use improper indentation.
#this is a comment
Basic Python Logic
if:
if test:
#do stuff if test is true
elif test 2:
#do stuff if test2 is true
else:
#do stuff if both tests are false
while:
while test:
#keep doing stuff until
#test is false
for:
for x in aSequence:
#do stuff for each member of aSequence
#for example, each item in a list, each
#character in a string, etc.
for x in range(10):
#do stuff 10 times (0 through 9)
for x in range(5,10):
#do stuff 5 times (5 through 9)
Python Strings
A string is a sequence of characters, usually used to store text.
creation:! the_string = “Hello World!”
! ! the_string = ‘Hello World!’
accessing:! the_string[4]! ! returns ‘o’
splitting:! the_string.split(‘ ‘)! returns [‘Hello’, ‘World!’]
! ! the_string.split(‘r”)! returns [‘Hello Wo’, ‘ld!’]
To join a list of strings together, call join() as a method of the string you want to separate the values in the list (‘’ if
none), and pass the list as an argument. Yes, it’s weird.
words = [“this”, ‘is’, ‘a’, ‘list’, ‘of’, “strings”]
‘ ‘.join(words)! ! returns “This is a list of strings”
‘ZOOL’.join(words)! returns “ThisZOOLisZOOLaZOOLlistZOOLofZOOLstrings”
‘’.join(words)! ! returns “Thisisalistofstrings”
String Formatting: similar to printf() in C, uses the % operator to add elements of a tuple into a string
this_string = “there”
print “Hello %s!”%this_string! returns “Hello there!”
Python Tuples
A tuple consists of a number of values separated by commas. They are useful for ordered pairs and returning several
values from a function.
creation: emptyTuple = ()
singleItemTuple = (“spam”,) note the comma!
thistuple = 12, 89, ‘a’
thistuple = (12, 89, ‘a’)
accessing: thistuple[0] returns 12
资源评论
Kola
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功