python基础不懂语法学习(一)

列表(List):

有序可更改的集和,允许重复成员。使用方括号表示 "[]"

# 创建列表
thistlit = ["apple","banana","cherry","orange","kiwi","melon","mango"]
# 访问项目 -1表示最后一个项目 -2表示倒数第二个,依次类推
print(thislist[-1])
# 索引范围 返回第三项到第五项 同理反方向索引
print(thislist[2:5])
print(thislist[-4:-1])
# 遍历列表 for循环遍历
for x in thislist:
    print(x)
# 检测项目是否存在 使用 in 关键字
if "apple" in thislist
    print("yes",'apple' is in the fruits list")
# 添加项目 append() 指定索引处添加项目 insert()
thislist.append("orange")
thislist.insert(1,"orange")
# 删除项目 remove()
thislist.remove("banana")
# 删除指定索引 未索引删除最后一项
thislist.pop()
# 关键字删除指定索引
del thislist[0]
# 复制列表 copy() 或 使用内建方式
mylist = thislist.copy()
mylist = list(thislist)
# 合并列表 使用 + 运算符 或 使用append追加 或 使用extend()
list3 = list1 + list2
for x in list2:
    list1.append(x)
list.extend(list2)
# 构造函数list()
thislist = list(("apple","banana","cherry")) #双括号
print(thislist)

元组(Tuple):

有序且不可更改的集和,允许重复成员。使用圆括号表示 "()"

# 大部分与列表相同 
# 创建仅包含一个项目的元组,必须 在该项目后添加一个逗号 否则无法识别为元组
thistuple = ("apple",)
print(type(thistuple))
thistuple = ("apple") #报错
# 元组不可更改,故无法删除项目,只能完全删除元素
del thislist
# 构造函数tuple()
thislist = tuple(("apple","banana","cherry")) #双括号
# 删除所有元素 clear()
thislist.clear()
# 返回指定值的元素数量 count()
x = thislist.count("cherry")
print(x)
# 返回指定值首次出现的位置 index()
x = thislist.index("cherry")
print(x)
print(thislist)

集和(Set):

无序无所引集和,没有重复成员。使用花括号表示 "{}"

set是无序的,项目没有索引,使用for循环遍历 ,或使用 in 查询集和是否存在指定值。

thisset = {"apple","banana","cherry"}
for x in thisset:
    print(x)

print("banana" in thisset)
# 添加一个项目到集和 add() 
thisset.add("orange")
# 添加多个项目到集和 update()
thisset.update(["orange","mango","grapes"])
# 获取Set长度 删除项目remove 删除指定项目
thisset.discard("banana")
# 删除项目 pop() 只能删除最后一项 因为set是无序的 不知道会删除什么项目 返回值是被删除项目
x = thisset.pop()
print(x)
# 合并两个集和 union() 或 使用 update() 将一个集和所有项目插入另一个集和
## union() 和 update() 都将排除任何重复项
set1 = {"a","b","c"}
set2 = {1,2,3}

set3 = set1.union(set2)
print(set3)

set1.update(set2)
print(set1) 
# 返回一个包含两个结合之间的差异结合 different()
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.difference(y) # 返回x集和中的不同值
z = y.difference(x) # 返回y集和中的不同值
print(z)
# 返回两个或更多集和相似性的集和 intersection()
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.intersection(y) 
# 构造函数 set()
thisset = set(("apple","banana","cherry"))
print(thisset)

字典(Dictionary):

无序,可变和有索引的集和, 没有重复成员。使用花括号表示 "{}" 拥有键和值

thisdict = {
    "brand": "Porsche",
    "model": "911",
    "year": 1963
}
# 获取"model" 键的值 或 使用get()
x = thisdict["model"]
x = thisdict.get("model")
# 更改值 引用键来改
thisdict["year"] = 2019
# 使用for循环遍历字典
for x in thisdict:
  print(x) # 打印所有键名
  print(thisdict[x]) # 打印所有值
# 其他语法同理 构造函数 dict()
thisdict = dict(brand="Porsche",model="911",year=1963)

For循环

# range()返回一个数字序列,默认从0开始,并递增1,并以指定的数字结束
for x in range(10): # 0-9
    print(x)
# 使用第三个参数指定增量值
for x in range(3,50,6): # 3-49 步长6
    print(x)

函数传参

如果不知道传递给用户的函数多少个参数,可以在函数定义的参数名称中添加星号 * ,这样函数将接收一个参数元组,并可以访问各项

def my_function(*kids):
    print("The yougest chile is" + kid[2])
my_function("Phoebe","Jenifer","Rory")
# 结果输出 The youngest child is Rory

递归

定义的函数可以调用自身。

def tri_recursion(k):
  if(k>0)
    result = k+tri_recursion(k-1)
    print(result)
  else:
    result = 0
  return result
print("\n\nRecursion Example Result")
tri_recursion(6)

# 注释
k = 6,满足 k > 0,于是调用 tri_recursion(5):
  k = 5,满足 k > 0,于是调用 tri_recursion(4):
    k = 4,满足 k > 0,于是调用 tri_recursion(3):
      k = 3,满足 k > 0,于是调用 tri_recursion(2):
        k = 2,满足 k > 0,于是调用 tri_recursion(1):
          k = 1,满足 k > 0,于是调用 tri_recursion(0):
            k = 0,此时不满足 k > 0,返回 0。
# 递归开始返回
tri_recursion(1):返回 1 + 0 = 1,打印 1
tri_recursion(2):返回 2 + 1 = 3,打印 3
tri_recursion(3):返回 3 + 3 = 6,打印 6
tri_recursion(4):返回 4 + 6 = 10,打印 10
tri_recursion(5):返回 5 + 10 = 15,打印 15
tri_recursion(6):返回 6 + 15 = 21,打印 21

Lambda函数

一种小的匿名函数,可接收任意数量的参数,但只能有一个表达式

# lambda argument参数: expression表达式
def myfunc(n)
  return lambda a:a * n
mydoubler = myfunc(2)
mytripler = myfunc(3)

print(mydoubler(11))
print(mytripler(11))

数组

python没有内置对数组的支持,使用python列表代替。用法与列表相同

类和对象

python中几乎所有的东西都是对象,拥有属性和方法。类(Class)类似对象的构造函数,或者用于创建对象,且每次创建新对象时都会自动调用__init__()函数。

# 内置的__init__()函数 始终在启动类时执行 将值赋给对象属性 
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def myfunc(self):
        print("Hello my name is " + self.name)
p1 = Person("Bill",63)
p1.myfunc()
# self参数 必须是类中任意函数的首个参数 可以使用其他字符代替
 class Person:
    def __init__(mysillyobject, name, age);
      mysillyobject.name = name
      mysillyobject.age = age
    def myfunc(abc)
      print("Hello my name is " + abc.name)
p1 = Person("Bill", 63)
p1.myfunc()
# del关键字删除对象和其属性
CC BY-NC-SA 4.0 Deed | 署名-非商业性使用-相同方式共享
最后更新时间:2025-09-19 19:59:20