Python基础

一. Python快速入门

  1. 安装Anaconda,编译环境Jupyter(http://localhost:8888/tree)。
  2. 变量类型
days=365
print (365)
print (days)
print ('hello python')

365
365
hello python
str_test = "China"
int_test = 123
float_test = 122.5
print(str_test)
print(int_test)
print(float_test)
print(type(str_test))
print(type(int_test))
print(type(float_test))

China
123
122.5
<class 'str'>
<class 'int'>
<class 'float'>
  1. List基础
    3.1数据类型转换
str_eight = str(8)
print(type(str_eight))

str_eight = '8'
int_eight = int(str_eight)
print(type(int_eight))

<class 'str'>
<class 'int'>

3.2 符号
Addition:+
Subtraction:-
Multiplication:*
Division:/
Exponent:**(平方)

china  = 10
united_states = 100
china_plus_10 = china + 10
us_times_100 = united_states * 100
print(china_plus_10)
print(us_times_100)
print(china**2)

20
10000
100

3.3 数组LIST

#LIST
months = []
print(type(months))
print(months)
months.append("January")
months.append("February")
months.append(1)
months.append("2")
print(months)

<class 'list'>
[]
['January', 'February', 1, '2']

3.4 数组索引

countries = []
temperatures = []

countries.append("China")
countries.append("India")
countries.append("US")

temperatures.append(30.5)
temperatures.append(20.5)
temperatures.append(10.5)

china = countries[0]
china_temperature = temperatures[0]
print(china)
print(china_temperature)

China
30.5
int_month = [1,2,3,4,5,6,7,8,9,10,11,12]
length = len(int_month)
print(length)

12
int_month = [1,2,3,4,5,6,7,8,9,10,11,12]
length = len(int_month)#int_month的长度
print(length)

#获取最后一个元素
index = len(int_month)-1
last_value = int_month[index]
print(last_value)

print(int_month[-1])#获取最后一个元素
print(int_month[-2])#获取倒数第二个元素

12
12
12
11

切片

months = ["Jan","Feb","Mar","Apr","May","Jun","Jul"]
two_four = months[2:4]
print(two_four)

three_month = months[3:]
print(three_month)

['Mar', 'Apr']
['Apr', 'May', 'Jun', 'Jul']
  1. 循环
cities = ["Austin","Dallas","Houston"]
for city in cities:
    print(city)

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

print("-------------")
    
for i in range(10):
    print(i)


Austin
Dallas
Houston
-------------
0
1
2
-------------
0
1
2
3
4
5
6
7
8
9
cities = [["Austin","Dallas","Hoston"],["Haerbin","Shanghai","Beijing"]]
for city in cities:
    print(city)
    
for city in cities:
    for i in city:
        print(i)

['Austin', 'Dallas', 'Hoston']
['Haerbin', 'Shanghai', 'Beijing']
Austin
Dallas
Hoston
Haerbin
Shanghai
Beijing
  1. 判断结构
#Booleans
cat = True
dog = False
print(type(cat))

<class 'bool'>

print (8 == 8)
print (8 != 8)
print (10 == 8)
print (10 != 8)

print ("8" == "8")
print (['Austin', 'Dallas', 'Hoston'] == ['Austin', 'Dallas', 'Hoston'])
print (5.0 == 5.0)

True
False
False
True
True
True
True
rates = [10,15,20]
print(rates[0] > rates[1])
print(rates[1] >= rates[0])

False
True
sample_rate = 700
greater = (sample_rate > 5)
if greater:
    print(sample_rate)
else:
    print("less than")

700
t = True
f = False

if t:
    print("Now you see me")
if f:
    print("Now you don't see me")
  1. 字典
#传统方法
students = ["Tom","Jim","Sue","Ann"]
scores = [70,80,90,100]
indexes = [0,1,2,3]
name = "Sue"
score = 0
for i in indexes:
    if name==students[i]:
        score=scores[i]
print(score)

90
#字典解决
scores = {}
print (type(scores))
scores["Tom"] = 70
scores["Jim"] = 80
scores["Sue"] = 90
scores["Ann"] = 100
print(scores.keys())
print(scores)
print(scores["Sue"])

<class 'dict'>
dict_keys(['Tom', 'Jim', 'Sue', 'Ann'])
{'Tom': 70, 'Jim': 80, 'Sue': 90, 'Ann': 100}
90
#另一种书写
students = {}
students ["Tom"] = 70
students ["Jim"] = 80

print (students)

students = {
    "Tom":70,
    "Jim":80
}
print(students)

{'Tom': 70, 'Jim': 80}
{'Tom': 70, 'Jim': 80}
#字典相加
students = {
    "Tom":70,
    "Jim":80
}
students ["Tom"]=75
print(students)
students ["Tom"]=students ["Tom"]+5
print(students)

{'Tom': 75, 'Jim': 80}
{'Tom': 80, 'Jim': 80}
#字典判断
stuents = {
    "Tom":70,
    "Jim":80
}
print("Tom" in students)

True
pantry = ["apple","orange","grape","orange","apple","tomato","potato","grape"]
pantry_counts = {}

for i in pantry :
    if i in pantry_counts:
        pantry_counts[i] = pantry_counts[i]+1
    else:
        pantry_counts[i] = 1
print(pantry_counts)

{'apple': 2, 'orange': 2, 'grape': 2, 'tomato': 1, 'potato': 1}
  1. 文件
#读文件
f = open("Demo1.rtf","r")
g = f.read()
print(g)
f.close()
#写文件
f = open("Demo_write.txt","w")
f.write("123456")
f.write("\n")
f.write("Hello Python")
f.close()
weather_data = []
f = open("weather.csv","r")
data = f.read()
rows = data.split("\n")
for row in rows:
    split_row = row.split(",")
    weather_data.append(split_row)
print(weather_data)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值