栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python基础

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python基础

基础知识 第二章变量和简单数据类型 2.3.1使用方法修改字符串的大小写

方法:title()单词首字母变为大写,upper()全部字母变为大写,lower()全部字母变为小写

name = "Ada LovelAce"
print(name.title())
print(name.upper())
print(name.lower())
Ada Lovelace
ADA LOVELACE
ada lovelace
2.3.2在字符串中使用变量

要在字符串中插入变量的值,可在前引号前加上字母f,再将插入的变量放入花括号内

first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name}{last_name}"
print(f"Hello,{full_name.title()}")
Hello,Adalovelace
2.3.3使用制表符或换行符来添加空白

制表符t 换行符n

2.3.4删除空格

方法:rstrip()删除右边的空格,lstrip()删除左边的空格,strip()删除两边空格

favorite_language = ' python  '
favorite_language.strip()
'python'
3.2列表修改、添加、和删除元素
motircyles = ['honda','yamaha','suzuki']
motircyles[1]='ducati'
motircyles.append('添加')
del motircyles[2]
motircyles.insert(2,'插入')
motircyles
['honda', 'ducati', '插入', '添加']
3.2.1使用pop()方法
motorcycles = ['honda','yamaha','suzuki']
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
['honda', 'yamaha']
suzuki
motorcycles = ['honda','yamaha','suzuki']
first_owned = motorcycles.pop(0)
print(f"The first motorcycles I owned was a {first_owned.title()}")
The first motorcycles I owned was a Honda
只知道要删除元素的值,用方法remove()
motorcycles = ['honda','yamaha','suzuki']
motorcycles.remove('honda')
print(motorcycles)
['yamaha', 'suzuki']
3.3组织列表

方法:使用sort()对列表永久排序

cars = ['bmw','audi','toyota','subaru'] 
cars.sort()  #正序
print(cars)
['audi', 'bmw', 'subaru', 'toyota']
cars = ['bmw','audi','toyota','subaru']
cars.sort(reverse=True)  #倒序
print(cars)
['toyota', 'subaru', 'bmw', 'audi']

函数:使用sorted()对列表临时排序

cars = ['bmw','audi','toyota','subaru']
print(sorted(cars))
print(cars)
['audi', 'bmw', 'subaru', 'toyota']
['bmw', 'audi', 'toyota', 'subaru']
cars = ['bmw','audi','toyota','subaru']
print(sorted(cars,reverse=True))
print(cars)
['toyota', 'subaru', 'bmw', 'audi']
['bmw', 'audi', 'toyota', 'subaru']

方法:reverse()永久性的反转列表

cars = ['bmw','audi','toyota','subaru']
cars.reverse()
print(cars)
['subaru', 'toyota', 'audi', 'bmw']

函数:len()可快速获取列表的长度

cars = ['bmw','audi','toyota','subaru']
len(cars)
4
第四章 操作列表 使用函数range()
for i in range(1,5):
    print(i)
1
2
3
4
numbers = list(range(6))
print(numbers)
[0, 1, 2, 3, 4, 5]
第六章 字典
favorite_languages = {
    'jen':'python',
    'sarah':'c',
    'enwar':'ruby',
    'phil':'python',
}
language = favorite_languages['sarah'].title()
print(f"Saran's favorite language is {language}")
Saran's favorite language is C

方法get() 调用get()时,如果没有指定第二个参数,且指定的参数不存在,将返回None

alien_0 = {'color':'green','speed':'slow'}
point_value = alien_0.get('points','没有分数')
print(point_value)
没有分数
6.3.1遍历所有的键值对
user_0 = {
    'username':'efermi',
    'first':'enrico',
    'last':'fermi'
}

for key,value in user_0.items():
    print(f"nKey:{key}")
    print(f"Value:{value}")
Key:username
Value:efermi

Key:first
Value:enrico

Key:last
Value:fermi
favorite_languages = {
    'jen':'python',
    'sarah':'c',
    'enwar':'ruby',
    'phil':'python',
}
for name,language in favorite_languages.items():
    print(f"{name.title()}'s favorite language is {language.title()}")
Jen's favorite language is Python
Sarah's favorite language is C
Enwar's favorite language is Ruby
Phil's favorite language is Python
6.3.2遍历所有的键
favorite_languages = {
    'jen':'python',
    'sarah':'c',
    'enwar':'ruby',
    'phil':'python',
}
for name in favorite_languages.keys():
    print(name)
jen
sarah
enwar
phil

默认遍历所有的键

favorite_languages = {
    'jen':'python',
    'sarah':'c',
    'enwar':'ruby',
    'phil':'python',
}
for name in favorite_languages:
    print(name)
jen
sarah
enwar
phil
favorite_languages = {
    'jen':'python',
    'sarah':'c',
    'enwar':'ruby',
    'phil':'python',
}
friends = ['phil','sarah']
for name in favorite_languages.keys():
    print(f"Hi,{name.title()}")
    if name in friends:
        language = favorite_languages[name].title()
        print(f"{name.title()},I see you love {language} ")
Hi,Jen
Hi,Sarah
Sarah,I see you love C 
Hi,Enwar
Hi,Phil
Phil,I see you love Python 

方法keys()并非只能用于遍历:实际上,它返回一个列表,其中包含字典中的所有键

favorite_languages = {
    'jen':'python',
    'sarah':'c',
    'enwar':'ruby',
    'phil':'python',
}
if 'earn' not in favorite_languages.keys():
    print("Erin,please take our poll!")
Erin,please take our poll!
for name in sorted(favorite_languages.keys()):
    print(f"{name.title()},thank you for taking the poll.")
Enwar,thank you for taking the poll.
Jen,thank you for taking the poll.
Phil,thank you for taking the poll.
Sarah,thank you for taking the poll.
6.3.4 遍历字典中的所有值
favorite_languages = {
    'jen':'python',
    'sarah':'c',
    'enwar':'ruby',
    'phil':'python',
}
for language in favorite_languages.values():
    print(language.title())
Python
C
Ruby
Python
favorite_languages = {
    'jen':'python',
    'sarah':'c',
    'enwar':'ruby',
    'phil':'python',
}
for language in set(favorite_languages.values()):
    print(language.title())
C
Ruby
Python
set(favorite_languages.values())
{'c', 'python', 'ruby'}

集合中的每一个元素都必须是独一无二的,可使用一对花括号直接创建集合,并在其中用逗号分隔元素,集合不会以特定的顺序存储元素

languages = {'python','ruby','python','c'}
print(languages)
{'c', 'ruby', 'python'}
嵌套

在字典中存储列表

pizza = {
    'crust':'thick',
    'toppings':['mushrooms','extra cheese'],
}
print(f"You ordered a {pizza['crust']}-crust pizza with the following toppings:")
for topping in pizza['toppings']:
    print("f"+topping)
You ordered a thick-crust pizza with the following toppings:
mushrooms
extra cheese

在字典中存储字典

users = {
    'aeinstein':{
        'first':'albert',
        'last':'einstein',
        'location':'princeton',
    },
    'mcurie':{
        'first':'marie',
        'last':'curie',
        'location':'paris',
    },
}
for username,user_info in users.items():
    print(f"nUsername:{username}")
    full_name = f"{user_info['first']}{user_info['last']}"
    location = user_info['location']
    print(f"tFull name:{full_name.title()}")
    print(f"tLocation:{location.title()}")
Username:aeinstein
	Full name:Alberteinstein
	Location:Princeton

Username:mcurie
	Full name:Mariecurie
	Location:Paris
message = input("Tell")
print(message)
TellHello evertone
Hello evertone
 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/744808.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号