3.关于变量

一、变量命名规定:
1.不能以数字和特殊字符(@,#,¥,%,空格,等)开头。
2.不能以关键字开头。比如print,if,else等。
 
二、缩进:指定控制块
  可以缩两个,但是必须上下级保持一致,python规定标准缩四个空格(一个tab)。

三、以下几个常用的重要的运算符:  
1.算数运算符: +      -        *        /
2.赋值运算符: =    +=     -=      *=       /=
        i=0
        i=+1 ————> i=i+1
        print(i)
3.比较运算符: ==       >=   <=    !=    >    <    (得到一个布尔值,用来判断)
4.逻辑运算符: and       or   not (与,或,非)
5.关系运算符: in, not in 
6.--位运算符:--  (很少用到)
 
四、数据类型:
加深记忆:https://www.processon.com/mindmap/5909a1d8e4b05b005ea0e8f5
1.因为字符的表现形式有很多种(长的短的胖的瘦的高的矮的),所以会有数据类型的区分。
所有的数据类型都分为:
(1).不可变数据类型:(一旦创建不能修改。)
   整形,元祖,字符串,
(2).可变数据类型:
    列表,字典
2.数据类型有以下几种:
(1).整形:长整形(py2里分整形长整形,py3里统一长整形)。
(2).浮点型:就是小数,包括 float类型和double(精度比较高)类型。
(3).布尔型: false(0)和ture(1)——> 可以进行运算。
         false+1=1 ture+1=2
(4).字符串(string):
    它的创建方式:1.表达式创建 s=1
            2.关键字创建 s=str(1),s=list(1,2,3),s=[1,2,3]
 
五、  \   ——>     为转义符号(将有意义的字符转成普通字符)   s='let\'s go' == s="let's go"
                         print(\\) 将\转义 , print(r"\sdf"sdf'sdf\ffd")将整个字符串全部转成普通字符。
 
六、对字符串的增删改查:
1.
[::]   查找   切片功能。
     a=("hello world")
     print(s[2:5:2]) #从索引值第2个每隔2个字符切到第5个,第5个不取。
      (默认隔一个,倒数第一个为-1 但是不取)。
2.
strip()
把字符串里面从开头和结尾的(空格,\n 回车,tab)去掉,不包括中间的。
使用情况:(在对文本处理时,因为空格,\n,tab在文本里看不见,处理时会匹配不到,所以必须先进行一个去除)。
3.
join()  拼接功能。

  “a”+“b” 使用就两个字符拼接。
  “任何字符”.join(["i","love","you"]) 多个字符串时效率高。
4.
split  将字符串分割成列表。

   s="hello world".split(" ")按空格分  [hello world]
   s="hello world".split("e")按e分  ['h', 'llo world']
   s="hello world".split("l")按l分     ['he', '', 'o wor', 'd']
5. 
find     查找字符,返回索引值。
  print("hello world".find("l"))   2  默认从左开始找。
   print("hello world".find("l",3)) 3  从第3个字符开始找索引值最小的那个l。
   print("hello world".find("l",4)) 9  从第4个字符开始找索引值最小的那个l。
   print("hello world".rfind("l"))  9  从右边开始找。  
   print("hello world".rfind("a"))  -1  没有这个字符会返回一个  -1。
6.
index    跟find相似,都是查找返回索引值的意思。
  print("hello world".index(" "))  5   查找空格的索引值。
   print("hello world".index("b"))  报错   没有这个字符会报错。
7. 
replace  替换字符,是一种完全匹配,必须一对一的去匹配。
   print("hello world".replace("world","python"))  hello python  
8. swapace   字符串内的小写换大写,大写换小写
   print("HELLO WorlD".swapcase())     hello wORLd  。
9.   
center    居中显示   下面显示的是一共多少个字节。
ljust     居左显示 
rjust     居右显示
     print("hello world".center(30,"*"))   *********hello world**********
     print("hello world".ljust(30,"*"))   hello world*******************
     print("hello world".rjust(30,"*"))   *******************hello world
 
10. 字符串的格式化输出。
 
 %s:字符串    %d:整形    %f:浮点型
 
     print("hello %s %s"%("world","good"))                     hello world good
     print("hello %s %d"%("world",35))           %d可换成%s    hello world 35
     print("hello %s %f"%("world",35.1234567))   %f可换成%s    hello world 35.1234567
     print("hello %s %.4f"%("world",35.1234567))  取小数点后4位        hello world 35.1234
 
format  推荐使用这种方法。
 
     print("hello{0},thank{1}".format("world","you"))                        helloworld,thankyou
     print("hello{name},thank{number}".format(name="world",number=2017))    helloworld,thank2017
     print("hello{name},thank{number}".format(name="world",number=2017))
   
 
format_map   这种方法使用了字典,看着好麻烦,其实使用起来也好麻烦。
 
      print("hello{name},thank{number}".format_map({"name":"world","number":2017}))

   判断是不是数字 
   print("12".isdecimal())   ture
   print("一".isdecimal())   false
   print("12".isdigit())   ture
   print("一".isdigit())   false
   print("12".isnumeric())   ture
   print("一".isnumeric())   true
   print("壹".isnumeric())   ture

  对单词字母操作 
   print("hello world".capitalize())   Hello world  第一个单词首字母大写
   print("hello world".title())  Hello World  每个单词首字母大写
   print("HELLO world".lower())  hello world  所有字母转成小写
   print("HELLO world".upper())  HELLO WORLD  所有字母转成大写
   print("hello\tworld".expandtabs(20))  hello               world  对tab的扩展  
   print("hello\nworld\n".splitlines()) ['hello', 'world'] 以\n 分割成列表,最后一个直接去掉
  
列表  可迭代对象:能进行for循环
 
 l=[1,"hello",[4,5],{"name":"tom"}]  []里可以是字符串,列表,字典

列表的增删改查:
  查:切片 [:]

 增加:
  
   l=[1,2,3,333,444,555,343434]
   l.append("liu")    在列表最后只添加一个字符
   l.extend(["de","hua"])  在列表最后添加多个
   l.insert(3,999)     指定位置添加   在索引值3前面添加999元素
 删除:
  知道具体位置的删除: pop删除特点会返回所删除的对象的值
   l.pop() 默认删除最后一个
   l.pop(3)删除下标为3的字符
  不知道位置的删除:
   l.remove(333)
  强删:
  del.l[0] 删除索引值0的元素     del l 删除列表l
 改:
  l=[1,2,3,[333,444],555,343434]
  l[3][1]="fei"   将索引值3的列表元素中的索引值为1的元素改为fei
  print(l) ----->  [1, 2, 3, [333, 'fei'], 555, 343434]
列表的统计:
  print(len(l)) 统计列表中元素的个数 
列表中元素的排序:
 数字:按数字大小排序
  l=[9,5,2,7,4,]
  l.sort()
  print(l)      
 字符串:根据内置所对应的数字大小进行排序
 
 l.reverse()  直接倒着反转排序

 

字典    python中唯一具有映射关系的数据类型,字典的查询效率高于列表,但是存储数据较大。
  a={1:"feifeifei","name":"biubiubiu"}
  字典中键必须唯一切不可变,所以键不能为列表,字典
  而value可以为任何数值
  字典是无序的
字典查操作:
 对字典的取值:
  a={"name":"111","age":"222"}
  print(a["age"])      如果取不到会报错。
  print(a.get("names"))   如果取不到会返回一个None值。
  if False:     如果上面没取到执行下面print
  print("没有此内容")  
 字典的遍历:
 for i in a:
  print(i)   name age  只是打印出对应的key值
  print("%s:%s"%(i,a[i]))   name:111  age:222   
  
for循环:
 for i in [44,33,22,55,66,]:  --->for i in range(5)  当不对列表里数值进行操作时可用range代替
    print("ok")  按照列表里的数值个数  打印5次ok,与i和列表的数值无关。
    print(i) 依次打印列表里的数值
while循环:
  i=1
  while i<=10:
   print(i)
   i+=1
 也可以用for循环表达:
  for i in range(1,11):  1到11取值  取10次,写11是因为左取右不取。
   print(i)

 增:
 d={1:"fei","name":"tom"}
 d["age"]=30
 print(d)     {1: 'fei', 'name': 'tom', 'age': 30}
 这种方式也是修改的方式
 update  更新    也是增加的一种方式
 
 
 删除:
 d.pop(1)   会有一个返回值,删除的values
 del
 
 
set  集合: 去重, 关系测试(交集,并集等等)
   属于可变数据类型,但是集合内元素一定是不可变数据类型。
  去重:
    s={1,2,3,5,2,1,}
    print(set(s))     {1, 2, 3, 5}  数字去重
    s="hello"
    print(set(s))  {'h', 'l', 'e', 'o'}   字符串去重
  关系测试:
   union   并集
    s={"name",333,("a","b")}
    d={"name",999,("a","c")}
    print(s.union(d))   {'name', 999, 333, ('a', 'b'), ('a', 'c')}
    print(d.union(s))   {'name', 999, 333, ('a', 'b'), ('a', 'c')}
    print(s|d)     {'name', 999, 333, ('a', 'b'), ('a', 'c')}
   intersection 交集:
    print(s.intersection(d))
    print(d.intersection(s))
    print(s&d)     {'name'}
   difference  差集:
    print(s.difference(d))  {333, ('a', 'b')}
    print(s-d)
    
    print(d.difference(s))  {('a', 'c'), 999}
    print(d-s)
   symmetric_difference 对称差集  去掉交集就是
   print(s.symmetric_difference(d))
   print(d.symmetric_difference(s))    
   print(s^d)    {999, 333, ('a', 'b'), ('a', 'c')}
 元组 类似列表,是只读的,不应该被修改。
   
   list(tuple)  变成列表
   tuple(list)  变成元组
 
 重点的字符串方法:

  strip()
  " ".join(["I","am","world!"])
  "hello world".split("l",1)
  "hello world".index("q")
  s.replace("world","Python")
  print("hello %s,%s"%("sb","egon"))
  print("hello {name}, his age is {age}".format(age=30,name="wusir"))
  print("一".isdigit())
练习:
1.列表:
l=[1,2,3,"a","b","c"]
#顾头不顾尾特点
#[::]切片
print(l[1:4:1])#步长为1进行取值
print(l[1:4])#默认步长为1
print(l[1:4:2])#隔一个取一个值
print(l[::-1])#从末尾取到开始,显示为反着的列表
print(l[5:0:-1])#从末尾取到0位置值,0不取
print(l[-1:0:-1])#从最后一个值取到原来列表的第一个值但是不取这个值
#pop删除:
print(l.pop())#默认删除最后一个值并返回这个值
print(l)#此时列表改变成新的列表
print(l.pop(3))#指定删除索引值为3的元素
print(l)#列表变成新的列表
# print(l.pop("a"))#pop的参数必须为索引值
# remove删除
print(l.remove(3))#删除指定元素,但是没有返回值
print(l.remove(4))#不能添加索引值删除,报错
print(l)#列表变为新的列表
# clear
print(l.clear())#清空列表,没有返回值
print(l)#此时列表被清空成空列表

#增加
t=l.append("")#默认在列表末尾添加,没有返回值
t1=l.insert(0,"d")#在索引值为0的元素前插入
l.insert(13,"p")#在末尾后添加p元素,只要13大于列表长度就会添加到末尾
l.insert(10,"u")#同上,注:先后顺序,不会添加到p前面
print(len(l))
print(l)
#改
l[0]="x"
t=l.extend([1,2,3,4]) #接收一个可迭代对象
# 以上三个方法都没有返回值,只对l本身进行修改。

#查
a=l.index("c")#参数必须为元素,有返回值
l.index(4)#会报错
print(a) #index里面是元素,并返回一个索引值,不要弄反了;
l.count(2)#count统计  有返回值
print(a)

  

posted @ 2017-04-04 00:29  im777  阅读(258)  评论(0编辑  收藏  举报