python note1.简单定义,数据类型

print 'hello'
hello

字符串定义

总共79个字符

# 三个引号
'''hello world'''
'hello world'
# \n 表示空格
"""hello world\nhello world"""
'hello world\nhello world'
"""
hello world
hello world
"""
'\nhello world\nhello world\n'
print("""hello world\nhello world""")
hello world
hello world
'hello
world'
  File "<ipython-input-6-bfed5f26aacb>", line 1
    'hello
         ^
SyntaxError: EOL while scanning string literal
#加个\
'hello\
world'
'helloworld'

转义字符

\n 换行

' 单引号

\t 横向制表

print('hello \\n world')#输出\n
hello \n world
print('c:\northwind\northwest')
c:
orthwind
orthwest
print('c:\\northwind\\northwest')
c:\northwind\northwest
print(r'c:\northwind\northwest')# r 小写r表示原始字符串,所见即所得 R也可以
c:\northwind\northwest

合并字符串、保留字符串中某个字符=字符串的运算

字符串的运算

'hello ' + 'world'
'hello world'
'hello'*3
'hellohellohello'
'hello world'[0]
'h'
'hello world'[3]
'l'
'hello world'[-1]# [-n]从后往前数
'd'
#作业题目 获取w
'hello world'[-5]
'w'
'hello world'[6]
'w'
'hello world'[0:-1]# 步长
'hello worl'
'hello world'[0:5]
'hello'
#作业截取world
'hello world'[6:11]
'world'
'hello world'[-5:11]
'world'
 'hello world'[6:20]#等同于6-11
'world'
'hello world'[6:]
'world'
'hello world'[-5:]
'world'

Python的基本数据类型

组的概念

type([1,2,3,4,5,6])#列表
list
#二维数组
type([[1,2],[3,4],[True,False]])
list
['2','1','3','4'][0]
'2'
['新月打击','苍白之瀑','月之降临','月神冲刺'][1]
'\xe8\x8b\x8d\xe7\x99\xbd\xe4\xb9\x8b\xe7\x80\x91'
(1,2,3,4,5)#元组
(1, 2, 3, 4, 5)
(1,2,3,4,5)[0]
1

列表可以+不可以-

列表与元组是有区别的
type((1,2,3,4,5))
tuple
#奇怪的现象 非元组类型
type(('hello'))
str
type((1))
int
#理解 数学符号运算优先而不是tuple
(1+1)
2

如何定义只有一个元素的元祖

type((1,))
tuple
#空的
type(())
tuple
type((,))
  File "<ipython-input-19-ff592953e249>", line 1
    type((,))
          ^
SyntaxError: invalid syntax

str list tuple 都是序列

序列的共有操作

#e.g.
'hello world'[3]
'l'
#切片
[1,2,3,4][1:2]
[2]
#3个数字切片
"hello world"[1:3:5]
'e'

posted @ 2019-09-05 21:19  摘不完的老花生  阅读(149)  评论(0编辑  收藏  举报