1、type函数:
days = 365
print(days, type(days))

2、类型转换:str、int、float函数
str_eight = str(8)
float_eight = float("8")
int_eight = int("8")

3、加减乘除、乘方
+ - * /  、**
print(10**2)结果为100

4、list类型
months = []
print(type(months)结果为 ‘list’
months.append("January")
months.extend("February", 3)//注意:extend与append的区别就是extend可以同时添加多个元素
months.append(3)//可加入任意类型 months.append(4.0)//可加入任意类型months.append(5.0) //可加入任意类型print(months)
删除list中元素:
del months[4]
months.pop() //删除最后一个元素其它
添加list和删除list元素的方法(append、insert、extand、del、pop、remove):
https://www.cnblogs.com/chaofn/p/4592258.html

5、list索引和长度
one = months[0]
two = months[1]
print(len(months))结果为4
last_value = months[len(months) - 1]
或者
last_value = months[-1]
打印切片(取头不取尾):
print(months[2:4])//取得index为2和3的值
print(months[2:])//取得index为2开始后面所有值
print(months[:2])//取得index从头到index为2(不含2)所有值

6、循环:
for item in months:
print(item)
while i < 3:
print(i)
i += 1
for i in range(0, 10):
print(i)

7、list结果中的元素仍可以为list:
cities = [["a", "b"], ["c"]]
for i in cities:
for j in i:
print j

8、判断条件 之 bool类型:Ture / Flase

9、在list中找一个值是否存在:
if “hello”in  list:
print("yes!")
aa = hello in list
print(type(aa))结果为 bool类型

10、字典:
scores = {}  #key : value
scores["a"] = 80
scores["b"] = 90
print(scores.keys()) //可以得到所有keys,结果像list结构,但不同于list结构
结果:dict_keys(['a', 'b'])
print(scores) 
结果:{"b": 90, "a" : 80}
print(scores["a"])
键在字典中是否存在:
if 'b' in scores:
print("yes!")
删除字段中元素:
scores.pop("a")  #对应的value也被删除
应用1:
根据一个list生成一个字典。list中的元素有重复。要求生成的字典的key为list中的元素,value为该元素的重复个数;

11、文件操作
f = open("a.txt", "r")
text = f.read()
f.close()
print(text)

f = open("a.txt", "w")
f.write("123456\n")
f.close()

读取excel文件:
python常用基础python常用基础如何写excel文件? 。。。有待尝试。

12、定义函数:
python常用基础

相关文章:

  • 2021-05-18
  • 2021-07-06
  • 2021-07-02
  • 2021-07-30
  • 2021-06-03
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
猜你喜欢
  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-25
相关资源
相似解决方案