由于一般的脚本可以用shell完成,但是高级一点,复杂一点的需要用python编写,所以复习一下大学学的python基础语法吧。
六个标准的数据类型:
- Numbers(数字)
- String(字符串)
- List(列表)
- Tuple(元组)
- Sets(集合)
- Dictionaries(字典)
Numbers(数字)
Python 3 支持 int(整型)、float(浮点型)、bool(布尔型)、complex(复数)。
String(字符串)
Python 中的字符串 str 用单引号(' ')或双引号 (" ") 括起来
List(列表)
List(列表) 是 Python 中使用最频繁的数据类型。
>>> a = ['him', 25, 100, 'her']
>>> print(a)
['him', 25, 100, 'her']
Tuple(元组)
元组(tuple)与列表类似,不同之处在于元组的元素不能修改。元组写在小括号里,元素之间用逗号隔开。
元组中的元素类型也可以不相同:
>>> a = (1991, 2014, 'physics', 'math')
>>> print(a, type(a), len(a))
(1991, 2014, 'physics', 'math') <class 'tuple'> 4
Sets(集合)
集合(set)是一个无序不重复元素的集。
>>> student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'}
>>> print(student) # 重复的元素被自动去掉
{'Jim', 'Jack', 'Mary', 'Tom', 'Rose'}
>>> 'Rose' in student # membership testing(成员测试)
True
条件控制
if
if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3
for
#!/usr/bin/env python3
edibles = ["ham", "spam","eggs","nuts"]
for food in edibles:
if food == "spam":
print("No more spam please!")
break
print("Great, delicious " + food)
else:
print("I am so glad: No spam!")
print("Finally, I finished stuffing myself")
while
while 判断条件:
statements
range
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
... print(i, a[i])
...
0 Mary
1 had
2 a
3 little
4 lamb
函数
def 函数名(参数列表):
函数体
def hello() :
print("Hello World!")
hello()
Hello World!
def area(width, height):
return width * height
def print_welcome(name):
print("Welcome", name)
print_welcome("Fred")
w = 4
h = 5
print("width =", w, " height =", h, " area =", area(w, h))
列表
>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print(a.count(333), a.count(66.25), a.count('x'))
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]
输入和输出
面向对象
#!/usr/bin/python3
#类定义
class people:
#定义基本属性
name = ''
age = 0
#定义私有属性,私有属性在类外部无法直接进行访问
__weight = 0
#定义构造方法
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s 说: 我 %d 岁。" %(self.name,self.age))
# 实例化类
p = people('W3Cschool',10,30)
p.speak()
多线程
#!/usr/bin/python3
import _thread
import time
# 为线程定义一个函数
def print_time( threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print ("%s: %s" % ( threadName, time.ctime(time.time()) ))
# 创建两个线程
try:
_thread.start_new_thread( print_time, ("Thread-1", 2, ) )
_thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
print ("Error: 无法启动线程")
while 1:
pass
时间
#!/usr/bin/python3
import time; # 引入time模块
ticks = time.time()
print ("当前时间戳为:", ticks)
import time
# 格式化成2020-03-20 11:45:39形式
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
# 格式化成Sat Mar 28 22:24:24 2020形式
print (time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))
# 将格式字符串转换为时间戳
a = "Sat Mar 28 22:24:24 2020"
print (time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y")))