码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础day-3

时间:2017-06-09 00:54:19      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:打印   中间   比较   第一个   add   while   可变   显示   blog   

变量的组成:

  变量由变量名,赋值符号,变量值三部分组成,变量还分类型.

            变量名→n = 1←变量值

            ↑

        赋值符号

比较方法:

  身份比较:比较的是变量值的id

>>> x = 300
>>> id(x)
46137936
>>> y = 300
>>> id(y)
46137984
>>> x is y
False
>>>

  值比较:比较两个变量值

>>> x = 300
>>> y = 300
>>> id(x)
54002288
>>> id(y)
54002352
>>> x == y
True
>>>

  类型比较:比较两个变量的类型

>>> x = 300
>>> y = 300
>>> type(x)
<class int>
>>> type(y)
<class int>
>>> type(x) is type(y)
True
>>>

变量赋值操作(补充):

  x = 1   #就是一个赋值操作 没有任何返回值
 
  链式赋值:
    a=b=c=10
 
  多元赋值:
    xy的值互换
    x = 1
    y = 2
    咸鱼换法:
    z = x
    x = y
    y = z
    大神换法:
    python中: x,y=y,x
    示例:
>>> x = 1
>>> y = 2
>>> x,y = y,x
>>> x
2
>>> y
1
>>>

   增量赋值:

    x = 10
    x +=1    #+-*/都行
 
  解压序列类型:
    s = ‘hello‘
    咸鱼法:
    a = s[0]
    b = s[1]
    ...
    大神法:
    a,b,c,d,e=s
    只打印出 对应的  a 和 e的值,使用‘_‘来填充中间的变量, ‘_‘约定俗称代表丢弃的变量
    a,_,_,_,e = s
    a,*_,e    #使用*_ 代表中间全部
>>> s = hello
>>> a,b,c,d,e = s
>>> a
h
>>> b
e
>>> c
l
>>> d
l
>>> e
o
>>> a,*_,b = s
>>> a
h
>>> b
o
>>>

 分类:

  一:可变不可变
    可变:列表,字典
    不可变:数字,字符串,元组

  二:存放值的个数
    一个值:数字,字符串
    多个值(容器类型):列表,元组,字典

  三:取值方式
    直接取值:数字
    序列类型:字符串,元组,列表
    映射类型:字典

可变或者不可变的定义:在id和type不变的情况下,值可变的数据是可变类型.id,type,value都不可变的是不可变类型

列表list:

  特性:list中的元素可以随意删除增加且list的id和type不会改变,ist是一种可变类型.列表中的所有元素可以是 int 可以是 str 也可以是list(子列表).list中的值是有序的.

  定义一个list:

    l = [1,‘abc‘,[3,‘a‘]]

  取值: 通过索引取值

    print(l[0],l[2][0])

  循环: liang

#! /usr/bin/env python
# -*- coding: utf-8 -*-
l=[1,2,3,[4,5]]

count=0
while count < len(l):
    print(l[count])
    count+=1

l=[1,2,3,[4,5]]
for count in range(len(l)):
    if type(l[count]) is list:
        for i in range(len(l[count])):
            print(l[count][i])
    else:
        print(l[count])

 

   列表常用操作: 

    切片:

    l = [‘hello‘,12,‘abc‘,343,‘ppp‘]
    print(l[1:3])

D:\Python\Python36-32\python.exe E:/Python/123.py
[12, ‘abc‘]

Process finished with exit code 0

 

    追加:

    l = [‘hello‘,12,‘abc‘,343,‘ppp‘]
    l.append(‘add_1‘)
    l.append(‘add_2‘)
    l.append(‘add_3‘)
    print(l)

D:\Python\Python36-32\python.exe E:/Python/123.py
[hello, 12, abc, 343, ppp, add_1, add_2, add_3]

Process finished with exit code 0

    插入:

    l = [‘hello‘,12,‘abc‘,343,‘ppp‘]
    l.insert(0,‘insert_1‘)
    print(l)
    l.insert(2,‘insert_2‘)
    print(l)
    l.insert(4,‘insert_3‘)
    print(l)

D:\Python\Python36-32\python.exe E:/Python/123.py
[insert_1, hello, 12, abc, 343, ppp]
[insert_1, hello, insert_2, 12, abc, 343, ppp]
[insert_1, hello, insert_2, 12, insert_3, abc, 343, ppp]

