首页 > 代码库 > python学习之路

python学习之路

数据类型

1.数字  不加引号
Int 整型 就是正负整数
Long 长整型 比整数长的,基本就是无限大 python3长度到了会自动化为long 基本就没有long一说了,都是巨大的整型了
Float  浮点型  大约的理解为就是小数  占用空间大  尽量不用浮点数
Complex 复数


2.布尔 真或假 true false
     1或0

3.字符串 加引号
字符串的常用功能
移除空白
长度
索引
切片
分割
格式化输出
具体如下
##1.移除空白
Name.strip()
>>> name = "yangqiqi "
>>> name.strip()
‘yangqiqi‘
>>> name = "yangqiqi   \n"
>>> name.strip()
‘yangqiqi‘  ##不仅仅脱空白,换行也会去掉
>>>
##2.长度
len()
>>> name = "yangqiqi"
>>> len(name)
8
>>> name = "yangqiqi   "
>>> len(name)
11
##3.索引
从0开始
>>> name = "yangqiqi"
>>> name
‘yangqiqi‘
>>> name[0]
‘y‘
>>> name[1]
‘a‘
##4.切片
yang qi qi
0123456789
>>> name = "yang qi qii"
>>> name
‘yang qi qii‘
>>> name[0:4] ##最后的g对应的数字是3,但是切片顾头不顾尾,所以要往后多数一位
‘yang‘
##5.格式化输出
%s 占位符
>>> name = "yangqiqi"
>>> age = 23
>>> msg = "my name is %s ,and I am %s years old!!!" % (name,age)
>>> msg
‘my name is yangqiqi ,and I am 23 years old!!!‘

decode 编译  解读 解码

4.列表
字符串不能修改,除非重新赋值,是不一样的概念,虽然现在懵懵懂懂的。
插入开头 如何插入 可以
列表里可以有重复的吗   允许
如果有重名的 names.remove(qiqi)的时候都删除吗   应该是删除第一个
names.append 可以同时插入多个吗  不可以
列表取数的时候 如果取隔开的 如何取到
如何批量修改部分值


需求: 存入全班的学生的名字想到如下的方法,看似不错的样子哦!!

>>> names = "yangbinghai  anergai yanglongyan  yanglongxia  yangqiqi"
>>> names
‘yangbinghai  anergai yanglongyan  yanglongxia  yangqiqi‘

需求:把 anergai 取出来 (采用索引的方法取了出来),看似不错的样子哦!!
>>> names[13:20]
‘anergai‘
>>>

需求:班级现在有100个学生,把yangqiqi取出来....数的头都大了

>>> names = "yangbinghai  anergai yanglongyan  yanglongxia  yangqiqi ..."
列表驾到 []中括号 不再是双引号
##列表规定以及取某个值
>>> names = ["yangbinghai","anergai","yanglongyan","yanglongxia","yangqiqi"]
>>> names
[‘yangbinghai‘, ‘anergai‘, ‘yanglongyan‘, ‘yanglongxia‘, ‘yangqiqi‘]
>>> names[0]
‘yangbinghai‘
>>> names[1]
‘anergai‘
>>> names[3]
‘yanglongxia‘
>>> names[-1]
‘yangqiqi‘
>>> names[-2]
‘yanglongxia‘
不再是取出单个字符,是以,为分割的一个组合
>>>

##修改某个值
>>> names[1]
‘anergai‘
>>> names[1] = "haomama"  字符串添加双引号,别总是忘记添加,形成良好的习惯。
>>> names
[‘yangbinghai‘, ‘haomama‘, ‘yanglongyan‘, ‘yanglongxia‘, ‘yangqiqi‘]
>>> names[1]
‘haomama‘
>>>

##增加值
##1.追加到末尾
##2.插入到指定位置
##3.加到开头,或者前面
**append
英 [?‘pend]  美 [?‘p?nd]
vt. 附加;贴上;盖章

