资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

python笔记-变量-数据类型-列表-创新互联

'''
变量名只能包含字母、数字和下划线。变量名能以字母或下划线打头,但不能以数 字打头。例如,可将变量命名为message_1 ,但不能将其命名为1_message 。 
变量名不能包含空格,但能使用下划线来分隔其中的单词。例如,变量名 greeting_message 可行,但变量名greeting message 会引发错误。 不要将Python关键字和函数名用作变量名,即不要使用Python保留用于特殊用途的 单词,如print (请参见附录A.4)。 
变量名应既简短又具有描述性。例如,name 比n 好,student_name 比s_n 好,name_length 比length_of_persons_name 好。
慎用小写字母l 和大写字母O ,因为它们可能被人错看成数字1 和0 。

'''

"""
变量,字符串

"""

first_name = "li"
last_name = "jingjing"
#在字符串中使用变量
full_name = f"{first_name} {last_name}"

print(full_name)
print(f"Hello,{full_name.title()}!")
message = f"Hello,{full_name.title()}!"
print(message)

#py3.5使用这种方法在字符串中使用变量
full_name2 = "{} {}".format(first_name, last_name)
print("full_name2="+full_name2)

#制表符,\t,换行符,\n,空白
print("\tpython")
print("\npython")

str_formart = 'python      '
#删除字符串右边的空白.rstrip()
#删除字符串左边的空白.lstrip()
#删除字符串两端的空白.strip()

"""
Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。
"""

str_formart2 = str_formart.rstrip()
print(str_formart2)

#整数的四则运算:+ - * /
#浮点数:0.1
#整数和浮点数运算,结果总是浮点数

#数中的下划线,增加可读性,下划线并不会被打印
#适用整数和浮点数,py3.6以上支持

int = 12_000_000_000
i = int -666
print(int)
print(i)

#多变量赋值
x, y, z = 1, 2, 3
print(x,y,z)


#常量-使用全大写的的变量
STR_STR = "STR"
print(STR_STR)

#注释,(#单行) ('''多行)("""多行)

#python之chan禅
"""
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""

'''
The Zen of Python,作者:Tim Peters
美丽总比丑陋好。
显式总比隐式好。
简单总比复杂好。
复杂总比复杂好。
平坦比嵌套好。
稀疏比密集好。
可读性很重要。
特殊情况不足以违反规则。
虽然实用性胜过纯洁。
错误永远不应该默默地过去。
除非明确沉默。
面对模棱两可,拒绝猜测的诱惑。
应该有一种——最好只有一种——显而易见的方法。
尽管除非您是荷兰人,否则这种方式起初可能并不明显。
现在总比没有好。
虽然从来没有比现在*正确*更好。
如果实现难以解释,这是一个坏主意。
如果实现很容易解释,这可能是一个好主意。
命名空间是一个很棒的主意 - 让我们做更多的事情!
'''

#列表由一系列按特定顺序排列的元素组成。类似java中的数组
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
#使用-1访问列表最后一个元素
print(bicycles[-1])
bicycles_message = f"My first bicycles was a {bicycles[0].title()}."
print(bicycles_message)

#修改、添加和删除元素
#修改元素0
str_list = ['honda', 'yamaha', 'suzuki']
print(str_list)
str_list[0]='ducati'
print(str_list)
#使用append方法在列表最后添加元素
str_list.append('ducati')
print(str_list)

#创建空列表并依次添加元素,使用最多的方式
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)

#使用insert()方法在列表中插入元素,需要下标和值,如下:
str_list.insert(0, 'zero')
print(str_list)

#使用del()方法删除列表中的元素,需要下标,如下:
del str_list[0]
print(str_list)

#pop()删除元素
print("************pop***************")
#这里将str_list列表中最后一个变量删除了,使用pop()方法,并把删除的变量赋值给了popped_str_list
popped_str_list = str_list.pop()
print(str_list)
#popped_str_list打印的是列表中的,被删除的最后一个元素
print(popped_str_list)
print("************pop***************")

#使用pop()方法删除列表中任意位置的元素,只需要在方法中制定要删除的元素的‘下标’。
first_owned = str_list.pop(0)
print(f"The first motorcycle I owned was a {first_owned.title()}.")

#使用remove方法删除列表中指定的元素,'.remove(0)'
print(str_list)
str_list.remove('yamaha')
print(str_list)

#指出将'ducati' 从列表motorcycles 中删除的原因,可以使用变量too_expensive访问'ducati' 
print("*************************")
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] 
print(motorcycles)
too_expensive = 'ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print(f"\nA {too_expensive.title()} is too expensive for me.")

#使用sort()方法对列表按字母排序
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort() 
print(cars)

#反向排序需要向sort()中传入True,如下:
cars.sort(reverse=True)
print(cars)

'''
sort-排序并保存。
sorted-排序不保存。

sort 与 sorted 区别:
sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。
list 的 sort 方法返回的是对已经存在的列表进行操作,无返回值,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。
'''
print("****************************")
cars = ['bmw', 'audi', 'toyota', 'subaru']
print("Here is the original list:") 
print(cars)
print("\nHere is the sorted list:") 
print(sorted(cars))
print("\nHere is the original list again:") 
print(cars)

#反向打印列表,'.reverse()'
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)

#len()方法确定列表长度
cars = ['bmw', 'audi', 'toyota', 'subaru']
str_len = len(cars)
print(str_len)



你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧

网站设计制作、成都做网站服务团队是一支充满着热情的团队,执着、敏锐、追求更好,是创新互联的标准与要求,同时竭诚为客户提供服务是我们的理念。创新互联公司把每个网站当做一个产品来开发,精雕细琢,追求一名工匠心中的细致,我们更用心!

分享题目:python笔记-变量-数据类型-列表-创新互联
浏览路径:http://cdkjz.cn/article/icgdj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220