Python 学习笔记

2017-01-20  本文已影响0人  JinkwanZau

Python 学习笔记

Python 基础语法

Python是一种面向对象解释型计算机编程语言,语法简洁凝练,强制使用空格键作为语句缩进放弃大括号吧,同时再此基础上使得代码结构更易于理解和阅读。
同时Python也是一门高级动态语言,代码运行时才会进行类型检查,这一特点和静态类型语言有明显区分。

Python 基本类型

Numbers

类型
运算法则
注意

例如:

>>>tax = 12.5 / 100
>>>price = 100.50
>>>price * tax
12.5625
>>>price + _
113.0625
>>>round(_, 2)
113.06
>>>-3**2
-9

Strings

类型
>>>'doesn\'t'
"doesn't"
>>>"\"Yes,\" he said."
'"Yes," he said.'
>>>'"Isn\'t," she said.'
'"Isn\'t," she said.'
函数
>>>print('"Isn\'t," she said.')
"Isn't," she said.
>>>s = 'First line.\nSecond line.'
>>>s
'First line.\nSecond line.'
>>>print(s)
First line.
Second line.

你也可以通过参数r来强制整个字符串转义,例如:

>>>print('C:\some\name')
C:\some
ame
>>>print(r'C:\some\name')
C:\some\name
特性
>>>3 * 'un' + 'ium'
'unununium'
>>>'Py' 'thon'
'Python'
>>>"py" 'thon'
'python'
>>>prefix = 'Py'
>>>prefix 'thon'
    ...
SyntaxError: invalid syntax
>>>('un' * 3)'ium'
    ...
SyntaxError: invalid syntax
>>>prefix + 'thon'
'Python'
>>>word = 'Python'
>>>word[0]
'P'
>>>word[5]
'n'
>>>word[2:5]
'tho'
>>>word[:2]
'Py'
>>>word[-2:]
'on'
>>>word[2:42]
'thon'

Lists

Python 数组类型

上一篇 下一篇

猜你喜欢

热点阅读