#1.追加到末尾
>>> names
[‘yangbinghai‘, ‘haomama‘, ‘yanglongyan‘, ‘yanglongxia‘, ‘yangqiqi‘]
>>> names.append("kongzixuan")
>>> names
[‘yangbinghai‘, ‘haomama‘, ‘yanglongyan‘, ‘yanglongxia‘, ‘yangqiqi‘, ‘kongzixuan‘]
#2.将anwei插入到yangbinghai之后
###索引是从0开始,注意以下的数字,都是多数一个
>>> names.insert(1,"anwei")
>>> names
[‘yangbinghai‘, ‘anwei‘, ‘haomama‘, ‘anwei‘, ‘yanglongyan‘, ‘yanglongxia‘, ‘yangqiqi‘, ‘kongzixuan‘]
#3.加到开头,也是查到前面
>>> names.insert(0,"anwei")
>>> names
[‘anwei‘, ‘yangbinghai‘, ‘anwei‘, ‘haomama‘, ‘anwei‘, ‘yanglongyan‘, ‘yanglongxia‘, ‘yangqiqi‘, ‘kongzixuan‘]
>>>

##删除某个值
>>> names
[‘anwei‘, ‘yangbinghai‘, ‘anjunfeng‘, ‘anwei‘, ‘haomama‘, ‘anwei‘, ‘yanglongyan‘, ‘yanglongxia‘, ‘yangqiqi‘, ‘kongzixuan‘]
>>> del names[2]
>>> names
[‘anwei‘, ‘yangbinghai‘, ‘anwei‘, ‘haomama‘, ‘anwei‘, ‘yanglongyan‘, ‘yanglongxia‘, ‘yangqiqi‘, ‘kongzixuan‘]
>>> names.remove("anwei")
>>> names
[‘yangbinghai‘, ‘anwei‘, ‘haomama‘, ‘anwei‘, ‘yanglongyan‘, ‘yanglongxia‘, ‘yangqiqi‘, ‘kongzixuan‘]
重复的删除第一个
>>> names
[‘yangbinghai‘, ‘haomama‘, ‘anwei‘, ‘anwei‘, ‘yanglongyan‘, ‘yanglongxia‘, ‘yangqiqi‘, ‘kongzixuan‘]
>>> names.remove("anwei")
>>> names
[‘yangbinghai‘, ‘haomama‘, ‘anwei‘, ‘yanglongyan‘, ‘yanglongxia‘, ‘yangqiqi‘, ‘kongzixuan‘]
>>>

##索引
#多个命令合为一个命令
索引又被称为下标
>>> names
[‘xiaokong‘, ‘dakong‘, ‘wobuhao‘, ‘nihao‘, ‘danni‘, ‘xiaoni‘]
>>> names.index("nihao")
3
>>> names[3]="wobuhao"
>>> names
[‘xiaokong‘, ‘dakong‘, ‘wobuhao‘, ‘wobuhao‘, ‘danni‘, ‘xiaoni‘]
直接找出索引并修改
>>> names[names.index("wobuhao")]="nihao"
>>> names
[‘xiaokong‘, ‘dakong‘, ‘nihao‘, ‘wobuhao‘, ‘danni‘, ‘xiaoni‘]
##统计
>>> names
[‘xiaokong‘, ‘dakong‘, ‘nihao‘, ‘wobuhao‘, ‘danni‘, ‘xiaoni‘]
>>> names.append("yangqiqi")
>>> names.append("yangqiqi")
>>> names.append("yangqiqi")
>>> names.append("yangqiqi")
>>> names.append("yangqiqi")
>>> names
[‘xiaokong‘, ‘dakong‘, ‘nihao‘, ‘wobuhao‘, ‘danni‘, ‘xiaoni‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘]
>>> names.count("yangqiqi")
5
##排序
sort 排序
默认按照ascii排序
>>> names
[‘!!‘, ‘23‘, ‘55‘, ‘NIHAO‘, ‘dakong‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘]
>>> names.sort() 从小到大
>>> names
[‘!!‘, ‘23‘, ‘55‘, ‘NIHAO‘, ‘dakong‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘]
>>>
>>> names.reverse() 从大到小
>>> names
[‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘dakong‘, ‘NIHAO‘, ‘55‘, ‘23‘, ‘!!‘]
##切片
>>> names
[‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘xiaoni‘, ‘xiaokong‘, ‘wobuhao‘, ‘nihao‘, ‘danni‘, ‘dakong‘, ‘NIHAO‘, ‘55‘, ‘23‘, ‘!!‘]
>>> names[0:3] ##正常的这种,就是顾头不顾尾
[‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘]
>>> names[-1:-3] ##这种反着来的,从左到右,所以得先写-3
[]
>>> names[-3:-1]##反着来的顾尾不顾头
[‘55‘, ‘23‘]

