2020元旦的python学习

效率好低今天(我爱屁股,毕竟今天好歹爬上黄金了hhh)

总结一下今天学的东西吧

+

-

*

/

//

整除(取商)

%

取余

**

指数(x**y=xy

1、+可以用来连接多个字符串

2、

%s表示字符串,

%d表示整数,在整数前加空格可以写成%5d  %123会表示为“ 123”共占5个字符位置

%f用于格式化浮点数%4.2f来格式化,表示总长度为4, 2表示两个小数位

3、
1 a='This is {0:s} {1:d}st python program.'.format ('Max', 1)
2 
3 print(a)
4、列表
userAge=[21,22,23,24,25]

a=userAge[0]#取出第0个元素

b=userAge[2:4]#取出[2,4)总是左闭右开

c=userAge[1:5:2]#取出[1,5)步长为2

#修改元素可以直接

userAge[1]=5

d=userAge

#末尾添加元素使用append()函数

userAge.append(99)

e=userAge

#删除元素

del userAge[2]

f=userAge

print(a)

print(b)

print(c)

print(d)

print(e)

print(f)

5、元组

与列表很像但是用()不用[]。另外你不能修改元组的值

6、字典

使用{}

userNameAndAge={"Peter":38, "Max":18,"Dell":19}#dict可用可不用,声明字典

#写成userNameAndAge=dict(Peter=38,Max=18,Dell=19)

a=userNameAndAge["Peter"]

print(a)

userNameAndAge["Peter"]=39#修改字典

del userNameAndAge["Peter"]#删除Peter选项

7、交互程序:Input、print

myName=input("Please enter ur name")
print("Hello World, my name is ",myName)

 

8、扩展多行:三引号

print('''Hello World
My name is Max and
I love Dell.
''')

9、转义

\n打印新行

\t制表符

10、if条件

①If condition 1 is met:

       do A

elif #else if condition 2 is met:

       do B

python依托tab来确定程序开始位置

②do Task A if condition is true else do Task

e.g.

        num1=12 if myInt==10 else 13 

11、for循环

pets=['cats','dogs','rabbits']
for index, myPets in enumerate(pets):#enumerate用来显示列表中的索引值&&另外别忘了:这个玩意
        print(index,myPets)

12、while循环

While condition is true:

       do A

e.g.

counter=5
while counter>0:
    print("Counter = ", counter)
    counter=counter-1

Break用法不再赘述

Continue关键字后的程序会在本次循环中被跳过

j=0
for i in range(5):
    j=j+2
    print('\ni = ', i, ', j = ', j)
    if j==6:
        continue
    print('I will be skipped over if j=6')

13、try &except

try :

       do something

except:

       do something else when an error occurs.

try:
    answer =12/0
    print(answer)
except:
    print("An error occured")

猜你喜欢

转载自www.cnblogs.com/DMax/p/12130987.html