Python學習----Day1

來源:互聯網
上載者:User

標籤:條件判斷   username   python學習   count   while   學習   world   互動式   else   

一:重點知識點概括

     1. Python的輸出與輸入函數---- print(), input()

     2. 條件判斷---- if else, elif

     3. 迴圈 ---- while, for

 

二: Python的輸出與輸入函數

      1. Python的輸出函數: print()  #列印輸出

          print()可以列印輸出字串、整數、數字計算結果,遇到逗號“,”會輸出一個空格。

              例如: >>> print("Hello world!")

                        Hello world!

                       >>> print("你好,世界!")

                        你好,世界!

                       >>> print("100")

                        100

                       >>> print(100+200)

                        300

                       >>> print("Hello world!", "100")

                        Hello world! 100

 

      2. Python的輸入函數: input()

          input()函數可以讓使用者輸入字串,並存放到一個變數裡。

               例如:>>> name = input("請輸入您的姓名:")

                      請輸入您的姓名:

          當你輸入name = input("請輸入您的姓名:") 並按下斷行符號後,Python互動式命令列就在等待你的輸入了。這時,你可以輸入任一字元,然後按斷行符號後完成輸入。輸入完成後,剛才輸入的內容就存放到變數name裡了。

          我們可以通過print()列印出name。

                例如: name = input("請輸入您的姓名:")

                         print(name)

         

三:條件判斷

     python中條件判斷語句是 if else,其執行形式如下:

     --------------------------------------------------

      if <條件1>:

          執行1

      else:

          執行2

     -----------------------------------------------------

      其意思是:如果if語句判斷是True,就把縮排的語句執行了。如果if判斷是False,不執行if的內容,執行else的內容。

      例如:

          name = "Jack"
          password = "king"


          name1 = input("Please input your usename:")
          password1 = input("Please input your password:")

          if name1 == name and password1 == password:
              print("Well done...")


          else:
              print("Wrong!!")

 

       如果想if判斷複雜一點,可以用elif語句,elif是else if的縮寫,其執行形式如下:

       --------------------------------------------------------------

       if <條件1>:

          執行1

       elif<條件2>:

          執行2

       elif<條件3>:

          執行3

       else:

          執行4

       ---------------------------------------------------------------  

       其意思是:如果if語句判斷是True,就把縮排的語句執行了。如果if判斷是False,則判斷elif的內容。說白了,就是,如果條件1滿足,就執行1,如果條件1不滿足,就判斷條件2是否滿足,以此類推。如果都不滿足,則執行else判斷。

       例如:

      name = "Jack"
      password = "king"


      name1 = input("Please input your username:")
      password1 = input("Please input your password:")

      if name1 == name and password1 == password:
          print("Well done...")


      elif name1 == name and password1 != password:
          print("密碼錯誤!")


      elif name1 != name and password1 == password:
          print("使用者名稱錯誤!")


      else:
          print("使用者名稱或密碼錯誤!")

 

四: while迴圈 與 for迴圈

       1. while迴圈語句用於迴圈執行程式,即在某條件下,迴圈執行某段程式,當條件為False時退出迴圈,並執行迴圈體後面的語句。

        例如:

    a = 0
while a < 5:
print(a)
a +=1

我們寫一個稍微複雜點的while迴圈,比如:
    age = 12
count = 0

while count<3:
age1 = int(input("please input your age:"))
if age1 == age:
print("Good")
break
elif age1 > age:
print("Bigger")
else:
print("Smaller")
count +=1

2. for迴圈語句。 for迴圈的形式是:for x in ...
例如:
       age = 12
for a in range(3):
age1 = int(input("please input your age:"))
if age1 == age:
print("Good")
break
elif age1 > age:
print("Bigger")
else:
print("Smaller")

===============================================================================================
小結: 慢慢學吧,道阻且長,行則將至。

 

Python學習----Day1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.