PYTHON基础

# 1 Variables

days_1 = 365
days_2 = 366

print('string')
print(days_1)
print(days_2)

str_test = 'China'
int_test = 123
float_test = 123.45

print(type(str_test))
print(str_test)
print(type(int_test))
print(int_test)
print(type(float_test))
print(float_test)

# 2 type convert
eight = 8
str_eight = str(eight)
print(str_eight)
print(type(str_eight))

eight_str = "8"
int_eight = int(eight_str)
print(int_eight)
print(type(int_eight))

# 3 List
months = []
months2 = [1,2,3,4]
months.append(1)
months.append("January")
months.append(2)
months.append("February")
print(months)
print(type(months))

m1 = months[0]
m3 = months[1]
mlast = months[-1]
mcols = months[1:4]
mcols2 = months[1:]
print(m1)
print(m3)
print(mlast)
print(mcols)
print(mcols2)

int_months = len(months)
print(int_months)

# 4 Circulate
cities = ["Austin", "Dallas", "Houston"]
for city in cities:
    print(city)

i = 0
while i < 3:
    i += 1
    print(i)

for i in range(10):
    print(i)
    
cities2 = [["Austin", "Dallas", "Houston"],["Austin2", "Dallas2", "Houston2"]]
for i in cities2:
    for j in i:
        print(j)

# 5 Booleans
cat = True
dog = False

print(8 == 8) #True
print(8 != 8) #False
print(["January","Febuary"] == ["January","Febuary"])

# 6 If statements
sample_rate = 700
greater = (sample_rate > 5)
if greater:
    print(sample_rate)
else:
    print("less than")
        

# 7 Find a value
animals = ["dog","cat","rabbit"]
if "cat" in animals:
    print("cat Found")
    
res = "cat" in animals
print(res)

# 8 Dicrionaries
# before
students = ["Tom","Jim","Sue","Ann"]
scores = [70,80,85,75]
indexes = [0,1,2,3]
name = "Sue"
score = 0
for i in indexes:
    if students[i] == name:
        score = scores[i]
print(score)


# after
# scores = {}
# scores["Jim"] = 80
# scores["Sue"] = 85
# scores["Ann"] = 75
scores = {
    "Tom":60,
    "Sue":85,
    "Ann":75
}
print(scores.keys())
print(scores)
print(scores["Sue"])
print("Tom" in scores)

# 9 File
f = open("D:/temp/test.txt","r")
g = f.read()
print(g)
f.close()

f = open("D:/temp/test.txt","w")
f.write("123321")
f.write("\n")
f.close()

weather_data = []
f = open("D:/temp/featureX.dat","r",encoding="utf-8")
data = f.read()
rows = data.split("\n")
for row in rows:
    split_row = row.split(",")
    weather_data.append(split_row)
print(weather_data)
f.close()

# 10 Function
def printHello():
    print ("hellp python")

def add(a,b):
    return a+b

printHello()
print(add(1,3))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值