Python速成笔记(上)

一、输出

print("输出语句内容")

二、变量使用

a=5 #整型
b=5.2 #浮点型
c="床前明月光" #字符串

三、类数组

Python没有数组的概念,但有类似的东西存在

1.列表

    声明:

abc=["my","you"]

    调用:

>>> abc[0]
'my'

2.元组

    声明:

cde=("床前明月光","疑是地上霜")

    调用:

>>> cde[0]#注意中括号
'床前明月光

列表和元组的区别:

列表中的元素是可以修改的,但是元组这种数据类型是不能修改的。

3.集合

    声明:

a="窗前明月光1"
sa=set(a)
>>> sa 
{'光', '明', '1', '前', '窗', '月'}‘

集合运算:

a="窗前明月光1"
b="疑是地上霜1"
sa=set(a)
sb=set(b)
>>> sa&sb
{'1'}
>>> sa|sb
{'是', '光', '明', '疑', '地', '霜', '1', '前', '窗', '上', '月'}

4.字典

    声明:

 d1={"name":"ZYH","sex":"male"}

    调用:

>>> d1["sex"]
'male'

四、运算符

>>> 5+6
11
>>> p="python"
>>> "hello"+p
'hellopython'
>>> 4/3
1.3333333333333333
>>> 4//3
1
>>> 19//3
6
>>> 19%3
1
>>> (9+4)*5+5
70
>>> 9+(4*5)+5 #多此一举也比不知道强
34

五、控制流

1.选择:

a=6
if(a>7): #或者if a>7
    print(a)
elif(a<2):
    print(a)
else:
    print("nnn")  #最终输出"nnn"

 2.循环

(1)while

a=0
while a<8:  #或while(a<8)
    print("hello")
    a+=1   #最终输出了八次"hello"

(2)for

a=["a","c","b","d"]
for i in a:
    print(i)
#输出:
a
c
b
d
for i in range(0,8):#输出8次
    print("hello")
#输出:
hello
hello
hello
hello
hello
hello
hello
hello

中断形式:

for i in range(0,8):
    if(i==6):
        continue
    print(i)
#输出:
0
1
2
3
4
5
7
for i in range(0,8):
    if(i==3):
        break
    print(i)
#输出:
0
1
2

输出乘法口诀:

for i in range(1,10):
    for j in range(1,i+1):
        print(str(i)+"*"+str(j)+"="+str(i*j)+" ",end="")#end控制不换行
    print() #print自带换行

#输出:
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 

逆序输出乘法口诀:

for i in range(10,0,-1):
    for j in range(i,0,-1):
        print(str(i)+"*"+str(j)+"="+str(i*j)+" ",end="")#end控制不换行
    print() #print自带换行
#输出:
10*10=100 10*9=90 10*8=80 10*7=70 10*6=60 10*5=50 10*4=40 10*3=30 10*2=20 10*1=10 
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 








猜你喜欢

转载自blog.csdn.net/smart3s/article/details/80977362