初识python

python学习网 2018-07-30 22:28:01

自学人生的第一门编程语言,创此博客记录学习过程,刚开始学,内容相对简单,对像我这种小小小白来说很不错

python基础(一):

运算符:

    算术运算:

        除了基本的+ - * / 以外,还需要知道 :  // 为取整除 返回的市商的整数部分 例如: 9 // 2  ---> 4  , 9.0 //  2.0 --->4.0

                         %  为取余数  返回的是除法的余数  没有余数则为0  例如: 3 % 3 ---> 0 , 有余数:6 % 3 --->2

    逻辑运算符:

        and , or , not

    比较运算符:

         == 判断两个数是否相等 , != 判断两个数是否不相等 ,  > , < , >= ,<=

    成员运算符:

        

        list_1 =[1,2,3,4,5]

        in : 

          1 in list_1 --->   True
        not in :

          6 in list_1 --->  False

格式化字符 .format():

    控制精度:

        pi = 3.1415926
        print("圆周率精确到小数点后3位为:{0:.3f}".format(pi))
        圆周率精确到小数点后3位为:3.142

    填充字符:

        w_1 = "word"

        "{0:-^30}".format(w_1)  --->   '-------------word-------------'

if 判断 和 while for 循环:

while循环一般通过数值是否满足来确定循环的条件

for循环一般是对能保存多个数据的变量,进行遍历

 

list_2 = [1,2,3,4,5,66]
for i in list_2:
    print(i)

 

   乘法表()

i = 0
while i <=9:
    m = 1
    while m <= i:
        print('%d*%d=%d'%(m,i,m*i),end=' ')
        m +=1
    print('')

    i+=1

  猜拳

 1 import random
 2 
 3     player = input('请输入:剪刀(0)  石头(1)  布(2):')
 4 
 5     player = int(player)
 6 
 7     computer = random.randint(0,2)
 8 
 9     if ((player == 0) and (computer == 2)) or ((player ==1) and (computer == 0)) or ((player == 2) and (computer == 1)):
10         print('获胜,哈哈,你太厉害了')
11     elif player == computer:
12         print('平局,要不再来一局')
13     else:
14         print('输了,不要走,洗洗手接着来,决战到天亮')

break 和 continue

  break的作用:用来结束整个循环

  continue的作用:用来结束本次循环,紧接着执行下一次的循环

 

字符串,列表,元组,字典:

切片:

  • 切片是指对操作的对象截取其中一部分的操作。字符串、列表、元组都支持切片操作。

  • 切片的语法:[起始:结束:步长]

 

阅读(1885) 评论(0)