python基础语法学习2

对于 windows : control +[ 恢复 for循环中的文字结构及其作用域

对于 mac:command +[  

while 循环 

while True:

        print("正则表达式");

for循环的使用  关于for循环的作用域

        example_list=[1,2,3,4,5,7,562,4562] 

        for i in example_list:

                print(i)

输出该数组中的数据

if  x<y>z:

        print ('x is less than y')

if x>y:

        print('x is greater than y')

elif x<z:

        print ('x')

else:

        print ('x is less or equal to y')

  python 中的函数

定义函数,函数作用域为四个空格之后

def function():

        print('this is  python')

funtion()

参数变量

def fun(a,b):

        c=a*b

        print('this is',c)

默认参数

def sale_car(price,colour='red',brand='carmy',is_second_hand=True)

        print('price',price,

        'colour',colour

        'brand',brand

        'is_second_hand',is_second_hand)

sale_car(2000,'green','奔驰',false)

要将赋予默认值的参数进行分类

全局变量&局部变量 apple则为全局变量

apple=10

a=None

def fun():

        //声明全局变量

        global a

        a=10

        print(a)

        return a+100

print(a)

print(fun())

print(a)

文件读写  /n换行

text ='this is my first test.\nthis is next line\nthis is last time'

print(text)

打开文件 /如果不存在该文件夹将创建  w(write)为写 已有文件的话进行追加则需要 a(append)

my_life =open('my life.txt','w') my life 为文件名

my_life.write(text)

my_life.close()

append_text ='\nthis is append_file'
my_life =open('my life.txt','a')

my_life.write(append_text)

my_life.close()

附加文件内容添加

读取文件

file =open('my life.txt','r')

content=file.read()

print(content)

选择excel方式读取

file =open('my life.txt','r')

content=file.readline()  //readlines()全部行数都读

//python_list=[1,2,3,4,5,6,'dashi','轩墨']

print(content)

类的定义

class Calculator:

        name='Good calculator'

        price=20

        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):

                print(x*y)

        def divide(self,x,y)

                print(x/y)

calcul=Calculator() 类的实例化方式
calcul.name
print(calcul.price)
calcul.add(45,68)
calcul.divide(9,3)

类的初始 init

class Calculator:

        name='Good calculator'

        price=20

        def _init_(self,name,price) //构造函数初始构造对象时必须输入所需参数

                self.name=name

                self.price=price

        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):

                print(x*y)

        def divide(self,x,y)

                print(x/y)

input

a_input =input('请输入一个数:')

print('this input  number is ',a_input)

元组和列表

a_tuple=(12,3,5,15,6)

another_tuple=2,4,6,7,8

a_list=[23,2,4,9,8]

for content in a_list:

        print(content)

for index in range(len(a_list)):   #len计算长度
    print('index=',index,'number in list',a_list[index])

列表

a=[1,23,45,7,7]

a.append(0)#在尾部加零

print(a)

#指定添加位置 第一个数为添加位置,第二个参数为所添加的数

a.insert(1,0)

a.remove(7) #移除第一个7

print(a[-1])打印最后一位

打印一系列的值

print(a[0:3])#打印前三位

#列表中第一次出现2的位置
print(a.index(45))

列表中出现数的次数

a.count()

排序

a.sort() 并覆盖原有的a并排序,默认为从小到大排序,添加参数a.sort(reverse=Ture)则为倒叙排序

a=[1,2,3,5,4,89,32] 多维列表 即二维数组,矩阵

a=[[1,2,3]

        [4,8,6]

        [6,9,8]]

字典

d={'apple':1,'pear':2,'orange':3}

或者 d2={1:'a','c':'b'}

print(d['apple'])

删除元素 del d['pear']

增加元素 d['b']=20 

字典内容可以是列表或者字典

d={'apple':[1,2,3],'pear':{1:3,9:'a'},'orange':3}

print(d['pear'][9])  输出为a

载入模块

import time

print(time.localtime) 打印现在时间

若只想 import 目标功能

from time import time,localtime

自己的模块

确保模块在自己写的脚本在一个目录下

其他下载的外部模块也在同一目录下

try 学习错误处理

try:

        file=open('111','r')

except Exception as e:

        print('there is no file named as eee')

        response=input('do you want to create a new file')

        if response =='y':

                file=open('111','w')

        else:

                pass

else:

        file.write('ssss')

file.close()

zip 功能 

a=[1,2,3] 

b=[4,5,6]

zip(a,b)   #将其可视化 list(zip(a,b))

输出为[(1,4),(2,5),(3,6)]

for i,j in zip(a,b):

        print(i/2,j*2)   #在其中打印zip中的运算结果

list(zip(a,a,b)) 多个元组也可以连接

fun2 =lambda  x , y :  x+y

fun2(2,3)

输出为5

map功能

map(fun1,[1],[2])

输出为3

map(fun1,[1,4],[2,8])

输出为[3,8]

copy  浅拷贝  id表明索引

deepcopy 则进行完全复制,建立新的集合

import copy
a=[1,2,[3,8]]
b=a
print(a,b)
c=copy.copy(a)

如果a[0]=111

则a输出为[111,2[3,8]] b输出为[1,2,[3,8]]

a[2][0]=333

则a输出为a=[1,2,[333,8]]

b为[1,2,[333,8]]

pickle模块

a_dict={'da':11,2:[1,8,9],'23':{1:2,'D':124}}

file=open(‘pickle_example.pickle’,'wb') #写入二进制形式打开

pickle.dump(a_dict,file)

file.close()

相当于一个推土车把字典里的数据倒入file文件中

with open('pickle_example.pickle','rb') as file:
    file=open('pickle_example.pickle','rb')
    a_dict1=pickle.load(file)

用with 结构实现自动关闭

set实现去除重复数据

char_list=['a','a','b','c','c']

print(set(char_list))

char=set(char_list)

set() 不能传入列表

加入字符串char_list.add('x')单独加一个'x'

char.add('x')

print(char_list.add)

char.remove('x')//移除x 没有x 则报错

使用char.discard('x')  //如果存在x则删除,如果没有则返回原有数据

st1=char

st2={'a','s','c'}

找出其相同的地方st1.intersection(st2)

找其不同的地方 st1.difference(st2)

正则表达式

主要用于文字处理

regular Expression

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值