本蒻苟已粗略学习过c++基础,略去大部分相似且熟悉部分。
Python由荷兰数学和计算机科学研究学会的Guido van Rossum 于1990 年代初,出于打发时间的目的设计(这就是dalao吧),就连Python的名字也是源于当时他正在看的电视剧“Monty Python’s Flying Circus”。
(就是这位帅哥)
Python之禅
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!
(翻译自行百度)
Python是一种脚本语言,是解释执行的,不需要经过编译,所以很方便快捷,且能够很好地跨平台,写一些小工具小程序特别合适。
而C++则是一种需要编译后运行语言,在特定的机器上编译后在特定的机上运行,运行效率高,安全稳定。但编译后的程序一般是不跨平台的。
print( "let's go" )
print( 'lalala"lalala"lala' ) #没想到例子瞎写一个吧
print('''abc
xyz
ghi''')
#输出结果:
abc
xyz
ghi
person = """name # 姓名
age # 年龄
sex # 性别
"""
print(person)
#输出结果
name # 姓名
age # 年龄
sex # 性别
'''
多行注释
可用三引号
'''
-运算符优先级和结合性
操作符 | 名称 |
---|---|
// | 整除(地板除,向下取整) |
** | 乘方 |
例:
print(3 // 4) # 0
print(2 ** 3) # 8
print(3**2**3) # 6561
操作符 | 名称 |
---|---|
and | 与 |
or | 或 |
not | 非 |
例:
print((3 > 2) and (3 < 5)) # True
print((1 > 3) or (9 < 2)) # False
print(not (2 > 1)) # False
操作符 | 名称 |
---|---|
~ | 按位取反 |
& | 按位与 4 & 5 |
| | 按位或 |
^ | 按位异或 4 ^ 5 |
<< | 左移 4 << 2 |
>> | 右移 |
例:
print(bin(4)) # 0b100
print(bin(5)) # 0b101
print(bin(~4), ~4) # -0b101 -5
print(bin(4 & 5), 4 & 5) # 0b100 4
print(bin(4 | 5), 4 | 5) # 0b101 5
print(bin(4 ^ 5), 4 ^ 5) # 0b1 1
print(bin(4 << 2), 4 << 2) # 0b10000 16
print(bin(4 >> 2), 4 >> 2) # 0b1 1
例:
x, y = 4, 5
small = x if x < y else y
print(small) # 4
操作符 | 名称 |
---|---|
in | 存在 |
not in | 不存在 |
is | 是 |
is not | 不是 |
例:
letters = ['A', 'B', 'C']
if 'A' in letters:
print('A' + ' exists')
if 'h' not in letters:
print('h' + ' not exists')
# A exists
# h not exists
注:
print(type(1)) #
print(type(5.2)) #
print(type(True)) #
print(type('5.2')) #
print(isinstance(1, int)) # True
print(isinstance(5.2, float)) # True
print(isinstance(True, bool)) # True
print(isinstance('5.2', str)) # True
参考
运算符 | 描述 | 实例 |
---|---|---|
& | 按位与运算符:参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0 | (a & b) 输出结果 12 ,二进制解释: 0000 1100 |
| | 按位或运算符:只要对应的二个二进位有一个为1时,结果位就为1。 | (a | b) 输出结果 61 ,二进制解释: 0011 1101 |
^ | 按位异或运算符:当两对应的二进位相异时,结果为1 | (a ^ b) 输出结果 49 ,二进制解释: 0011 0001 |
~ | 按位取反运算符:对数据的每个二进制位取反,即把1变为0,把0变为1 。~x 类似于 -x-1 | (~a ) 输出结果 -61 ,二进制解释: 1100 0011,在一个有符号二进制数的补码形式。 |
<< | 左移动运算符:运算数的各二进位全部左移若干位,由 << 右边的数字指定了移动的位数,高位丢弃,低位补0。 | a << 2 输出结果 240 ,二进制解释: 1111 0000 |
>> | 右移动运算符:把">>"左边的运算数的各二进位全部右移若干位,>> 右边的数字指定了移动的位数 | a >> 2 输出结果 15 ,二进制解释: 0000 1111 |
例:
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0
c = a & b; # 12 = 0000 1100
print "1 - c 的值为:", c
c = a | b; # 61 = 0011 1101
print "2 - c 的值为:", c
c = a ^ b; # 49 = 0011 0001
print "3 - c 的值为:", c
c = ~a; # -61 = 1100 0011
print "4 - c 的值为:", c
c = a << 2; # 240 = 1111 0000
print "5 - c 的值为:", c
c = a >> 2; # 15 = 0000 1111
print "6 - c 的值为:", c
leetcode 习题 136. 只出现一次的数字
class Solution:
def singleNumber(self, nums: List[int]) -> int:
i = 0
for num in nums:
i ^= num
return i
'''
for 迭代变量 in 可迭代对象:
代码块
'''
for i in 'ILoveRM':
print(i, end=' ') # 不换行输出
#输出结果
# I L o v e R M
dic = {
'a': 1, 'b': 2, 'c': 3, 'd': 4}
for key, value in dic.items():
print(key, value, sep=':', end=' ')
#输出结果
# a:1 b:2 c:3 d:4
range([start,] stop[, step=1])
for i in range(1, 10, 2):
print(i)
#输出结果
# 1
# 3
# 5
# 7
# 9
enumerate(sequence, [start=0])
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
print(list(enumerate(seasons)))
#[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
#与for结合
seq = ['one', 'two', 'three']
for i, element in enumerate(seq, start = 1): #下标从 1 开始
print i, element
#1 one
#2 two
#3 three