Day1 of Learning Python

Day 1 of Learning Python

书本为《Python编程:从入门到实践》
注意:
函数:通过“函数名()”的方式进行调用。
方法:通过“对象.方法名”的方式进行调用
在这里插入图片描述

message="Hello Eric, would you like to learn some Python today?"
print(message.upper()) #全部大写
print(message.lower()) #全部小写
print(message.title()) #首字母大写
message2='Albert Einstein once said, "A person who never made a mistake never tried anything new"'
print(message2)

运行结果如下

HELLO ERIC, WOULD YOU LIKE TO LEARN SOME PYTHON TODAY?
hello eric, would you like to learn some python today?
Hello Eric, Would You Like To Learn Some Python Today?
Albert Einstein once said, “A person who never made a mistake never tried anything new”

\n换行 \t添加一个制表位
Python 用两个称号表示乘方运算

>>>3*3
9
>>>10**6
1000000

列表

python中,用方括号 [] 来表示列表,并用逗号来分割其中元素。第一个列表元素的索引为0,而不是1. 索引指定为-1,可以让python返回最后一个列表元素,-2返回倒数第二个元素,以此类推。
方法append()将元素添加到列表末尾,而不影响列表中其它元素。

names.append('laowang')

方法insert()可在列表任何位置添加新元素,需要指定新元素的索引和值

names.insert(0,'laowang')

使用del 可删除任何位置处列表元素,条件是知道其索引

Del names[1]

方法pop()可删除列表末尾的元素,并让你能够接着使用它。术语弹出(pop)源自这样的类比:列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶元素。

motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
popped_motorcycle=motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

运行结果为

[‘honda’, ‘yamaha’, ‘suzuki’]
[‘honda’, ‘yamaha’]
suzuki

实际上,你可以使用pop()删除列表中任意一个元素,只需在括号中指定要删除的元素的索引即可。motorcycles.pop(0)
如果你要从列表中删除一个元素,且不再以任何方式使用它,就是用del语句;如果你要在删除元素后还能继续使用它,就是用方法pop()。
根据值删除元素remove(),比如motorcycles.remove(‘suzuki’)

练习题3-4~3-7

mylist=['A','B','C','D','E']
print(mylist)
cannot=mylist.pop(1)
print(cannot+' cannot come')
mylist.insert(1,'F')
print(mylist)
mylist.insert(0,'G')
mylist.insert(3,'H')
mylist.append('I')
print(mylist)
print("I can only invite two people")
print("sorry, "+ mylist.pop()+", I can only invite two people")
print("sorry, "+ mylist.pop()+", I can only invite two people")
print("sorry, "+ mylist.pop()+", I can only invite two people")
print("sorry, "+ mylist.pop()+", I can only invite two people")
print("sorry, "+ mylist.pop()+", I can only invite two people")
print("sorry, "+ mylist.pop()+", I can only invite two people")
print(mylist[0]+", you are still invited")
print(mylist[1]+', your are still invited')
del mylist[1]
del mylist[0]
print(mylist)

结果如下

[‘A’, ‘B’, ‘C’, ‘D’, ‘E’]
B cannot come
[‘A’, ‘F’, ‘C’, ‘D’, ‘E’]
[‘G’, ‘A’, ‘F’, ‘H’, ‘C’, ‘D’, ‘E’, ‘I’]
I can only invite two people
sorry, I, I can only invite two people
sorry, E, I can only invite two people
sorry, D, I can only invite two people
sorry, C, I can only invite two people
sorry, H, I can only invite two people
sorry, F, I can only invite two people
G, you are still invited
A, your are still invited
[]

组织列表

方法sort()永久性按字母顺序排列。按字母顺序相反顺序排列只需向sort()传递参数reverse=True。

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

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

运行结果

[‘audi’, ‘bmw’, ‘subaru’, ‘toyota’]
[‘toyota’, ‘subaru’, ‘bmw’, ‘audi’]

函数sorted()能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序。

cars=['bmw','audi','toyota','subaru']
print("Here is the original list:")
print(cars)
print("Here is the sorted list:")
print(sorted(cars))
print("Here is the original list again:")
print(cars)

运行结果

Here is the original list:
[‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
Here is the sorted list:
[‘audi’, ‘bmw’, ‘subaru’, ‘toyota’]
Here is the original list again:
[‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]

也可向sorted()传递参数reverse=True。
注意:在并非所有值都是小写时,按字母排列列表要复杂些。
要反转列表元素的排列顺序,可使用方法reverse()。

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

结果
[‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
[‘subaru’, ‘toyota’, ‘audi’, ‘bmw’]

方法reverse()不是指按与字母顺序相反的顺序排列列表元素,而只是反转列表元素的排列顺序。其可永久性修改列表排列顺序,但只需对列表再次调用reverse()即可恢复原排序。
函数len()可获得列表长度
习题3-8~3-10

places=['tokyo','sydney','london','paris','hongkong']
print('original list')
print(places)
print('\nlisted by character')
print(sorted(places))#使用sorted()按字母顺序打印,但不修改
print('\noriginal list')
print(places)  #核实原来顺序
places.reverse()#修改列表排序
print('\nreverse the original list')
print(places)
places.reverse()#恢复原来顺序
print('\noriginal list')
print(places)
places.sort()#按字母顺序排列
print('\nlisted by character')
print(places)
places.sort(reverse=True)#按字母顺序反向排列
print('\nreverse the list')
print(places)

print('\nThere are '+str(len(places)) +' places in total')

结果

original list
[‘tokyo’, ‘sydney’, ‘london’, ‘paris’, ‘hongkong’]

listed by character
[‘hongkong’, ‘london’, ‘paris’, ‘sydney’, ‘tokyo’]

original list
[‘tokyo’, ‘sydney’, ‘london’, ‘paris’, ‘hongkong’]

reverse the original list
[‘hongkong’, ‘paris’, ‘london’, ‘sydney’, ‘tokyo’]

original list
[‘tokyo’, ‘sydney’, ‘london’, ‘paris’, ‘hongkong’]

listed by character
[‘hongkong’, ‘london’, ‘paris’, ‘sydney’, ‘tokyo’]

reverse the list
[‘tokyo’, ‘sydney’, ‘paris’, ‘london’, ‘hongkong’]

There are 5 places in total

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值