python基础知识

1.文件写入

text="this is my first test.\nthis is next line.\n this is last line"


#文件写入,打开my file.text,如果不存在就创建一个my file.text文件

my_file=open('my file.text','w')

#文件读取

my_file=open('my file.text','r')

my_file.write(text)

my_file.close()、

2.在后面添加内容

append_text="\nthis is appended file"
my_file=open('my file.text','a')
my_file.write(append_text)
my_file.close()

3.读取text

file=open('my file.text','r')
content=file.read()
print(content)


##

file=open('my file.text','r')
content=file.readline()  //如果想要读取全部内容则用content=file.readlines(),输出为列表格式,输出:['this is my first test.\n', 'this is next line.\n', ' this is last line\n', 'this is appended file']

second_read_line=file.readline()
print(content)

4 class

计算器类

class Calculator:
    price=18
    name="a good calculator"
    def add(self,x,y)://self代表类自身的属性
        print(self.name)
        result=x+y
        print (result)
    def minus(self,x,y):
        result=x-y
        print (result)
    def times(self,x,y):
        result=x*y
        print (result)
    def devide(self,x,y):
        result=x/y
        print (result)

输出:


5.类的init功能


class Calculator:
    price=18
    name="a good calculator"
    def __init__(self,name,price,height,width,weight):
        self.name=name
        self.price=price
        self.h=height
        self.wi=width

        self.add(1,2)

        self.we=weight

    def add(self,x,y):
        result=x+y
        print (result)
    def minus(self,x,y):
        result=x-y
        print (result)
    def times(self,x,y):
        result=x*y
        print (result)
    def devide(self,x,y):
        result=x/y
        print (result)

调用如下:>>> c=Calculator('gc',12,30,15,19)
>>> c.name
'gc'
>>> c=Calculator('bc',12,30,15,19)
>>> c.name
'bc'

6.input

a_input=input('please give a number:') #return a string '1'
#也可以进行类型转换   a_input=interesting(input('please give a number:'))
if a_input=='1':
    print('this is a good one')
elif a_input=='2':
    print('this is a bad one')
else:
    print('good luck')


输出:==================== RESTART: C:\Users\Desktop\test.py ====================
please give a number:1
this is a good one
>>> 
==================== RESTART: C:\Users\Desktop\test.py ====================
please give a number:2
this is a bad one

7.元祖列表

# tuple list
a_tuple=(12,3,5,15,6)
another_tuple=2,4,6,7,8
a_list=[12,3,67,7,82]
for content in a_list :
    print(content)
for content in a_tuple :
    print(content)
for index in range(len(a_list)):
     print('index=',index,'number in list=',a_list[index])
for index in range(len(a_list)):
     print('index=',index,'number in list=',a_list[index])

输出:

12
3
67
7
82
12
3
5
15
6
index= 0 number in list= 12
index= 1 number in list= 3
index= 2 number in list= 67
index= 3 number in list= 7
index= 4 number in list= 82
index= 0 number in list= 12
index= 1 number in list= 3
index= 2 number in list= 67
index= 3 number in list= 7
index= 4 number in list= 82

列表的相关操作:

a=[1,2,3,4,5,1,1]
a.append(0)
print(a)
a.insert(1,0)
print(a)
a.remove(1)
print(a)
print(a[-1])
print(a[0:3])
print(a[2:3])
print(a.index(2))#2出现的索引
print(a.count(1))#计算1出现的次数
a.sort(reverse=True)#默认从小到大,reverse一下就是反向
print(a)

>>> 
==================== RESTART: C:\Users\Desktop\test.py ====================
[1, 2, 3, 4, 5, 1, 1, 0]
[1, 0, 2, 3, 4, 5, 1, 1, 0]
[0, 2, 3, 4, 5, 1, 1, 0]
0
[0, 2, 3]
[3]
1
2
[5, 4, 3, 2, 1, 1, 0, 0]
>>> 

8.多维列表(可以用numpy)

a=[1,2,3,4,5]
multi_dim_a=[ [1,2,3],
                       [2,3,4],
                       [3,4,5]]
print(a[1])
print(multi_dim_a[0][1])

9.字典

d={'apple':1,'pear':2,'orange':3}
d2={1:'a','c':'d'}
print(d['apple'])
del d['pear']
print(d)
d['b']=20
print(d)
#字典里面可以加字典。也可以有列表,也可以是一个值
d={'apple':[1,2,3],'pear':{1:3,3:'a'},'orange':2}
print(d['pear'][3])

输出:

1
{'orange': 3, 'apple': 1}
{'orange': 3, 'b': 20, 'apple': 1}
a

9.improt 模块

from time import time,localtime#import time 里面所有的功能 from time import *
print(localtime())
print(time())

