PHP前端开发

关于python3学习基础知识总结

百变鹏仔 3小时前 #Python
文章标签 基础知识

一、数据类型

1、数字

  • int(整型)

  • long(长整型)

  • float(浮点型)

  • complex(复数)

2、布尔值

  •  True或False

3、字符串

二、变量

变量命名规则:

  • 变量名只能是 字母、数字或下划线的任意组合

  • 变量名的第一个字符不能是数字

  • 变量名不能为关键字(例如:and,or,continue,break,in,else,print 等)

三、字符串拼接

1、使用加号(+)

name = "Tom"age = 25print(name + "s age is " + str(age))#输出:Toms age is 25

2、字符串格式化

name = = 25( %

 ps:在python中,使用+号连接字符串,每出现一个+号,就要重新在内存中申请一块空间,有多少个+号,就要申请多少块空间。一般不建设使用+号连接字符串。

四、列表和元组

1、列表(list)

  • 创建列表

str_list = ['Tom','Lucy','Mary']或者str_list = list(['Tom','Lucy','Mary'])
  •  索引(访问列表中某一个值)

str_list[0]
  • 追加(增加元素到末尾)

str_list.append('lilei')print(str_list)#输出:['Tom', 'Lucy', 'Mary', 'lilei']
  • 插入(在指定位置加入元素)

str_list.insert(1,'lilei')print(str_list)#输出:['Tom', 'lilei', 'Lucy', 'Mary']
  •  删除(删除指定元素)

str_list.remove('Lucy')print(str_list)#输出:['Tom', 'Mary']
  •  切片

str_list = [3,4,5,6,7,8,9]new_1 = str_list[1:3]    #从索引1开始取,取到索引3new_2 = str_list[0:6:2]  #从索引0开始取,每两位一取,到第6位为止new_3 = str_list[-2:]    # 取后面2个数new_4 = str_list[:3]     # 取前面3个数new_5 = str_list[::3]    #所有数,每3个取一个print(new_1,new_2,new_3,new_4,new_5)#输出:[4, 5] [3, 5, 7] [8, 9] [3, 4, 5] [3, 6, 9]

2、元组(tuple)

  • 创建元组

age = (18,25,33)或者age = tuple((18,25,33))

 除了不能修改、增加、删除元素,其它操作元组和列表几乎一样。

 五、字典

使用key-value的存储方式

  • 创建字典

phone = {    '张三':'13075632152',    '李四':'15732015632',        '王五':'13420321523',}
  •  获取字典中key的值

print(phone['张三'])      #如果key不存在,会报错,key用中括号装print(phone.get('老黄'))  #如果key不存在,返回None,key用小括号装#输出:13075632152#     None
  •  赋值

phone[] =    phone[] =
  •  删除

phone.pop('张三')   #第一种方法del phone['李四']   #第二种方法phone.popitem()    #随机删除某一个
  • 遍历

for key in phone:    print(key,phone[key])#输出:# 王五 13420321523# 张三 13075632152# 李四 15732015632
  • 多级嵌套

phone = {    '人事部':{'老张':'13700112233','老李':'13432023152'},    '财务部':{'小丽':'13230555666','小映':'13723688888'},    '技术部':{'老罗':'13866666333'}}print(phone['人事部']['老李'])#输出:13432023152

六、if语句

1、if...else

age = 16if age <p style="text-align: left;"><span style="font-size: 15px">2、if...elif....else</span></p><pre class="brush:python;toolbar:false">score = 85if score &gt; 0 and score= 60 and score =80 and score<p style="text-align: left;"><span style="font-size: 14pt">七、<a href="http://www.php.cn/code/5908.html" target="_blank">while循环</a></span></p><pre class="brush:python;toolbar:false">i=0num=0while i<p style="text-align: left;"><span style="font-size: 14pt"> 八、for...in循环</span></p><pre class="brush:python;toolbar:false">num = []for i in range(10):    num.append(i)print(num)#输出:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

九、用户交互(input)

name = input('请输入你的名字:')height = input('请输入你的身高:')print('%s的身高%s厘米' %(name,height))

 十、文件基本操作

 打开文件:f = open('文件路径','模式')    或者   with open('文件路径','模式') as f:

模式:

  • r:以只读方式打开文件

  • w:打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。

  • a:打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。

  • w+:打开一个文件用于读写。(文件一打开就清空了,还能读到东西吗?)

  • a+:打开一个文件用于读写。

 读文件:

 read()  readlines()  readline() 的用法

f = open('d:/test.txt','r')  #以只读方式打开文件print(f.read())  #read()一次读取文件的全部内容for line in f.readlines():   #readlines()读取整个文件,并按行存进列表    print(line.strip(''))  #去掉行尾的''while 1:    line = f.readline()   #readline()每次只读取一行    print(line.strip(''))    if not line:        breakf.close()   #关闭文件

写文件:

f =open('d:/test.txt','a')f.write('hello,boy!')  f.close()