Python之禪基礎(一)--基礎&&條件判斷__Python

來源:互聯網
上載者:User

一.注釋->1.#號注釋;2.'''來作為注釋;

二.Python2中只要有中文,只要中文是程式的一部分,包括注釋,就需要在程式中加上:#coding=utf-8或者#-*-coding=utf-8-*-

三.print的佔位輸入:print("%d",%age);        print("%s",%name)

四.輸入:py2:raw_input("請輸入:")                  py3:input("請輸入:")

    input 輸入進來的是str類型;如果是數位話記得int強制轉化;


五.運算子+-*/;其中:運算子//表示的意思為取整,即取商; **表示的是次方;

六.若有多個%s以上的空需要值來填,那麼在後面需要有%加小括弧;

     eg:  print("姓名是:%s,年齡是:%d,地址是:%s",%(name,age,addr))

七.條件判斷:

    if  條件一:

          ......

    else  條件二:

         .......

    elif   條件三:

        .......

    else:

       .....

八. 如果想讓print列印出的結果不換行,就在小括弧裡面加上“end=”

    while j<=5:  

        print("*",end="")

        j+=1

   print("")


九.列印三角形

    思路:複雜問題簡單化,先研究簡單的套路,然後一步一步走。

    先實現列印5行,再實現每行中列印的依次遞減;

  

i=1while i<=5:  j=1  while j<=i:    print("*",end="")    j+=1  print("")  i+=1

十.九九乘法表

    print('\t')的使用

i=1while i<=9:  j=1  while j<=i:    print("%d*%d=%d\t"%(j,i,i*j),end="")    j+=1  print("")  i+=1

十一.簡單的剪刀石頭布

   random.randint(0,2)  //產生隨機的0-2之間的數字

import random#1. 提示並擷取使用者的輸入player = int(input("請輸入 0剪刀 1石頭 2布:"))#2. 讓電腦出一個computer = random.randint(0,2)#2. 判斷使用者的輸入,然後顯示對應的結果#if 玩家獲勝的條件:if (player==0 and computer==2) or (player==1 and computer==0) or (player==2 and computer==1):    print("贏了,,,,可以去買奶粉了.....")#elif 玩家平局的條件:elif player==computer:    print("平局了,,,洗洗手決戰到天亮....")else:    print("輸了,,,回家拿錢 再來....")

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.