Python学习笔记1
python 课程
docker
ansible salt openstack
安装python3 和
pycharm
day1
一.变量赋值
name = “123”
num = 123
name是字符串,num是数字,不加引号时会当成变量
起名需要显示,通俗易懂;numsofalexgf
写法 nums_of_alex_gf;NumsOfAlesGf
不合法:数字不能开头;特殊字符不能有;空格不能有;某些关键字不能为变量,python自己保留的
查看变量的内存地址:
>>> a = 3
>>> b = a
>>> a,b
(3, 3)
>>> id(a)
9079904
>>> id(b)
9079904
>>> a = 5
>>> id(a)
9079968
>>>
这个id不是物理内存地址,是python中的内存地址
二.用户交互
>>> name = input(“please input your name:”)
please input your name:zyd
>>> name
‘zyd’
>>>
三.条件判断与缩进
if…else和缩进
sex = input(“input your gender:”)
if sex == “girl”:
print(“like”)
elif sex == “man”:
print(“hmoesexaul”)
else:
print(“pervert!”)
用缩进来确定作用域,所以必须要写缩进(强制缩进)
缩进出错报错:
File “if.py”, line 5
print(“like”)
^
IndentationError: unindent does not match any outer indentation level
同一级别缩进要保持一致
四.循环控制
python3以上 input默认是字符串
number = 6
while True:
number2 = int(input(“input the num:”))
if number2 < number:
print(“你打印大一点”)
elif number2 > number:
print(“你打印小一点”)
else:
print(“你打印正好”)
break
while 循环 break 打破循环
优化
number = 6
number2 = 1
while number2 != number:
number2 = int(input(“input the num:”))if number2 < number:
print(“你打印大一点”)
elif number2 > number:
print(“你打印小一点”)
print(“你打印正好”)
次数限制:
number = 6
number2 = 1
retry_count = 0
while number2 != number and retry_count < 3:
number2 = int(input(“input the num:”))
if number2 < number:
print(“你打印大一点”)
elif number2 > number:
print(“你打印小一点”)
retry_count += 1
if number == number2:
print(“你打印正好”)
else:
print(“too many trys…”)
最终版:
number = 6
number2 = 1
retry_count = 0
while retry_count < 3:
number2 = int(input(“input the num:”))
if number2 < number:
print(“你打印大一点”)
elif number2 > number:
print(“你打印小一点”)
else:
print(“你打印正好”)
break
retry_count += 1
else:
print(“too many trys…”)
for循环
number = 6
number2 = 1
retry_count = 0
for i in range(3):
number2 = int(input(“input the num:”))
if number2 < number:
print(“你打印大一点”)
elif number2 > number:
print(“你打印小一点”)
else:
print(“你打印正好”)
break
else:
print(“too many trys…”)
五.数据类型
数字
int 整形
float 浮点
long 长整形
布尔
真或假
1或0
字符串(python中单引号和双引号没什么区别,三个单(双)引号有用处)
“hello world”
格式化:
例1
name = input(“name:”)
age = input(“age:”)
job = input(“job:”)
print(“Infomation of []:”+ name +”\nName:[]”+ name +”\nAge:[]”+age+”\nJob:[]”+job+””) #每一个字符串一个空间
print(“Information of %s:\nName:%s\nAge:%s\nJob:%s” %(name,name,age,job)) #所有字符串单独一个空间
例2
name = input(“name:”)
age = input(“age:”)
job = input(“job:”)
msg = ”’
Information of %s:
Name:%s
Age:%s
Job:%s
”’ %(name,name,age,job)
print(msg)
改字符串的话,就需要再次开辟空间,万恶的+号每出现一次就会在内存中重新开辟一块空间。
字符串格式化
name = “Zz”
print(“i am %s” %(name))
#输出:i am Zz
%d:数字 %f:浮点
字符串的常用功能
- 移除空白 .strip()
-
-
-
-
- name = input(“name:”).strip() #(中间的空格是去不掉的,是去前后的东西,比如strip(z)就是把前后的z都去掉 )
age = input(“age:”)
job = input(“job:”)
msg = ”’
Information of %s:
Name:%s
Age:%s
Job:%s
”’ %(name,name,age,job)
print(msg)
列表
>>> name_list = [‘Alex’,’34boys’,’tenglan’]
>>> name_list[2]
‘tenglan’
>>> name_list[0]
‘Alex’
好处的可以存多个信息,在一个变量里存取多个信息
基本操作
dir(name_list)
[‘__add__’, ‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__delitem__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__gt__’, ‘__hash__’, ‘__iadd__’, ‘__imul__’, ‘__init__’, ‘__init_subclass__’, ‘__iter__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__mul__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__reversed__’, ‘__rmul__’, ‘__setattr__’, ‘__setitem__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘append’, ‘clear’, ‘copy’, ‘count’, ‘extend’, ‘index’, ‘insert’, ‘pop’, ‘remove’, ‘reverse’, ‘sort’]
这样可以查看功能,有双下划线的可以忽略
- 索引 name_list[2] 其中的2就是索引值,又称下标,从0开始
- 切片 a[0:2] a[1:5] a[1:5:2](跳着切) a[-2:]取后面的两个 a[:3]取前面的几个 a.extend(name) extend是拆开成每个字符
- 追加 name_list.append(“091334s”) 追加在最后,而且元素可以重复
- 插入 name_list.insert(2,”66kdkdkd”) 在第三个元素后插入
- 删除 name_list.pop() 删除最后一个元素 name_list.remove(’66kdkdkd’) 删除66kdkdkd这个元素
- 长度
- 循环
- 包含
- 反转和排序 name_list.reverse() name_list.sort() 按照ascli排序
批量删除:
>>> name_list
[’34boys’, ’34boys’, ‘Alex’, ’34boys’, ‘tenglan’]
>>> for i in range(name_list.count(’34boys’)):
… name_list.remove(“34boys”)
…
>>> name_list
[‘Alex’, ‘tenglan’]
>>>
判断包含(是否在里面)
>>> a = [1, 4, 5, 7, ‘hg’, ‘u’, 8]
>>> 4 in a
True
>>> 9 in a
False
>>>
经常会用到如下:
>>> if 4 in a:
… print(“111”)
…
111
>>>
元祖(和列表的区别,元祖是只读)
>>> a = (1,2,3,4) #是圆括号
>>> a
(1, 2, 3, 4)
>>> dir(a)
[‘__add__’, ‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__getnewargs__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__iter__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__mul__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__rmul__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘count’, ‘index’]
>>>
只有 ‘count’, ‘index’两个功能
存在的意义就是让人不要改
列表和元祖可以互转list 改成列表 tuple改成元祖
字典
算术运算符
加减乘除略去
** 幂
// 整除
例如:
>>> 5/2
2.5
>>> 5//2
2
>>>
%= 取模赋值运算符(可用于前端区分行)
位运算符
按位与运算符
>>> 10&50
2
按位或运算符
>>> 10|50
58
按位异或运算符
>>> 10^50
56
按位取反运算符(忽略)
左移动运算符
右移动运算符
>>> 10<<1
20
>>> 10>>1
5
>>>
逻辑运算符
and
>>> if sex == ‘m’ and age > 25:
… print (“dsf”)
…
dsf
>>>
not
>>> names
[‘abc’, ‘touch’]
>>> if ‘mark’ not in names:
… print(“mark not in”)
…
mark not in
>>>
成员运算符
身份运算符
>>> type(names) is list
True
>>> type(3) is int
True
>>> 3is not int
True
>>> 3 is not int
True
>>>
简单的嵌套循环
continue
如:
for i in range(10):
if i < 5:
continue
print (i)
结果:56789
所以continue是跳出本次循环,继续下次循环
两层嵌套
for j in range(5):
for i in range(10):
if i < 5:
continue
print (i)
打印5遍56789
for j in range(5):
for i in range(10):
if i < 5:
continue
if i > 8:
break
print(i)
打印5遍5678
for j in range(5):
for i in range(10):
if i < 5:
continue
if j > 3:
break
print(i)
打印4次56789(bread跳出当前的循环)
文件操作
当心覆盖
写的不错哦!