目录
Python 2.x与3.x
- 2.x的默认编码是ASSIC,默认不支持中文。而3.x的默认编码是UNICODE,默认支持中文
- 3.x不兼容2.x,新特性只在3.x上有
- 3.x的核心语法更加简单易学
变量
变量的命名规则
- 要具有描述性
- 变量名只能_,数字,字母组成,不可以是空格或特殊字符(#?<.,¥$*!~)
- 不能以中文为变量名
- 不能以数字开头
- 不能被使用保留字符
- 变量名严格区分大写
注释
单行注释用#,多行注释用三个单引号或三个双引号 ”’被注释的内容”’
另外,三个引号也可用于输入多行代码
msg = """a1
a2
a3
"""
output
a1
a2
a3
缩进
在python中,缩进级别必须保持一致,并且不要直接使用tab缩进
编码
- 支持中文的第一张表就叫 GB2312 ,之后出现了unicode
- unicode 支持所有国家和地区的编码 ,2**16 = 65535 = 存一个字符,每个字符统一占用2个字节
- unicode 向下兼容gb2312 , gbk
- UTF-8是unicode 的扩展集,可变长的字符编码集
- 编码出现顺序
ASSIC –>GB2312 ->GBK1.0–>GB18030
ASSIC –>unicode –>UTF-8
逻辑运算
and,且,并且
只有两个条件全部为True的时候, 结果才会为True
例如 1 and 0
为False,1 and 1
为True
And | True | False |
---|---|---|
True | True | False |
False | False | False |
or ,或,或者
只要有一个条件为True,则结果为Ture
例如1 or 0
为True,1 or 1
为True
Or | True | False |
---|---|---|
True | True | True |
False | True | False |
not,不
把False和True颠倒
例如not 5>3
为False,not 5<3
为True
短路原则
- 对于and 如果前面的第一个条件为假,那么这个and前后两个条件组成的表达式 的计算结果就一定为假,第二个条件就不会被计算
- 如果前面的第一个条件为真,那么这个or前后两个条件组成的表达式 的计算结果就一定为真,第二个条件就不会被计算
代码
三个数字比大小
num1 = int(input("Enter number_1>> "))
num2 = int(input("Enter number_2>> "))
num3 = int(input("Enter number_3>> "))
if num1 >= num2:
if num1 > num3:
print('Max number is', num1)
else:
print('Max number is', num3)
else:
if num2 > num3:
print('Max number is', num2)
else:
print('Max number is', num3)
output
Enter number_1>> 4
Enter number_2>> 76
Enter number_3>> 55
Max number is 76
根据用户需求输出#阵列
height = int(input("Height = "))
width = int(input("Width = "))#输入长宽
num_height = 1
while num_height <= height:
num_width = 1
while num_width <= width:
print("#", end='')#反复输出#
num_width += 1
num_height += 1
print()
输出倒三角形或三角形
- 倒三角形
line = int(input("Line ? "))
while line>0:
tmp = line
while tmp>0:
print("*", end=(''))
tmp -= 1
print("")
line -= 1
output
Line ? 5
*****
****
***
**
*
- 三角形
line = int(input("Line ? "))
line_2 = 1
while line_2<=line:
tmp = 1
while tmp <= line_2:
print("*", end=(''))
tmp += 1
print("")
line_2 += 1
output
Line ? 6
*
**
***
****
*****
******
九九乘法表
- 从大到小
first = 9
second = 9
while first > 0:
while second > 0:
tmp = first * second
print(str(first)+"*"+str(second)+"="+str(tmp), end=" ")
second -= 1
first -= 1
second = first
print('')
output
9*9=81 9*8=72 9*7=63 9*6=54 9*5=45 9*4=36 9*3=27 9*2=18 9*1=9
8*8=64 8*7=56 8*6=48 8*5=40 8*4=32 8*3=24 8*2=16 8*1=8
7*7=49 7*6=42 7*5=35 7*4=28 7*3=21 7*2=14 7*1=7
6*6=36 6*5=30 6*4=24 6*3=18 6*2=12 6*1=6
5*5=25 5*4=20 5*3=15 5*2=10 5*1=5
4*4=16 4*3=12 4*2=8 4*1=4
3*3=9 3*2=6 3*1=3
2*2=4 2*1=2
1*1=1
-从小到大
first = 1
while first <= 9:
second = 1
while second <= first:
tmp = first * second
print(str(first)+"*"+str(second)+"="+str(tmp), end=" ")
second += 1
first += 1
print('')
output
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81