
python 基本语法快速预览
基
本数据
类
型
数值类型
不过需要注意的是,Python并没有x++或者x--这样的自增或者自减操作符。另外,Python内置的也
提供了长整型与其他复杂数值类型的整合,可以参考这里。
布尔类型
Python提供了常见的逻辑操作符,不过需要注意的是Python中并没有使用&&、||等,而是直接使用了英
文单词。
字符串
Python对于字符串的支持还是很好的,不过需要注意到utf-8编码问题。
x = 3
print type(x) # Prints "<type 'int'>"
print x # Prints "3"
print x + 1 # Addition; prints "4"
print x - 1 # Subtraction; prints "2"
print x * 2 # Multiplication; prints "6"
print x ** 2 # Exponentiation; prints "9"
x += 1
print x # Prints "4"
x *= 2
print x # Prints "8"
y = 2.5
print type(y) # Prints "<type 'float'>"
print y, y + 1, y * 2, y ** 2 # Prints "2.5 3.5 5.0 6.25"
t = True
f = False
print type(t) # Prints "<type 'bool'>"
print t and f # Logical AND; prints "False"
print t or f # Logical OR; prints "True"
print not t # Logical NOT; prints "False"
print t != f # Logical XOR; prints "True"
hello = 'hello' # String literals can use single quotes
world = "world" # or double quotes; it does not matter.
print hello # Prints "hello"
print len(hello) # String length; prints "5"
hw = hello + ' ' + world # String concatenation
print hw # prints "hello world"
hw12 = '%s %s %d' % (hello, world, 12) # sprintf style string formatting
print hw12 # prints "hello world 12"
评论0