Python基础入门:从变量到异常处理
一、变量、运算符与数据类型
1、注释
在 Python 中,# 表示注释,作用于整行,即单行注释。
#这是一个注释
print("hello world")
‘’’ ‘’’ 或者 “”" “”" 表示区间注释,在三引号之间的所有内容被注释,即多行注释
'''
这是多行注释,用三个单引号
这是多行注释,用三个单引号
'''
print("hello python")
"""
这是多行注释,用三个双引号
这是多行注释,用三个双引号
"""
print("hello icpc")
2、运算符
算术运算符
操作符 | 名称 |
---|---|
+ | 加 |
- | 减 |
* | 乘 |
/ | 除 |
// | 整除 |
% | 取余 |
** | 幂 |
print(1 + 1) # 2
print(2 - 1) # 1
print(3 * 4) # 12
print(3 / 4) # 0.75
print(3 // 4) # 0
print(3 % 4) # 3
print(2 ** 3) # 8
比较运算符
操作符 | 名称 |
---|---|
> | 大于 |
>= | 大于等于 |
< | 小于 |
<= | 小于等于 |
== | 等于 |
!= | 不等于 |
print(2 > 1) # True
print(2 >= 4) # False
print(1 < 2) # True
print(5 <= 2) # False
print(3 == 4) # False
print(3 != 5) # True
逻辑运算符
操作符 | 名称 |
---|---|
and | 与 |
or | 或 |
not | 非 |
print((3 > 2) and (3 < 5)) #True
print((1 > 3) or (9 < 2)) # False
print(not (2 > 1)) # False
位运算符
操作符 | 名称 | 示例 |
---|---|---|
~ | 按位取反 | ~4 |
& | 按位与 | 4&5 |
^ | 按位异或 | 4^5 |
<< | 左移 | 4<<2 |
>> | 右移 | 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 | 存在 | ‘A’ in [‘A’,‘B’,‘C’] |
not in | 不存在 | ‘h’ not in [‘A’,‘B’,‘C’] |
is | 是 | “hello” is “hello” |
is not | 不是 | “hello” is not “hello” |
3、变量和赋值
- 在使用变量之前,需要对其先赋值。
- 变量名可以包括字母、数字、下划线、但变量名不能以数字开头。
- Python 变量名是大小写敏感的,foo != Foo。
二、位运算
1、原码、反码和补码
二进制有三种不同的表示形式:原码、反码和补码,计算机内部使用补码来表示。
原码:就是其二进制表示(注意,有一位符号位)。
00 00 00 11 -> 3
10 00 00 11 -> -3
反码:正数的反码就是原码,负数的反码是符号位不变,其余位取反(对应正数按位取反)。
00 00 00 11 -> 3
11 11 11 00 -> -3
补码:正数的补码就是原码,负数的补码是反码+1。
00 00 00 11 -> 3
11 11 11 01 -> -3
符号位:最高位为符号位,0表示正数,1表示负数。在位运算中符号位也参与运算。
2、按位运算
(1 按位与操作 &
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
(2 按位或操作 |
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0
(3 按位异或操作
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
(4 按位左移操作
num<<i表示将num的二进制表示向左移动i位所得的值。
00 00 10 11 -> 11
11 << 3
01 01 10 00 -> 88
(5按位右移操作
num >> i表示将num的二进制表示向右移动i位所得的值。
三、条件语句
1、if语句
if 2>1 and not 2>3:
print("Harden")
# Harden
2、if-else语句
temp=input("猜一猜我的球衣号码")
guess=int(temp)# input 函数将接收的任何数据类型都默认为 str。
if guess==13:
print("恭喜你猜对了")
else:
print("不好意思,猜错了")
print("游戏结束")
3、if-elif-else语句
temp = input('请输入成绩:')
source = int(temp)
if 100 >= source >= 90:
print('A')
elif 90 > source >= 80:
print('B')
elif 80 > source >= 60:
print('C')
elif 60 > source >= 0:
print('D')
else:
print('输入错误!')
四、循环语句
1、while循环
while语句最基本的形式包括一个位于顶部的布尔表达式,一个或多个属于while代码块的缩进语句。
while 布尔表达式:
代码块
string = 'abcd'
while string:
print(string)
string = string[1:]
# abcd
# bcd
# cd
# d
2、while-else循环
while 布尔表达式:
代码块
else:
代码块
当while循环正常执行完的情况下,执行else输出,如果while循环中执行了跳出循环的语句,比如 break,将不执行else代码块的内容。
count = 0
while count < 5:
print("%d is less than 5" % count)
count = count + 1
else:
print("%d is not less than 5" % count)
# 0 is less than 5
# 1 is less than 5
# 2 is less than 5
# 3 is less than 5
# 4 is less than 5
# 5 is not less than 5
3、for循环
for循环是迭代循环,在Python中相当于一个通用的序列迭代器,可以遍历任何有序序列,如str、list、tuple等,也可以遍历任何可迭代对象,如dict。
member = ['张三', '李四', '刘德华', '刘六', '周润发']
for each in member:
print(each)
# 张三
# 李四
# 刘德华
# 刘六
# 周润发
for i in range(len(member)):
print(member[i])
# 张三
# 李四
# 刘德华
# 刘六
# 周润发