Python基础学习笔记Day2

1.dir()函数

(1)含义

     dir()函数是Python中的一个内置函数,用于返回指定对象的所有属性和方法

(2)使用方法
  • dir():不带参数时,返回当前作用域内的所有变量、函数和模块等。
  • dir(str):带参数时,返回该对象的所有属性和方法。

2.help函数

(1)含义

     help()函数用于显示对象(如模块、函数、类等)的帮助信息。

(2)使用方法
  • help():不带参数时,提供一个交互式的帮助系统,允许你动态地查询不同的对象。这种方式更加灵活,适合当你想要探索Python的不同功能或模块时使用。
  • help(print):带参数时,直接针对指定的对象提供详细的帮助文档,适合当你已经明确知道要查询的对象时使用。

 3.列表(List)

(1)含义

     列表是Python中最常用的数据结构之一,用于存储一系列有序的元素,它是一个可变的、有序的集合,可以包含不同类型的数据(如整数、浮点数、字符串)等。

(2)使用方法

     列表使用方括号[]来表示,其中的每个元素由逗号分隔开。

(3)获取列表的属性和方法

     用dir()函数获取list所有属性和方法的列表

print(dir(list))
#输出:['__add__', '__class__', '__class_getitem__', '__contains__', 
# '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', 
# '__format__', '__ge__', '__getattribute__', '__getitem__', 
# '__getstate__', '__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']
(4)获取列表的帮助信息

     用help()函数获取list的帮助信息

help(list)
#输出:Help on class list in module builtins:
# 
# class list(object)
#  |  list(iterable=(), /)
#  |  
#  |  Built-in mutable sequence.
#  |  
#  |  If no argument is given, the constructor creates a new empty list.
#  |  The argument must be an iterable if specified.
#  |  
#  |  Methods defined here:
#  |  
#  |  __add__(self, value, /)
#  |      Return self+value.
#  |  
#  |  __contains__(self, key, /)
#  |      Return key in self.
#  |  
#  |  __delitem__(self, key, /)
#  |      Delete self[key].
#  |  
#  |  __eq__(self, value, /)
#  |      Return self==value.
#  |  
#  |  __ge__(self, value, /)
#  |      Return self>=value.
#  |  
#  |  __getattribute__(self, name, /)
#  |      Return getattr(self, name).
#  |  
#  |  __getitem__(...)
#  |      x.__getitem__(y) <==> x[y]
#  |  
#  |  __gt__(self, value, /)
#  |      Return self>value.
#  |  
#  |  __iadd__(self, value, /)
#  |      Implement self+=value.
#  |  
#  |  __imul__(self, value, /)
#  |      Implement self*=value.
#  |  
#  |  __init__(self, /, *args, **kwargs)
#  |      Initialize self.  See help(type(self)) for accurate signature.
#  |  
#  |  __iter__(self, /)
#  |      Implement iter(self).
#  |  
#  |  __le__(self, value, /)
#  |      Return self<=value.
#  |  
#  |  __len__(self, /)
#  |      Return len(self).
#  |  
#  |  __lt__(self, value, /)
#  |      Return self<value.
#  |  
#  |  __mul__(self, value, /)
#  |      Return self*value.
#  |  
#  |  __ne__(self, value, /)
#  |      Return self!=value.
#  |  
#  |  __repr__(self, /)
#  |      Return repr(self).
#  |  
#  |  __reversed__(self, /)
#  |      Return a reverse iterator over the list.
#  |  
#  |  __rmul__(self, value, /)
#  |      Return value*self.
#  |  
#  |  __setitem__(self, key, value, /)
#  |      Set self[key] to value.
#  |  
#  |  __sizeof__(self, /)
#  |      Return the size of the list in memory, in bytes.
#  |  
#  |  append(self, object, /)
#  |      Append object to the end of the list.
#  |  
#  |  clear(self, /)
#  |      Remove all items from list.
#  |  
#  |  copy(self, /)
#  |      Return a shallow copy of the list.
#  |  
#  |  count(self, value, /)
#  |      Return number of occurrences of value.
#  |  
#  |  extend(self, iterable, /)
#  |      Extend list by appending elements from the iterable.
#  |  
#  |  index(self, value, start=0, stop=9223372036854775807, /)
#  |      Return first index of value.
#  |      
#  |      Raises ValueError if the value is not present.
#  |  
#  |  insert(self, index, object, /)
#  |      Insert object before index.
#  |  
#  |  pop(self, index=-1, /)
#  |      Remove and return item at index (default last).
#  |      
#  |      Raises IndexError if list is empty or index is out of range.
#  |  
#  |  remove(self, value, /)
#  |      Remove first occurrence of value.
#  |      
#  |      Raises ValueError if the value is not present.
#  |  
#  |  reverse(self, /)
#  |      Reverse *IN PLACE*.
#  |  
#  |  sort(self, /, *, key=None, reverse=False)
#  |      Sort the list in ascending order and return None.
#  |      
#  |      The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
#  |      order of two equal elements is maintained).
#  |      
#  |      If a key function is given, apply it once to each list item and sort them,
#  |      ascending or descending, according to their function values.
#  |      
#  |      The reverse flag can be set to sort in descending order.
#  |  
#  |  ----------------------------------------------------------------------
#  |  Class methods defined here:
#  |  
#  |  __class_getitem__(...) from builtins.type
#  |      See PEP 585
#  |  
#  |  ----------------------------------------------------------------------
#  |  Static methods defined here:
#  |  
#  |  __new__(*args, **kwargs) from builtins.type
#  |      Create and return a new object.  See help(type) for accurate signature.
#  |  
#  |  ----------------------------------------------------------------------
#  |  Data and other attributes defined here:
#  |  
#  |  __hash__ = None
 (5)列表的创建
