一、基本数据类型
与大多数语言一样,Python有许多基本类型,也叫python内置的数据类型,包括整数,浮点数,布尔值和字符串。这些数据类型的行为方式与其他编程语言相似。
Numbers(数字类型)
代表的是整数和浮点数,它原理与其他语言相同:
x = 3
print(type(x)) # Prints "<class '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 "<class 'float'>"
print(y, y + 1, y * 2, y ** 2) # Prints "2.5 3.5 5.0 6.25"
注意,与许多语言不同,Python没有一元增量(x+
)或递减(x-
)运算符。
(Python还有用于复数的内置类型)
Booleans(布尔类型)
Python实现了所有常用的布尔逻辑运算符,但它使用的是英文单词而不是符号 (&&
, ||
, etc.):
t = True
f = False
print(type(t)) # Prints "<class '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"
Strings(字符串类型)
1)字符串合并
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 # way1: String concatenation
print(hw) # prints "hello world"
hw12 = '%s %s %d' % (hello, world, 12) # way2: sprintf style string formatting
print(hw12) # prints "hello world 12"
2)字符串大小写处理 && 特定处理
s = "hello"
print(s.capitalize()) # Capitalize a string; prints "Hello"
print(s.upper()) # Convert a string to uppercase; prints "HELLO"
print(s.rjust(7)) # Right-justify a string, padding with spaces; prints " hello"
print(s.center(7)) # Center a string, padding with spaces; prints " hello "
print(s.replace('l', '(ell)')) # Replace all instances of one substring with another;
# prints "he(ell)(ell)o"
print(' world '.strip()) # Strip leading and trailing whitespace; prints "world" 去掉空格
3)字符串特定取值[:]
>>> o_img = '0.bmp'
>>> print(o_img[:-4]) # 从第一个到倒数第四个
0
>>> print(o_img[-1:]) # 从倒数第一个到末尾
p
>>> print(o_img[:-1]) # 从第一个到倒数第一个
0.bm
>>> print(o_img[-2:]) # 从倒数第二个到末尾
mp
4)将逗号隔开的一串str变为list,变为numpy类型
strip()用于移除首尾空格
二、函数(Functions)
1、使用def关键字定义
Python函数使用def
关键字定义。例如:
def sign(x):
if x > 0:
return 'positive'
elif x < 0:
return 'negative'
else:
return 'zero'
for x in [-1, 0, 1]:
print(sign(x))
# Prints "negative", "zero", "positive"
我们经常定义函数来获取可选的关键字参数,如下所示:
def hello(name, loud=False):
if loud:
print('HELLO, %s!' % name.upper())
else:
print('Hello, %s' % name)
hello('Bob') # Prints "Hello, Bob"
hello('Fred', loud=True) # Prints "HELLO, FRED!"
2、匿名函数lambda
没有具体名称的函数,它允许快速定义单行函数
lambda的语法为:
lambda [arg1 [, agr2,.....argn]] : expression
例子:
1、单个参数:
>>> g = lambda x : x ** 2
>>> print g(3)
9
2、多个参数:
>>> g = lambda x, y, z : (x + y) ** z
>>> print g(1,2,2)
9
三、类(Classes)
在Python中定义类的语法很简单:
class Greeter(object):
# Constructor
def __init__(self, name):
self.name = name # Create an instance variable
# Instance method
def greet(self, loud=False):
if loud:
print('HELLO, %s!' % self.name.upper())
else:
print('Hello, %s' % self.name)
g = Greeter('Fred') # Construct an instance of the Greeter class
g.greet() # Call an instance method; prints "Hello, Fred"
g.greet(loud=True) # Call an instance method; prints "HELLO, FRED!"
参考文章
【1】https://www.numpy.org.cn/article/basics/python_numpy_tutorial.html#python