输出:time.struct_time(tm_year=2017, tm_mon=4, tm_mday=19, tm_hour=14, tm_min=15, tm_sec=27, tm_wday=2, tm_yday=109, tm_isdst=0)
1492582527.5835907


10.自己的模块(如果将自己的模块放入python当地模块的库中则也可以调用m1)

在同一个目录下创建两个.py文件

在m1.py文件中写入

def printdata(data):
    print(data)

在test.py文件中调用:

import m1
m1.printdata('i am test')


11.continue&break

a=True
while a:
    b=input('type something')
    if b=='1':
        break
    else:
        pass
    print('still run')


print('finish')
输出
type something211
still run
type something4
still run
type something1
finish

###

a=True
while a:
    b=input('type something')
    if b=='1':
        continue
    else:
        pass
    print('still run')

输出:

type something1
type something65
still run
type somethingf
still run
type something


12.错误处理try

try:
    file=open('eeee','r+')#r表示只读,r+表示只读+写入
except Exception as e:
    print(e)
    response= input('do you want to create a new file')
    if response=='y':
        file=open('eeee','w')
    else:
        pass
else:
    file.write('ssss')
file.close()

这个程序运行的结果就是会产生一个eeee的txt并在其中写入ssss


13.zip,lambda,map

>>> a=[1,2,3]
>>> b=[4,5,6]
>>> zip(a,b)
<zip object at 0x0000019261C99DC8>
>>> list(zip(a,b))
[(1, 4), (2, 5), (3, 6)]
>>> for i,j in zip(a,b):
print(i/2,j*2)



0.5 8
1.0 10
1.5 12
>>> list(zip(a,a,b))
[(1, 1, 4), (2, 2, 5), (3, 3, 6)]
>>> def fun1(x,y):
return(x+y)


>>> fun1(2,3)
5
>>> fun2=lambda x,y:x+y
>>> fun2(2,3)
5
>>> map(fun1,[1[,[2])
 
SyntaxError: invalid syntax
>>> map(fun1,[1],[2])
<map object at 0x0000019261CADC18>
>>> list( map(fun1,[1],[2]))
[3]
>>> list( map(fun1,[1,3],[2,5]))
[3, 8]

14.copy&deepcopy

>>> import copy
>>> a=[1,2,3]
>>> b=a
>>> id(a)
1728216714248
>>> id(b)
1728216714248
>>> b[0]=11
>>> a
[11, 2, 3]
>>> a[1]=22
>>> b
[11, 22, 3]
>>> print(id(a)==id(b))
True
>>> c=copy.copy(a)
>>> print(id(a)==id(c))
False
>>> c[1]=22222
>>> a
[11, 22, 3]
>>> c
[11, 22222, 3]
>>> a=[1,2,[3,4]]
>>> d=copy.copy(a)
>>> id(a)==id(d)
False
>>> id(a[2])==id(d[2])
True
>>> a[0]=11
>>> d
[1, 2, [3, 4]]
>>> a[2][0]=33
>>> d
[1, 2, [33, 4]]
>>> e=copy.deepcopy(a)
>>> id(e[2])==id(a[2])
False

b=a时,b指向的空间就是a的空间

浅复制第一层列表的id是不一样的,但是第二层列表的id是一样的

深复制是完全不一样的。


15.threading

16.多核运算muitiprocessing

17.tkinter:pyhton特定的gui界面,是一个图像窗口

18.picker保存和提取历史文件的功能

import pickle
a_dict={'da':111,2:[23,1,4],'23':{1:2,'d':'sad'}}
file=open('pickle_example.pickle','wb')
pickle.dump(a_dict,file)
file.close()


file=open('pickle_example.pickle','rb')#可以换成with open('pickle_example.pickle','rb') as file:这时候就不需要考虑file关闭的问题。省去file.close()

a_dict1=pickle.load(file)
file.close()
print(a_dict1)

19.set找不同

char_list=['a','b','c','c','d','d']
sentence='welcome to seu'#大小写也会有区分
print(set(sentence))
print(set(char_list))
print(type(set(char_list)))
unique_char=set(char_list)
unique_char.add('x')#也不能加list
print(unique_char)
unique_char.remove('x')#剪掉不存在的东西就会报错
unique_char.discard('y')#剪掉不存在的东西不会报错
#unique_char.clear()#清空
print(unique_char)


set1=unique_char
set2={'a','e','i'}
print(set1.difference(set2))
print(set1.intersection(set2))

输出:

{'c', 'o', 'e', 'm', ' ', 'u', 'w', 'l', 's', 't'}
{'c', 'b', 'a', 'd'}
<class 'set'>
{'c', 'b', 'a', 'x', 'd'}
{'c', 'b', 'a', 'd'}
{'c', 'b', 'd'}
{'a'}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值