Python 101 :究極のPythonガイド


Pythonは高レベルのプログラミング言語であり、素人言語では、それは機械に比べて人間の理解に近い.Pythonは解釈言語と汎用プログラミング言語です.
Pythonの簡単な歴史
Pythonは、1980年代に生命に入ったプログラミング言語であり、それは、ABCのプログラミング言語の後継者であるイデオロギーとオランダでCentrum Wiskunde&Informatica(CWI)でGuido Vo Rossumによって開発されました.
それ以来、Pythonは最近のバージョンのPython 3.10(この記事を執筆する時点で)の最初のリリース日(2021年10月4日)でいくつかのバージョンを通じて進化しました
パイソン2.0は2000年10月16日にリリースされました.そして、Unicodeのためのメモリ管理とサポートのための循環検出ガベージコレクターを含みます.
2008年12月3日にリリースされました.このバージョンでは、以前のPythonバージョンとの後方互換性がなかったという大きな修正がありました.
手を汚す前に、約束のルールを決めましょう!
Pythonの構文
Pythonは非常にシンプルで使いやすい構文を持って、あなたは理解しやすくシンプルに見つけることができます.
以下を含みます:
  • Pythonのインデントは重要です、それはあなたのコードを破ることができます!
  • Pythonは大文字小文字を区別し、状態と状態は異なります
  • すべての変数は小文字から始まります
  • クラスは大文字で始まる
  • 標準は実際にプログラミングで英語名を使うことです
  • 識別子(変数、型、関数、およびラベルのために定義する名前)は、Pythonのキーワードではなく、シンボルや数字から始めるべきです.
    スタイルとベストプラクティスの詳細についてはPythonでチェックアウトPEP-8
    Pythonのインストール
  • On windows machine
  • On linux machine
  • On MacOS
  • Pythonの基礎


    基底タイプ
  • int (整数)
  • value = 76
    
  • フロート
  • value = 56.21
    
  • bool ( boolean )
  • is_blue = True
    
  • str ( string )
  • message = "Hello world!"
    
    文字をエスケープするためにバックスラッシュを使用します.
    など
  • \' - シングル引用
  • \バックスペース
  • \n -新しい行
  • \キャリッジリターン
  • \T -タブ
  • message = " \t Sammy\'s cat ran away\nand into the bush it went.\n"
    # print - an inbuilt function to print an output
    print(message)
    
    # output
    #     Sammy's cat ran away
    # and into the bush it went.
    
    マルチライン文字列
       message = """ Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
       sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
       Ut enim ad minim veniam, 
       quis nostrud exercitation ullamco laboris nisi ut 
       aliquip ex ea commodo consequat."""
    
    シーケンス型
  • リスト
  • リストは変更可能であり、異なるタイプの値を保持することができます.
    list_country = ["Russia",  "South Africa", "Japan", "Peru"]
    random_list  = ["x", 3.14, 4, False, "No way"]
    
  • タプル
  • タプルは一度作成することはできません.それらは不変です.
    coordinates = (33.56, 4.56, 21.89)
    
  • 範囲
  • は一連の数を返す関数で、0から始まり、デフォルトで1ずつ増加します.
    フォーマット:範囲( start , stop , step )
    for x in range(0,10,2):
        print(x)
    
    マッピングタイプ
    辞書
    キー値ペアに格納されているデータのコレクションです
    “ラップトップ”はキーと“Macブック”値です.
    backpack = {"Laptop":"Mac book", "Headphones": "Sony",  "Hard drive": "SSD"}
    
    型を設定する
  • セット
  • セットは、変更可能で、iterableで、ユニークな要素を持っている順序がないコレクションです.
    fruits = {"apple", "mango", "orange"}
    
    2セット
    要素を凍結し、変更できない.
    fruits = ({"apple, "mango", "orange"})
    

    変換


    つのデータ型の値を別の型に変更します.
    """ 1.str to int"""
    int("10") #output: 10
    
    """ 2. float to int"""
    int(10.34) #output: 10
    
    """ 3. any data type to str"""
    str(10.10) #output: "10.1"
    str(3==3) #output:  "True"
    
    """ 4. character to ASCII code """
    ord('*') #output: 42
    
    """ 5. string to list"""
    message = "Hello world!"
    message_list = list(message)
    # print- print output
    print(message_list)
    # output
    # ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']
    
    """ 6. dictionary to list """
    day = {"day":14, "month": "Feb", "year": 2022}
    # create a list with list compression
    generated_list = [(x, y) for x, y  in day.items()]
    # print - print output
    print(generated_list)
    # output
    # [('day', 14), ('month', 'Feb'), ('year', 2022)]
    
    """ 7. list to string """
    word_list = ["The", "new", "version", "of", "slack", "is", "amazing"]
    word_str = " ".join(word_list)
    print(word_str)
    # output
    # The new version of slack is amazing
    
    """ 8. list to set """
    number_list = [34, 45, 67 ,87]
    number_set = set(number_list)
    print(number_set)
    # output
    # {34, 67, 45, 87}
    
    """ 9. list to dictionary """
    day_list = [('day', 14), ('month', 'Feb'), ('year', 2022)]
    day_dict = dict(day_list)
    print(day_dict)
    # output
    # {"day":14, "month": "Feb", "year": 2022}
    

    文字列の操作


    文字列を操作できるいくつかの方法があります.
    print("Hello")
    
    # output
    # Hello
    
    print("Hello", "world", "!")
    
    # output
    # Hello world !
    
    print("Hello", "world", sep="-")
    
    # output
    # Hello-world
    
    print("Hello") # for every print, output is set to a new line
    print("world")
    
    # output
    # Hello
    # World
    
    print("Hello", "world", sep="-", end="!") 
    
    # output
    # Hello-world!
    
    print("Hello", end=" ") # without new line
    print("World")
    
    # output
    # Hello world
    

    文字列連結


    文字列連結は、文字列をend - to - endに結合する操作です.ウィキペディア
    first_name = "John"
    middle_name = "Magarita"
    last_name = "Omondi"
    
    print(first_name + " " + middle_name + " " + last_name)
    
    # output
    # John Magarita Omondi
    

    文字列形式


    first_name = "John"
    middle_name = "Magarita"
    last_name = "Omondi"
    salary = 50000
    print("{0} {1} earns a salary of ${2}".format(first_name, middle_name, salary))
    
    # output
    # John Magarita earns a salary of $50000
    
    Pythonの基礎を知ることで、簡単なコマンドラインプログラムを書くことができます
    例えば、

    簡単なマッドLIBSジェネレータプログラム


    """ A simple word game that takes user input based on the word type and 
    inserting in to a paragraph """
    
    noun1 = input("Enter a noun(Plural): ")
    place = input("Enter a place: ")
    noun2 = input("Enter a noun: ")
    noun3 = input("Enter a noun(Plural): ")
    adjective1 = input("Enter an Adjective: ")
    verb1 = input("Enter a verb: ")
    verb2 = input("Enter a verb: ")
    number = input("Enter a number: ")
    adjective2 = input("Enter an Adjective: ")
    body_part = input("Enter a body part: ")
    verb3 = input("Enter a verb: ")
    
    mad_lib = """Two {0}, both alike in dignity,
    In fair {1} where we lay our scene,
    From ancient {2} break to new mutiny,
    Where civil blood makes civil hands unclean.
    From forth the fatal loins of these two foes
    A pair of star-cross`d {3} take their life;
    Whole misadventured piteous overthrows
    Do with their {4} bury their parents` strife.
    The fearful passage of their {5} love,
    And the continuance of their parents` rage,
    Which, but their children`s end, nought could {6},
    Is now the {7} hours` traffic of our stage;
    The which if you with {8} {9} attend,
    What here shall {10}, our toil shall strive to mend. """.format(noun1, place, noun2, noun3, adjective1, verb1, verb2,number, adjective2,body_part, verb3)
    
    print("\n====Mad Lib (Romeo and Juliet: Prologue Ad-lib) ====\n")
    print(mad_lib)
    
    結論
    Pythonは、スクリプトやデータサイエンスにAIから技術のほとんどすべての分野で使用することができる素晴らしい多目的プログラミング言語です.最良の部分は、それを学ぶのは楽しいし、ユーザーフレンドリーです.
    ハッピーコーディング!