Python基础知识

目录

一、基础知识

二、列表[]表示

三、元组()

四、集合{} 是无序的

五、字典 用大括号,有键和值

六、if语句

七、while循环  使用while循环,条件为真,可以执行一组语句

八、函数


一、基础知识

#切片 [:]获取字符

a='Hello,world!'
print(a[1:4])

#大写upper()

a='Hello,world!'
print(a.upper())

#小写lower()

#替换字符串replace()

a='Hello,world!'
print(a.replace('H','h'))

#拆分字符串split()

a="Hello,world"
print(a.split("!"))

#format()单个传参数

age=23
txt="今年{}"
print(txt.format(age))

#format()多个传参{}

#capitalize()首字母大写

#casefold()首字母小写

#布尔值 True/False

二、列表[]表示

#列表方括号[]或者list()

#len()长度

#更改

#插入列表insert()

mylist = ['a','b','c','d']
mylist.insert(2,'f') #指定位置添加
print(mylist)

#append()添加列表 加入列表

mylist = ['a','b','c','d']
mylist2 = ['f','g','h']
for x in mylist2:
    mylist.append(x)
print(mylist)

#extend()合并列表 extend()可以添加元组、集合、字典等

mylist = ['a','b','c','d']
mylist2 = ['f','g','h']
mylist.extend(mylist2)
print(mylist)

#remove()删除列表

#pop()删除指定索引,如果不指定删除最后一项

#del删除全部

#倒序 reverse()反转元素的当前排列顺序

#排序 sort()方法区分大小写,大写字母都在小写字母之前

三、元组()

mytuple = ('a','b','c')
print(type(mytuple))

#索引

#更改 先强制转换为列表

x= ('a','b','c','d')
y=list(x)
y[1] = 'f'
x=tuple(y)
print(x)

#添加项目值

thistuple = ('a','b','c','d')
y = ('f',) #创建只有一个元组时,加逗号
thistuple += y
print(thistuple)

#删除元组remove()函数 先转换为列表

#解包

#遍历元组 for  in 

四、集合{} 是无序的

#遍历集合 for in

#添加 add() 

#update()添加集合

myset = {'a','b','c','d'}
myset1 = {'e','f','g','h'}
myset.update(myset1)
print(myset)

#remove()移除

#集合连接 union()函数 update()方法将set2插入到set1中,无论union()和update()将排除任何重复的项目

#仅保留重复项 intersection_update()

myset = {'a','b','c','d'}
myset1 = {'a','f','g'}
myset.intersection_update(myset1)
print(myset)

#保留所有,但不保留重复项symmetric_difference()

myset = {'a','b','c','d'}
myset1 = {'a','f','g','h'}
z = myset.symmetric_difference(myset1)
print(z)

五、字典 用大括号,有键和值

thisdict = {
    "brand":"ford",
    "year":1999
}
print (thisdict)

#访问键值

thisdict = {
    "brand":"ford",
    "year":1999
}
x = thisdict["year"]
print (x)

#keys()方法将访问字典中所以键的列表

thisdict = {
    "brand":"ford",
    "year":1999
}
x = thisdict.keys()
print (x)

#添加

thisdict = {
    "brand":"ford",
    "year":1999
}
thisdict['age'] = 23
print (thisdict)

#values()方法返回字典中所有值的列表

x = thisdict.value()

items()方法将返回字典中的每个项目。作为列表中的元组

thisdict = {
    "brand":"ford",
    "year":1999
}
thisdict['age'] = 23
x = thisdict.items()
print (x)

#要确定字典中是否存在指定的键,in关键字

#更改

可以覆盖以及update()方法

thisdict = {
    "brand":"ford",
    "year":1999
}
thisdict.update({'year':2000})
print(thisdict)

  

#删除pop()方法

thisdict = {
    "brand":"ford",
    "year":1999
}
thisdict.pop('year')
print(thisdict)

#clear()清空

#遍历循环、values()、keys()

thisdict = {
    "brand":"ford",
    "year":1999
}
for i in thisdict:
    print(thisdict[i])
 

#嵌套字典

dict1 = {
    "brand":"ford",
    "year":1999
}
dict2 = {
    "brand":"ford",
    "year":2000
}
dict3 = {
    "brand":"ford",
    "year":2001
}
mydict = {
    'dict1':dict1,
    'dice2': dict2,
    'dice3': dict3
}
print(mydict)

六、if语句

#and语句

a = 200
b = 33
c = 500
if a > b and c > a:
    print("两种条件都满足")

# or语句

a = 200
b = 33
c = 500
if a > b or a > c:
    print("至少满足一种")

七、while循环  使用while循环,条件为真,可以执行一组语句

i = 0
while i < 6:
    print(i)
    i += 1

 #break停止循环

i = 0
while i < 6:
    print(i)
    if i == 3:
        break
    i += 1

#continue 停止当次循环,继续下一个

i = 0
while i < 6:
    i += 1
    if i == 3:
        continue
    print(i)

 

八、函数

#定义调用函数

def my_function():
    print(' Hello from a function ' )
my_function()

#参数

#单个参数

def my_function(fname):
    print(fname + ' Hello from a function ' )
my_function('a')

#多个参数 (逗号隔开)

def my_function(fname, lname):
    print(fname + ' Hello from a function ' + lname )
my_function('a','b')

#未知参数

def my_function(*lname):
    print(' Hello from a function ' + lname[1])
my_function('a', 'b', 'c')

#任意关键词参数**

def my_function(**lname):
    print(' Hello from a function ' + lname['a'])
my_function(a = 'f', b = 'g')

 #默认参数值

def my_function(country = 'Norway'):
    print(' Hello from a function ' + country)
my_function('Sweden')
my_function()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值