Process finished with exit code 0

    删除:

    l = [‘hello‘,12,‘abc‘,343,‘ppp‘]
    print(l)
    l.pop()
    print(l)
    l.pop(2)
    print(l)

D:\Python\Python36-32\python.exe E:/Python/123.py
[hello, 12, abc, 343, ppp]
[hello, 12, abc, 343]
[hello, 12, 343]

Process finished with exit code 0

    包含:

    l = [‘hello‘,12,‘abc‘,343,‘ppp‘]
    print(12 in l)
    print(‘abc‘ in l)  
    print(‘12‘ in l)

D:\Python\Python36-32\python.exe E:/Python/123.py
True
True
False

Process finished with exit code 0

    显示索引位置:

    l = [‘hello‘,12,‘abc‘,343,‘ppp‘]
    print(l)
    print(l.index(12))
    print(l.index(‘hello‘))

D:\Python\Python36-32\python.exe E:/Python/123.py
[hello, 12, abc, 343, ppp]
1
0

Process finished with exit code 0

    计算元素个数:

    l = [‘hello‘,12,‘abc‘,343,‘ppp‘]
    l.append(‘hello‘)
    print(l)
    print(l.count(‘hello‘))

D:\Python\Python36-32\python.exe E:/Python/123.py
[hello, 12, abc, 343, ppp, hello]
2

Process finished with exit code 0

    扩展列表:

    l = [‘hello‘,12,‘abc‘,343,‘ppp‘]
    l.extend([1,2,3,4])     #只能扩展列表,如果需要向列表添加 字典和元组需要使用append
    print(l)

D:\Python\Python36-32\python.exe E:/Python/123.py
[hello, 12, abc, 343, ppp, 1, 2, 3, 4]

Process finished with exit code 0

    移除元素: 通过元素名来移除元素

    l = [‘hello‘,12,‘abc‘,343,‘ppp‘]
    l.remove(‘abc‘)    #如果列表有多个元素,则只移除第一个
    print(l)

D:\Python\Python36-32\python.exe E:/Python/123.py
[hello, 12, 343, ppp]

Process finished with exit code 0

    顺序排列:

    l=[2,3,1]
    l.sort()
    print(l)

D:\Python\Python36-32\python.exe E:/Python/123.py
[1, 2, 3]

Process finished with exit code 0

    反转列表:

    l=[‘a‘,‘c‘,‘alex‘]
    l.reverse()
    print(l)

D:\Python\Python36-32\python.exe E:/Python/123.py
[alex, c, a]

Process finished with exit code 0

元组:

  特性:元组内的所有元素不能更改,元素可以是任意数据类型

  元组中可以包含list,元组中的list中的元素是可以更改的.当然list中也可以有元组.

  定义元组:

    a = (‘1‘,‘2‘,‘3‘,[‘s‘,‘b‘])

  更改元组中list的元素:

    a[3][0] = abc

  取值:

    print(a[0])

  循环:

    t = (‘a‘,1,‘b‘,1,(3,4))
    n = 0
    while n < len(t):
    print(t[n])
    n += 1

    for i in range(len(t)):
    print(t[i])

  元组的常用操作:

    计数:

    t = (‘a‘,1,‘b‘,1,(3,4))
    print(t.count(1))

D:\Python\Python36-32\python.exe E:/Python/123.py
2

Process finished with exit code 0

    索引位置:

    t = (‘a‘,1,‘b‘,1,(3,4))
    print(t.index(‘b‘))

D:\Python\Python36-32\python.exe E:/Python/123.py
2

Process finished with exit code 0

    切片:

    t=(‘a‘,1,‘b‘,1,(3,4))
    print(t[1:3])

D:\Python\Python36-32\python.exe E:/Python/123.py
(1, b)

Process finished with exit code 0

    包含:

    t=(‘a‘,1,‘b‘,1,(3,4))
    print(‘a‘ in t)

D:\Python\Python36-32\python.exe E:/Python/123.py
True

Process finished with exit code 0

 

 

    

    

      

 

Python基础day-3

标签:打印   中间   比较   第一个   add   while   可变   显示   blog   

原文地址:http://www.cnblogs.com/ldsly/p/6965239.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!