python學習第一天

安裝環(huán)境

安裝pycharm

下載地址:http://www.jetbrains.com/pycharm/download/#section=windows

安裝anaconda

下載地址:https://www.anaconda.com/distribution/

python

  • python2.x
  • python3.x
  • python 2 與 3 不兼容

python 語法

輸入語句:

    input()
    input('')

輸出語句:

    print()
    print('{}'.format(i))

循環(huán)語句:

    while 要滿足的條件:
          要執(zhí)行的語句
      跳出循環(huán)
      break  跳出本層循環(huán)
      continue  跳出本次循環(huán),執(zhí)行下次循環(huán)

計算所有奇數(shù)的和

i = 1
sum = 0
while i <= 100:
    #  % 取模  //整除
    if i % 2 == 0:
        # 偶數(shù)時跳出
        i += 1
        continue
    sum += i
    i += 1
print(sum)

字符串格式化輸出

    hero_name = '魯班七號'
    grade = 15
    print('您選擇的英雄是{},當前等級為{}級'.format(hero_name,grade)

練手小程序

  1. 使用input輸入,使用if語句判斷變量值大小
age = input('請輸入您的年齡')
if int(age) >= 18:
    print('大于等于18')
i = 0
score = 88
if score >= 90 and score <= 100:
    print('您的成績等級為A')
elif score >= 80 and score < 90:
  1. 猜數(shù)字的小游戲,輸入范圍,randint產(chǎn)生隨機數(shù),根據(jù)提示猜數(shù)值
from random import randint
min_num = int(input('請輸入最小值'))
max_num = int(input('請輸入最大值'))

ran = randint(min_num,max_num)
i = 0
while True:
    user = int(input('請輸入你的猜測'))

    i += 1
    if user<ran:
        print('小了')
    elif user > ran:
        print('大了')
    elif user == ran:
        if i == 1:
            print('這是高手{}次猜對'.format(i))
        elif 2 < i and i < 5:
            print('你也太厲害了吧{}次猜對'.format(i))
        elif i > 5:
            print('你也太菜了{}次才猜對,洗洗睡吧'.format(i))
        break
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容