Pythonの基礎2日目

2日目のメモ

1.whileサイクル

  • 基本的なメカニズム:
while 条件:
    循环体
while True:
    print("aloha")
    print("this is python")
    print("this is while")
  • 理論的根拠:

    • 最初の条件が真であるかどうかを判断します
    • Trueの場合、ループの本体の中へ
    • 下にループ本体の実行
    • 、条件を決定する条件が満たされた場合、ループ本体を再入力し続けます
  • どのようにループ終了へ

    • 変更の条件

      flag = True
      while flag:
          print("aloha")
          print("this is python")
          flag = False
          print("this is while")

      演習1:1ループ100は、すべての数字を出力します。

      count = 1
      while count <= 100:
          print(count)
          count += 1

      運動2:計算値1 + 2 + 3 + 4 + ... + 100

      result = 0
      count = 1
      while count <= 100:
          result += count
          count += 1
      print(result)
    • ブレーク

      サイクル遭遇したブレーク、終了ループ

      while True:
          print("aloha")
          print("this is python")
          break
          print("this is while")
      print("outside")

      演習:すべてのプリントアウト1〜100の偶数

      count = 2
      while True:
          print(count)
          count += 2
          if count > 100:
              break
    • システムコマンド

  • coutinue使い方

    サイクルに進み、ループの底部に直接続けると同等と

    flag = True
    while flag:
        print("aloha")
        print("this is python")
        flag = False
        continue
        print("this is while")
  • それ以外の使用中

    count = 1
    while count < 5:
        print(count)
        count += 1
    else:
        print("aloha")

    中断されたブレークした場合、else文が実行されています

    count = 1
    while count < 5:
        print(count)
        if count == 3:
            break
        count += 1
    else:
        print("aloha")
  • 演習:ユーザのログインシステムを作成するために、入力ミスに3回をユーザーに許可します

    count = 1
    while count <= 3:
        username = input("please enter your name:")
        password = input("please enter your password:")
        enter_code = input("please enter your code:")
        code = "abcd"
        chance_time = str(3 - count)
    
        if enter_code == code:
            if username == "yangsen":
                if password == "1234":
                    print("welcome python world!")
                else:
                    print("password error!")
            else:
                print("user does not exist!")
        else:
            print("code error!还剩" + chance_time + "次机会")
        count += 1
    else:
        print("错误3次,禁止登陆")

2.出力フォーマット

ストリングは、出力フォーマット考慮ダイナミック着信、最初のテイクに特定の場所をすることができ

name = input("请输入你的姓名:")
age = input("请输入你的年龄:")
job = input("请输入你的工作:")
hobbies = input("请输入你的爱好:")

# %-->占位符    s-->str
info = """
-----------%s的个人信息------------
姓名:%s
年龄:%s
工作:%s
爱好:%s
"""  % (name,name,age,job,hobbies)

print(info)

注意事項:出力形式、そこ%場合代わり必要性を示すためのプレースホルダとして使用するので、出力するコンテンツ%%%

以下のような:

msg ="我叫%s, 今年%s, 学习进度%s%%" % ('杨森',18,2)
print(msg)

3.オペレータ

算術演算子(+ - * /)、比較演算子(> =)、代入演算子(= + =)、論理演算子(AND OR)、オペレータのメンバー

i1 = 2
i2 = 3
print(2 ** 3)
print(10 // 3)
print(10 % 3)

print(3 != 4)

count = 1
count = count + 1
count += 1
print(count)
# and or not

# 1 在没有()的情况下,优先级:not > and > or,同一优先级从左至右依次计算
# 情况1:两边都是比较运算
# print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)
# print(True or False)

# 情况2:两边都是整数
'''
x or y , x为真,值就是x,x为假,值是y
'''
# print(1 or 2)
# print(3 or 2)
# print(4 or 2)
# print(-1 or 2)
# print(0 or 2)

# print(1 and 2)

データ型の変換

# str ---> int  : 只能是纯数字组成的字符串
s1 = '00100'
print(int(s1))
# int ----> str
i1 = 100
print(str(i1),type(str(i1)))

# int  ---> bool  : 非零即True ,0为False。
i = 0
print(bool(i))
# bool ---> int
print(int(True))  # 1
print(int(False))  # 0

4.エンコーディング知人

ASCII:文字、数字、特殊文字が含まれています。8ビット(1バイト)を占めます

GBK:文字、数字、特殊文字、中国語。文字は8ビット(1バイト)は、中国の16ビット(2バイト)を占め

ユニコード:全世界でのテキスト、中国語、英語や記号、32文字(4バイト)のために、各アカウントかどうかなどのUnicode、。スペースの廃棄物、資源の無駄。

UTF-8:Unicodeのバージョンをアップグレードする、英語は8(1バイト)を占め、ヨーロッパは中国の文字が24(3バイト)スタンド、16の文字(2バイト)を占めました

おすすめ

転載: www.cnblogs.com/west-yang/p/12529288.html