python使用缩进来控制代码块,不需要大括号。
例如
if True:
print("True")
else:
print("False")
运行方法:
python3 文件名.py
linux 下脚本顶部添加如下代码
#! /usr/bin/python3
可以以shell方式运行
./文件名.py
数字基本数据类型:
int, bool, float, complex(复数)
字符串
单引号和双引号一样
连接用加号
截取字符串方法:字符串变量[头下标:尾下标:步长]
三个引号包括多行字符串
列表List
截取列表跟截取字符串格式一样。
list[1:4] #获取第二个、第三个、第四个
list[2:] #获取第三个到最后一个
元组 Tuple
元组跟列表类似,但是元组的元素不能修改,元组写在小括号()里,元素用逗号隔开。元素类型可以不同。
tupe = ('a', 1, 2, 'b')
集合 Set
使用大括号{}。
sites = {'aaa', 'bbb', 'ccc'}
if 'pgres' in sites:
print("ok")
else :
print("ng")
字典 dict
dic = {'name': 'long', 'age':40}
print(dic['name'])
print(dic.keys()) #输出所有键
print(dic.values()) #输出所有值
循环
i=1
while i<10:
print(i)
i++
ls = ['a', 'b', 'c']
for item in ls:
print(item)
定义函数
def hello(name) :
print(name)
定义类
class WebSite:
name=''
class Pgres(WebSite):
def __init__(self):
print('init')
def say():
print("hello")
nb = Pgres()
nb.say()