python快速入门

环境 python3

类型转换

int(str(8))
float
int
str

变量类型

type(var) 

运算:

num=10
print (num**2) # 100

list使用:

months=[]
months.append('ONE')
months.append('TWO')
months.append('THREE')
print (months)
print (months[0]) #索引取值
print (len(months)) #长度
print (months[len(months)-1]) #取最后一个元素
print (months[-2]) #索引倒着取
print (months[0:2]) #从0取到1索引
print (months[0:]) #从0取到最后

['ONE', 'TWO', 'THREE']
ONE
3
THREE
TWO
['ONE', 'TWO']
['ONE', 'TWO', 'THREE']

循环

#while循环
citys=['beijing','hfei','jinan','shanghai']
i=0
while i < 3:
    i +=1;
    print(i)

#for循环
citys=['beijing','hfei','jinan','shanghai']
for city in citys :
    print(city)

#for循环
citys=['beijing','hfei','jinan','shanghai']
for i in range(len(citys)) :
    print(citys[i])

#for循环
citys=[['beijing','hfei','jinan','shanghai'],['beijing','hfei','jinan','shanghai']]
for city in citys :
    for c in city :
        print(c)

boolean类型

#boolean类型
blo = True
bfs = False
print(blo)
print(bfs)
print(9==5)
print("9"=="9")

if判断

#if判断 在python非0的值都为真
if 1:
    print(1)
else:
    print(2)

字典

#字典结构
scores = {}
scores['jim1']=80
scores['jim2']=90
scores['jim3']=92
print(scores)
print(scores['jim1'])
print(scores.keys())
print(scores.values())
print('jim3' in scores)

文件操作

#文件操作
f = open("test.txt","r")
g = f.read()
print(g)
f.close()

f2 = open("test.txt","w")
f2.write('你好 file')
f2.write('\n')
f2.close()

函数

#函数
def printhello(a):
    print('hello'+a)
    
printhello(' world')

#函数 参数为函数对象
def funback(printhello):
    printhello
    
funback(printhello(' world'))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值