############无论是什么顺序的,顾头不顾尾也好,顾尾不顾头也好,记住顺序总是从0开始,从左到右,数的时候从1开始数就好了
>>> names[0:-1]  取出所有的
[‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘xiaoni‘, ‘xiaokong‘, ‘wobuhao‘, ‘nihao‘, ‘danni‘, ‘dakong‘, ‘NIHAO‘, ‘55‘, ‘23‘]
>>> names[:-1] 0可以省略
[‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘yangqiqi‘, ‘xiaoni‘, ‘xiaokong‘, ‘wobuhao‘, ‘nihao‘, ‘danni‘, ‘dakong‘, ‘NIHAO‘, ‘55‘, ‘23‘]

>>> names
[‘yangqiqi‘, ‘xiaoni‘, ‘xiaokong‘, ‘wobuhao‘, ‘nihao‘, ‘danni‘, ‘dakong‘, ‘NIHAO‘, ‘55‘, ‘23‘, ‘!!‘]
>>>
>>>
>>> names[:-1]
[‘yangqiqi‘, ‘xiaoni‘, ‘xiaokong‘, ‘wobuhao‘, ‘nihao‘, ‘danni‘, ‘dakong‘, ‘NIHAO‘, ‘55‘, ‘23‘]
按照步长取出数值
>>> names[:-1:2] 后边的2就是步长的意思,从yangqiqi开始走两步,就好了nihao,所以接下来显示的就是nihao,以此类推。
2恰好也是取出奇数行
[‘yangqiqi‘, ‘xiaokong‘, ‘nihao‘, ‘dakong‘, ‘55‘]
>>> names[:-1:3]
[‘yangqiqi‘, ‘wobuhao‘, ‘dakong‘, ‘23‘]
>>> names[:-1:4]
[‘yangqiqi‘, ‘nihao‘, ‘55‘]


########运算符##########

1.算数运算
加 +
减 -
乘 *
除 /
求余 %
求幂 **
取整除 //


2.比较运算

等于 ==
不等于 !=或者<> 变态<>在python3里不存在了
大于 <
小于 <
大于等于 >=
小于等于 <=

>>> count = 1
>>> count
1
>>> count = count +1
>>> count
2
>>> count +=1 这样简写
>>> count
3
>>>


3.赋值运算

简单的赋值运算 =
加法赋值运算  +=
-+
*=
/=
%=
幂赋值运算  **=
取整除赋值运算 //=

4.逻辑运算
and
or
not 非

5.成员运算
in
not in
判断在不在一个列表里
>>> name = [1,2,3,4,5]
>>> name
[1, 2, 3, 4, 5]
>>> 1 in name
True
>>> 6 in name
False
>>> 6 not in name
True
>>>

6.身份运算
is
is not

7.位运算
&
|
^
~
<<
>>

#_*_ coding:utf-8 _*_
#username = input("username:")  单行注释
‘‘‘password=input("password:")‘‘‘ 多行注释
print(username,password)

在python中 单引号和双引号没有区别

三个单引号不仅仅可以做注释,多行的时候也需要 用到
#_*_ coding:utf-8 _*_

info = ‘‘‘---- personal info----
name:
age:
job:
like:
‘‘‘

print(info)
E:\>python  passwd.py
---- personal info----
name:
age:
job:
like:

交互输入
#_*_ coding:utf-8 _*_

name = input("name:")
age = input("age:")
job = input("job:")
like = input("like:")

info = ‘‘‘---- personal info----
name:%s
age: %s
job: %s
like: %s
‘‘‘ % (name,age,job,like)
print(info)

E:\>python  passwd.py
name:yangqiqi
age:23
job:linux
like:happy
---- personal info----
name:yangqiqi
age: 23
job: linux
like: happy



python学习之路