Python好神奇!!!
先说说最大的感受叭,python比c++用起来要简单,有很多c++中需要自己完成底层代码运用python只需要一行代码即可完成,但是人做的少那么机器就需要做的多,在运行时间上稍有劣势,而且python对格式的要求较高,所以c++还是相当重要。
*以下使用py代表python
一. 变量 数字 字符串
py中并没有对变量的类型加以区分,只要合法创建一个变量名即可任意赋值,字符、数字、字符串均可 / 字符串没有单双引号之分 / 创建多行字符时使用三引号 /
message = "Hello Python world!"
a = 6
my_string = "This is a double-quoted string."
quote = "Linus Torvalds once said, 'Any program is only as good as it is useful.'"
multiline_string = '''This is a string where I
can confortably write on multiple lines
without worring about to use the escape character "\\" as in
the previsou example.
As you'll see, the original string formatting is preserved.
'''
二. 列表和循环 (list)
列表是元素的集合,存储在一个变量中,列表中存储的元素类型没有限制,用方括号定义列表。
具体操作:命名和定义列表 / 访问列表元素 / 穷举列表 / 修改元素 / 查找元素 / 检测元素是否在列表中 / 列表排序 / 反转列表 / 列表数值排序 / 列表长度 / 列表元素移除 / 列表切割 / 列表复制
以上操作py都有对应的函数可以直接拿来用,很容易上手。
dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
print("Resultes for the dog show are as follows:\n")
for index, dog in enumerate(dogs):
place = str(index)
print("Place: " + place + " Dog: " + dog.title())
dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
print(dogs.index('australian cattle dog'))
dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
print('australian cattle dog' in dogs)
print('poodle' in dogs)
dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
dogs.append('poodle')
dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
dogs.insert(1, 'poodle')
students = ['bernice', 'aaron', 'cody']
students.sort()
students = ['bernice', 'aaron', 'cody']
students.reverse()
dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
del dogs[0]
dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
last_dog = dogs.pop()
usernames = ['bernice', 'cody', 'aaron', 'ever', 'dalia']
first_batch = usernames[0:3]
usernames = ['bernice', 'cody', 'aaron', 'ever', 'dalia']
copied_usernames = usernames[:]
列表推导式类似公式化简
字符串列表:切割字符串 / 查找子串 / 替换子串 / 子串计数 / 分裂字符串
message = "I like cats and dogs."
dog_present = 'dog' in message
message = "I like cats and dogs."
dog_index = message.find('dog')
message = "I like cats and dogs, but I'd much rather own a dog."
last_dog_index = message.rfind('dog')
message = "I like cats and dogs, but I'd much rather own a dog."
message = message.replace('dog', 'snake')
message = "I like cats and dogs, but I'd much rather own a dog."
number_dogs = message.count('dog')
message = "I like cats and dogs, but I'd much rather own a dog."
words = message.split(' ')
三. 元组 (Tuples)
元组可以被看成是不能改变的列表。列表是动态的,你可以增添,插入,删除,更改列表元素。有时我们需要这样的操作,但是有些时候我们需要保证有些数据是不能被用户或程序更改的。这就是元组的作用。
准确的说,列表是可变对象,元组是不可变对象。
元组的定义和列表相似,只是列表是用方括号定义,元组是用圆括号定义的。
colors = ('red', 'green', 'blue')
print("The first color is: " + colors[0])
四. 集合 (Sets)
集合对象是一系列无序的,离散的哈希对象的集合。常用于成员测试,移除重复元素和一些算术运算例如交,并,差和对称差等
shapes = ['circle', 'square', 'triangle', 'circle']
set_of_shapes = set(shapes)
shapes = {'circle', 'square', 'triangle', 'circle'}
for shape in shapes:
print(shape)
set_of_shapes.add('polygon')
print(set_of_shapes)
五. if、while、for 结构
用法同c++,不同点else if 结构变为 elif,且 if 后直接跟判断条件无需加括号。
desserts = ['ice cream', 'chocolate', 'apple crisp', 'cookies']
favorite_dessert = 'apple crisp'
hate_dessert = 'chocolate'
for dessert in desserts:
if dessert == favorite_dessert:
print("%s is my favorite dessert!" % dessert.title())
for dessert in desserts:
if dessert == favorite_dessert:
print("%s is my favorite dessert!" % dessert.title())
elif dessert == hate_dessert:
print("I hate %s." % dessert)
else:
print("I like %s." % dessert)
power = 5
while power > 0:
print("You are still playing, because your power is %d." % power)
power = power - 1
print("\nOh no, your power dropped to 0! Game Over.")
六. 输入和输出
在 py 中可以利用 input() 函数接受用户输入。//这里我觉得c++的输入输出是最简单的!! Java也有点麻烦qaq
names = ['guido', 'tim', 'jesse']
new_name = input("Please tell me someone I should know: ")
names.append(new_name)
print(names)
py中的输出采用c语言风格