python常用基础

1、type函数:

>>> days = 365
>>> print(days, type(days))
365 <class 'int'>
>>>    

2、类型转换:str、int、float函数

>>> str_eight = str(8)
>>> float_eight = float("8")
>>> int_eight = int("8")
>>> print(type(str_eight), type(float_eight), type(int_eight))
<class 'str'> <class 'float'> <class 'int'>
>>>     

3、加减乘除、乘方
+ - * /  、**

>>> a = 1+2**4/2-1*2
>>> print(a)
7.0
>>> 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)
...
a
b
c
>>>    

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也被删除
遍历字典:

>>> d = {'Python':'astonishing', 'C++':'complicated', 'Java':'versatile'}
>>> for key in d:
...     print(key + ':' + d[key])
...
Python:astonishing
C++:complicated
Java:versatile

10.1 创建字典的6种方式

方式 1:

>>> dict1 = {}
>>> dict1['firstname'] = 'ma'
>>> dict1['lastname'] = 'yun'

方式 2:

>>> dict1 = {'firstname':'ma', 'lastname':'yun'}

方式 3:

>>> dict1 = dict(firstname = 'ma', lastname = 'yun')

方式 4:

>>> dict1 = dict([('firstname','ma'), ('lastname', 'yun')])

方式 5:

>>> dict1 = dict((['firstname','ma'], ['lastname', 'yun']))

方式6:

>>> dict1 = dict(zip(['firstname', 'lastname'], ['ma', 'yun']))

应用1:

根据一个list生成一个字典。list中的元素有重复。要求生成的字典的key为list中的元素,value为该元素的重复个数;
alist=['a', 'b', 'b', 'c', 'c', 'c']

    方法一: (python3.5之后)
            >>> from collections import Counter      //统计库
            >>> dict(Counter(alist))
            {'a': 1, 'b': 2, 'c': 3}

     方法二:
            >>> key_list = list(set(alist))    //去重
            >>> key_list
            ['b', 'c', 'a']

key_list = list(set(alist))    //去重
value_list = []

for i in range(alist):
    value_list[i] = alist.count(key_list[i])  //计数

adict = dict(zip(key_list, value_list))  //将两个列表打包成一对一元组列表,再转换成字典
adist

{'a': 1, 'b': 2, 'c': 3}

 

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文件:
如何写excel文件? 。。。有待尝试。

12、定义函数:

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值