(新手小白自学python,留此文章,便于日后温故知新)
一、变量和简单数据类型
1.1 第一个程序
print("Hello Python world!")
1.2 变量和字符串
1.2.1 变量
message=Hello Python world!
print(message)
1.2.2 字符串
在Python中,用引号括起的都是字符串,其中的引号可以是单引号,也可以是双引号。
"This is a string."
'This is also a string.'
name = "ada lovelace"
print(name.title())
first_name = "ada"
last_name = "lovelace"
full_name = first_name + " " + last_name
print("Hello, " + full_name.title() + "!")
>>> print("Python")
Python
>>> print("\tPython")
Python
>>> print("Languages:\nPython\nC\nJavaScript")
Languages:
Python
C
JavaScript
>>> print("Languages:\n\tPython\n\tC\n\tJavaScript")
Languages:
Python
C
JavaScript
>>> favorite_language = ' python '
>>> favorite_language.rstrip()
>>> favorite_language.lstrip()
>>> favorite_language.strip()
1.3 数字和注释
>>> 2 + 3
5
>>> 3/2
1.5
>>> 3 ** 2
9
>>> (2 + 3) * 4
20
>>> 0.1 + 0.1
0.2
>>> 0.2 + 0.1
0.30000000000000004
>>> 3 * 0.1
0.30000000000000004
>>>age = 23
>>>message = "Happy " + str(age) + "rd Birthday!"
>>>print(message)
Happy 23rd Birthday!
二、列表简介
列表由一系列按特定顺序排列的元素组成,其中的元素之间可以没有任何关系。在Python中,用方括号([ ])来表示列表,并用逗号来分隔其中的元素。
2.1 创建列表
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0])
trek
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[1])
print(bicycles[3])
cannondale
specialized
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[-1])
specialized
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
message = "My first bicycle was a " + bicycles[0].title() + "."
print(message)
My first bicycle was a Trek.
2.2 修改、添加和删除元素
2.2.1 修改和添加元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha', 'suzuki', 'ducati']
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)
['ducati', 'honda', 'yamaha', 'suzuki']
2.2.2 删除元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha']
suzuki
motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0)
print('The first motorcycle I owned was a ' + first_owned.title() + '.')
The first motorcycle I owned was a Honda.
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'suzuki']
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
too_expensive = 'ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print("\nA " + too_expensive.title() + " is too expensive for me.")
['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'suzuki']
A Ducati is too expensive for me.
2.3 组织列表
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']
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)
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']
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)
['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']
>>> cars = ['bmw', 'audi', 'toyota', 'subaru']
>>> len(cars)
4
三、操作列表
3.1 遍历整个列表
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
alice
david
carolina
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
Alice, that was a great trick!
David, that was a great trick!
Carolina, that was a great trick!
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n")
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
David, that was a great trick!
I can't wait to see your next trick, David.
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n")
print("Thank you, everyone. That was a great magic show!")
Alice,that was a great trick!
I can't wait to see your next trick,Alice.
David,that was a great trick!
I can't wait to see your next trick,David.
Carolina,that was a great trick!
I can't wait to see your next trick,Carolina.
Thank you, everyone. That was a great magic show!
对于位于for 语句后面且属于循环组成部分的代码行,一定要缩进。 当出现语法错误时,如for语句之后紧接的语句忘记缩进、缩进了无需缩进的代码行、出现了不符合语法的代码或遗漏了冒号,Python会提醒出错哦; 当出现逻辑错误时,如忘记缩进额外的代码行或者进行了循环后的不必要缩进,那Python就大概率不会提醒啦~
3.2 创建数值列表
for value in range(1,5):
print(value)
1
2
3
4
numbers = list(range(1,6))
print(numbers)
[1, 2, 3, 4, 5]
even_numbers = list(range(2,11,2))
print(even_numbers)
[2, 4, 6, 8, 10]
squares = []
for value in range(1,11):
square = value**2
squares.append(square)
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
squares = []
for value in range(1,11):
squares.append(value**2)
print(squares)
3.3 统计计算函数与列表解析
3.3.1 统计计算函数
>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> min(digits)
0
>>> max(digits)
9
>>> sum(digits)
45
3.3.2 列表解析
列表解析是指将for循环和创建新元素的代码合并成一行,并自动附加新元素,即只需编写一行代码就能生成以上示例的列表。
squares=[]
for value in range(1,11):
square=value**2
squares.append(square)
print(squares)
squares=[]
for value in range(1,11):
squares.append(value**2)
print(squares)
squares=[value**2 for value in range(1,11)]
print(squares)
3.4 切片和遍历切片
处理列表中的部分元素,即使用列表的一部分,在python中称为切片。
3.4.1 切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
['charles', 'martina', 'michael']
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])
['martina', 'michael', 'florence']
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[:4])
['charles', 'martina', 'michael', 'florence']
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[2:])
['michael', 'florence', 'eli']
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[-3:])
3.4.2 遍历切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("Here are the first three players on my team:")
for player in players[:3]:
print(player.title())
Here are the first three players on my team:
Charles
Martina
Michael
3.5 复制列表
复制列表:具体操作是指创建一个包含整个列表的切片,即同时省略起始索引和终止索引([:] )。这时,Python会创建一个始于第一个元素,终止于最后一个元素的切片,即复制整个列表。
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
My favorite foods are:
['pizza', 'falafel', 'carrot cake']
My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake']
3.6 元组
Python将不能修改的值称为不可变的 ,而不可变的列表被称为元组。元组看起来犹如列表,但使用圆括号而不是方括号来标识。定义元组后,就可以使用索引来访问其元素,就像访问列表元素一样。
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
200
50
dimensions = (200, 50)
for dimension in dimensions:
print(dimension)
200
50
dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
print(dimension)
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)
Original dimensions:
200
50
Modified dimensions:
400
100
当存储的一组值在程序的整个生命周期内都不变时,可使用元组。
若电脑未配置环境,可配置后再进行学习,当然也可直接登录https://replit.com/进行python基础语法的学习。 以上为Python语法基础第一部分的内容,本文仅仅简单介绍了字符串、列表、切片和元组的使用方法及示例,余下的语法基础将在接下来的几篇文章中进行介绍。
社交有距春常在,核酸无恙岁月安。
|