#示例1 创建一个空列表  
empty_list=[]  
  
#示例2 创建一个包含整数的列表  
list=[1,2,3,4,5]  
  
#示例3 创建一个包含不同类型元素的列表  
mixed_list = [1,"Mia",5.20,True,[1,2,3]]  # 嵌套列表
(6)列表的索引和切片 
#示例4 访问列表中的元素
list=[1,2,3,4,5]
print(list[0])   #输出:1
print(list[-1])  #输出:5,负数索引表示从后往前数

#示例5 切片操作
print(list[1:3])  #输出:[2,3],不包括stop索引
print(list[::2])  #输出:[1,3,5],步长为2
(7)列表的内置方法

  append()方法:在列表的末尾添加一个新的元素

#示例6 
list=[1,2,3,4,5]
list.append(9)
print(list)      #输出:[1,2,3,4,5,9]

  extend()方法:将另一个列表的所有元素添加到当前列表的末尾

#示例7 
list=[1,2,3,4,5]
another_list=[6,7,8]
list.extend(another_list)
print(list)         #输出:[1,2,3,4,5,6,7,8]

  insert()方法:在指定索引处插入一个元素

#示例8  
list=[1,2,3,4,5]
list.insert(4,"Mia")
print(list)     #输出:[1,2,3,4,"Mia",5]

  remove()方法:移除列表中第一个出现的指定元素

#示例9  
list=[1,2,3,4,5,5]
list.remove(5)
print(list)     #输出:[1,2,3,4,5]

  pop()方法:移除并返回列表中指定索引处的元素,默认是最后一个元素

#示例10
list=[1,2,3,4,5]
last_element=list.pop()
print(last_element)  # 输出:5
print(list)          # 输出:[1,2,3,4]

  clear()方法:清空列表,移除所有元素

#示例11  
list=[1,2,3,4,5]
list.clear()
print(list)      #输出:[]

  index()方法:返回指定元素在列表中的索引

#示例12  
list=[1,2,3,4,5]
print(list.index(2))      #输出:1

  sort()方法:对列表进行排序

#示例13  
list=[6,1,5,3,4,2]
list.sort()
print(list)      #输出:[1,2,3,4,5,6]

  count()方法:返回指定元素在列表中出现的次数

#示例14  
list=[5,1,5,3,4,2]
print(list.count(5))       #输出:2

  sorted()方法:返回一个新的已排序的列表,不改变原列表

#示例15
list=[1,5,3,4,2]
sorted_list=sorted(list)
print(sorted_list)    #输出:[1,2,3,4,5]
print(list)           #输出:[1,5,3,4,2]

   reverse()方法:直接反转原列表

#示例16
list=[1,2,3,4,5]
print("原list:", list)       #输出:原list:[1,2,3,4,5]
list.reverse()
print("反转后的list:", list)  #输出:反转后的list:[5,4,3,2,1]

  列表推导式

#示例17   
squares=[x**2 for x in range(5)]
print(squares)      #输出:[0,1,4,9,16]

4.元组(Tuple)

(1)含义

     是Python中一种重要的内置数据类型,用于存储一组有序且不可变的元素,元组一旦被创建,就不能修改它的内容(如添加、删除或替换元素)。

(2)使用方法

     元组使用圆括号()来表示,其中的每个元素由逗号分隔开。

(3)元组的创建
#示例1 创建一个空元组
empty_tuple=()
print(empty_tuple)    #输出:()
(4)访问元组元素
#示例2
my_tuple=(1,2,3,4,5)
print(my_tuple[0])      #输出:1
print(my_tuple[4])      #输出:5
print(my_tuple[1:4])    #输出:(2,3,4)

     为什么用[]而不是()?

     []用于索引和切片操作,是Python语言设计的选择,以区分这些操作与元组定义或函数调用等其他使用()的情况。

(5)元组的用法

 创建一个以元组为键的字典

#示例3 
my_dict={(5,11):"xg", (10,20):"Mia"}
# 访问字典中的值
print(my_dict[(5,11)])    #输出:xg
print(my_dict[(10,20)])   #输出:Mia

  数据交换

#示例4
a=5
b=10
print("交换前:a=",a,"b=",b)     #输出:交换前:a=5 b=10
a,b=b,a
print("交换后:a=",a,"b =",b)    #输出:交换后:a=10 b=5

  连接与重复

#示例5
tuple1=(1,2,3)
tuple2=("a","b","c")
print(tuple1+tuple2)      #输出:(1,2,3,"a","b","c")

tuple3=("love")
print(tuple3*3)          #输出:lovelovelove

  最大值和最小值 

#示例6
tup=(1,4,5,88,99,2)
print(max(tup))            #输出:99
print(min(tup))            #输出:1

  count()方法 

#示例7  计算元组中某个元素的出现次数
my_tuple=(5,5,5,5,3,2,1)
print(my_tuple.count(5))      #输出:4

  index()方法 

#示例8  找到元组中某个元素的索引
my_tuple=(0,5,11,10,20)
print(my_tuple.index(5))    #输出:1(索引默认从0开始)

  • 57
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 64
    评论
评论 64
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值