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)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Python学习笔记》是由皮大庆编写的一本关于Python语言学习的教材。在这本书,作者详细介绍了Python语言的基础知识、语法规则以及常用的编程技巧。 首先,作者简要介绍了Python语言的特点和优势。他提到,Python是一种易于学习和使用的编程语言,受到了广大程序员的喜爱。Python具有简洁、清晰的语法结构,使得代码可读性极高,同时也提供了丰富的库和模块,能够快速实现各种功能。 接着,作者详细讲解了Python的基本语法。他从变量数据类型、运算符等基础知识开始,逐步介绍了条件语句、循环控制、函数、模块等高级概念。同时,作者通过大量的示例代码和实践案例,帮助读者加深对Python编程的理解和应用。 在书,作者还特别强调了编写规范和良好的编程习惯。他从命名规范、注释风格、代码缩进等方面指导读者如何写出清晰、可读性强的Python代码。作者认为,良好的编程习惯对于提高代码质量和提高工作效率非常重要。 此外,作者还介绍了Python的常用库和模块。他提到了一些常用的库,如Numpy、Pandas、Matplotlib等。这些库在数据处理、科学计算、可视化等领域有广泛的应用,帮助读者更好地解决实际问题。 总的来说,《Python学习笔记》是一本非常实用和全面的Python学习教材。通过学习这本书,读者可以系统地学习和掌握Python编程的基础知识和高级应用技巧,为以后的编程学习和工作打下坚实的基础。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值