• Python3 basic syntax


    coding

    ডিফল্টরূপে পাইথন 3 উত্স ফাইলগুলি UTF-8  এ এনকোড করা আছে এবং সমস্ত স্ট্রিং ইউনিকোড স্ট্রিং। অবশ্যই, আপনি উত্স ফাইলগুলির জন্য বিভিন্ন এনকোডিংগুলিও নির্দিষ্ট করতে পারেন:

    #-*-coding: cp-1252-*-
    উপরোক্ত সংজ্ঞাটি সোর্স ফাইলে সেট করা উইন্ডোজ -1252 অক্ষরে অক্ষর এনকোডিং ব্যবহারের অনুমতি দেয় এবং সম্পর্কিত উপযুক্ত ভাষা হ'ল বুলগেরিয়ান, বেলারুশিয়ান, ম্যাসেডোনিয়ান, রাশিয়ান, সার্বিয়ান।


    আইডেন্টিফাইয়ার

    • প্রথম অক্ষরটি বর্ণমালার একটি বর্ণ বা আন্ডারস্কোর _ হতে হবে 
    • বাকি শনাক্তকারীগুলিতে বর্ণ, সংখ্যা এবং আন্ডারস্কোর থাকে।
    • শনাক্তকারীরা কেস সংবেদনশীল।
    পাইথন 3-এ, চাইনিজগুলি একটি পরিবর্তনশীল নাম হিসাবে ব্যবহার করা যেতে পারে এবং নন-এএসসিআইআই শনাক্তকারীদেরও অনুমোদিত।


    python reserved words

    সংরক্ষিত শব্দগুলি হ'ল মূলশব্দ এবং আমরা সেগুলি কোনও সনাক্তকারী নাম হিসাবে ব্যবহার করতে পারি না। পাইথনের স্ট্যান্ডার্ড লাইব্রেরি একটি কীওয়ার্ড মডিউল সরবরাহ করে যা বর্তমান সংস্করণের জন্য সমস্ত কীওয়ার্ডকে আউটপুট করে:


    >>> import keyword
    >>> keyword.kwlist
    ['False', 'None', 'True', 'and', 'as',' assert ',' break ',' class', 'continue', 'def', 'del', 'elif', ' else ',' except ',' finally ',' for ',' from ',' global ',' if ',' import ',' in ',' is', 'lambda', 'nonlocal', 'not' , 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

    Comment

    Single-line comments in Python start with # , examples are as follows:

    Examples (Python 3.0+)

    #! / usr / bin / python3 # first comment print ( " Hello, Python! " ) # second comment
    Execute the above code, the output is:
    Hello, Python!
    Multi-line comments can use multiple # signs, as well as ' '' and "" " :

    Examples (Python 3.0+)

    #! / usr / bin / python3 #first comment # second comment '' ' third comment fourth comment ' '' "" " fifth comment sixth comment " "" print ( " Hello, Python! " )
    Execute the above code, the output is:
    Hello, Python!

    Line and indent


    The most distinctive feature of Python is the use of indentation to represent code blocks, without the need for braces {} .
    The number of indented spaces is variable, but statements in the same code block must contain the same number of indented spaces. Examples are as follows:

    Examples (Python 3.0+)

    if True : print ( " True " ) else : print ( " False " )
    The number of spaces indented in the last line of the following code is inconsistent, which will cause a running error:
    if True: True :
        print ("Answer")print ( "Answer" ) 
        print ("True")print ( "True" ) 
    else:else :
        print ("Answer")print ( "Answer" ) 
      print ("False") # Indentation is inconsistent, which will cause a running errorprint ( "False" ) # Indentation is inconsistent, leading to error     
    
    Due to inconsistent indentation of the above program, an error similar to the following will occur after execution:
     File "test.py", line 6File "test.py" , line 6 
        print ("False") # Indentation is inconsistent, which will cause a running errorprint ( "False" ) # Indentation is inconsistent, leading to error     
                                          ^^
    IndentationError: unindent does not match any outer indentation levelIndentationError : unindent does not match any outer indentation level
    

    Multi-line statement

    Python usually writes a statement in one line, but if the statement is very long, we can use a backslash (\) to implement a multi-line statement, for example:
    total = item_one + \= item_one + \
            item_two + \+ \
            item_three
    
    Multi-line statements in [], {}, or () do not require a backslash (\), for example:
    total = ['item_one', 'item_two', 'item_three',= [ 'item_one' , 'item_two' , 'item_three' ,   
            'item_four', 'item_five']'item_four' , 'item_five' ] 
    

    Number type

    There are four types of numbers in Python: integers, booleans, floating-point numbers, and complex numbers.
    • int (integer), such as 1, has only one integer type int, expressed as a long integer, without Long in python2.
    • bool , such as True.
    • float (floating point number), such as 1.23, 3E-2
    • complex (e.g. 1 + 2j, 1.1 + 2.2j

    String

    • Single and double quotes are exactly the same in Python.
    • Use triple quotes ('' 'or "" ") to specify a multi-line string.
    • Escapes'\'
    • Backslashes can be used for escaping. Use r to prevent backslashes from being escaped. If r "this is a line with \ n" then \ n will be displayed, not a new line.
    • Concatenate strings literally, such as "this" "is" "string" will be automatically converted to this is string.
    • Strings can be concatenated with the + operator and repeated with the * operator.
    • There are two ways to index strings in Python, starting with 0 from left to right and -1 from right to left.
    • Strings in Python cannot be changed.
    • Python does not have a separate character type, a character is a string of length 1.
    • The syntax format of the string interception is as follows: variable [head subscript: tail subscript: step length]
    word = 'string'= 'String' 
    sentence = "This is a sentence."= "This is a sentence." 
    paragraph = "" "This is a paragraph.= "" "This is a paragraph, 
    Can consist of multiple lines "" "
    

    Examples (Python 3.0+)

    #! / usr / bin / python3 str = ' Runoob ' print ( str ) # print the string print ( str [ 0 : -1 ] ) # print all characters from the first to the second to last print ( str [ 0 ] ) # Print the first character of the string print ( str [ 2 : 5 ] ) # print the characters from the third to the fifth print ( str [ 2 : ] ) # print all the characters from the third print ( str * 2 ) # print the string twice print ( str + ' hello ' ) # connect the string print ( ' -------------------------- ---- ' ) print ( ' hello \ n runoob ' ) # Use the backslash (\) + n to escape the special character print ( r ' hello \ n runoob ' ) # Add an r in front of the string to indicate the original String, no escape
    Here r refers to raw, which is raw string.
    The output is:
    Runoob
    RunooRunoo
    R
    noo
    noob
    RunoobRunoobRunoobRunoob
    Hello RunoobHello Runoob
    ------------------------------------------------------------
    hello
    runoob
    hello \ nrunoob

    Blank line

    new piece of code  কোডের সূচনা বোঝাতে কোনও শ্রেণীর ফাংশন বা পদ্ধতিগুলির মধ্যে পৃথক পৃথক রেখাগুলি। ক্লাস এবং ফাংশন এন্ট্রি এছাড়াও ফাঁকা রেখা দ্বারা পৃথক করা হয় ফাংশন প্রবেশের সূচনাটি হাইলাইট করার জন্য। ফাঁকা কোড     ইন্ডেন্টেশনের বিপরীতে, ফাঁকা লাইন পাইথন সিনট্যাক্সের অংশ নয়। লেখার সময় কোনও ফাঁকা লাইন inserted হয় না    এবং পাইথন ইন্টারপ্রেটারটি ত্রুটি ছাড়াই চলে। যাইহোক, ফাঁকা লাইনের কাজটি বিভিন্ন ফাংশন বা অর্থ সহ কোডের দুটি টুকরা পৃথক করা যা ভবিষ্যতে রক্ষণাবেক্ষণ বা কোডটির পুনর্নির্মাণের জন্য সুবিধাজনক।
    মনে রাখবেন: ফাঁকা লাইনগুলিও প্রোগ্রাম কোডের অংশ।

    Waiting for user input

    Run the following program and wait for user input after pressing Enter:

    Examples (Python 3.0+)

    #! / usr / bin / python3 input ( " \ n \ nPress enter to exit. " )
    In the above code, "\ n \ n" will output two new blank lines before the result is output. Once the user presses enter, the program will exit.

    Display multiple statements on the same line

    Python can use multiple statements on the same line, separated by semicolons (;), the following is a simple example:

    Examples (Python 3.0+)

    #! / usr / bin / python3 import sys ; x = ' runoob ' ; sys . stdout . write ( x + ' \ n ' )
    Using the script to execute the above code, the output is:
    runoob
    Using interactive command line execution, the output is:
    >>> import sys; x = 'runoob'; sys.stdout.write (x + '\ n') import sys ; x = 'runoob' ; sys . stdout . write ( x + '\ n' )  
    runoob
    77
    
    Here 7 is the number of characters.

    Multiple statements form a code group

    একই সেট স্টেটমেন্টের ইনডেন্ট করা একটি কোড ব্লক তৈরি করে, যা আমরা একটি কোড গ্রুপ বলি।
    যৌগিক বিবৃতি যেমন যেমন, যখন, def, and class, জন্য, প্রথম লাইনটি একটি কীওয়ার্ড দিয়ে শুরু হয় এবং কোলন (:) দিয়ে শেষ হয়  এই লাইনের পরে এক বা একাধিক লাইন একটি কোড গ্রুপ গঠন করে।
    আমরা কোডের প্রথম এবং পরবর্তী লাইনগুলিকে একটি ধারা/clause বলি।
    নিম্নলিখিত উদাহরণগুলি:

    if expression: expression : 
       suite
    elif expression: elif expression : 
       suite 
    else: else :  
       suite
    যদি প্রকাশ : 
       স্যুট
    এলিফ এক্সপ্রেশন : 
       স্যুট 
    অন্য :  
       স্যুট 

    Print output

    The default output of print is line feed. If you want to achieve no line feed, you need to add end = "" to the end of the variable :

    Examples (Python 3.0+)

    #! / usr / bin / python3 x = " a " y = " b " # line feed print ( x ) print ( y ) print ( ' --------- ' ) # line feed print ( x , end = " " ) print ( y , end = " " ) print ( )


    The execution result of the above example is:
    a
    b
    ------------------
    ab

    import and from ... import


    সম্পর্কিত মডিউল আমদানি করতে পাইথন import or from ... import ব্যবহার করুন 
    সম্পূর্ণ মডিউলটি (সামোডিয়ুল) বিন্যাসে আমদানি করুন : import some module
    বিন্যাসে একটি মডিউল থেকে একটি ফাংশন আমদানি করুন: from somemodule import somefunction/somemodule আমদানি থেকে কিছু কাজ করুন
    ফর্ম্যাটটিতে একটি মডিউল থেকে একাধিক ফাংশন আমদানি করুন: from somemodule import firstfunc, secondfunc, thirdfunc/samemodule আমদানি থেকে ফার্স্টফঙ্ক, সেকেন্ডফানক, থার্ডফান্ড
    বিন্যাসে মডিউলে সমস্ত ফাংশন আমদানি করুন: from somemodule import *

    mport sys module

    import sys print ( ' ================== Python import mode =============================== ' ) Print ( ' command line parameters: ' ) for I in SYS . the argv : Print ( I ) Print ( ' \ n- Python path ' , SYS . path )

    Import argv, path members of the sys module

    from sys import argv , path # Import specific members print ( ' ================= python from import =================== =================== ' ) print ( ' path: ' , path ) # Because the path member has been imported, you do not need to add sys.path for reference here


    Command line arguments

    কয়েকটি প্রোগ্রাম কিছু প্রাথমিক তথ্য দেখতে কিছু ক্রিয়াকলাপ করতে পারে। পাইথন প্রতিটি প্যারামিটারের সহায়তা তথ্য দেখতে -h প্যারামিটার ব্যবহার করতে পারে:

    $ python -h- H
    usage: python [option] ... [-c cmd | -m mod | file |-] [arg] ...: Python [ the Option ] ... [- c cmd | - m MOD | File | -] [ Arg ] ...      
    Options and arguments (and corresponding environment variables):Options and arguments ( and corresponding environment variables ): 
    -c cmd: program passed in as string (terminates option list)- c cmd : Program passed in AS String ( Terminates the Option List )   
    -d: debug output from parser (also PYTHONDEBUG = x)- D      : Debug Output from Parser ( Also PythonDebug = X )
    -E: ignore environment variables (such as PYTHONPATH)- E      : the ignore the Variables Environment ( SUCH AS the PYTHONPATH )
    -h: print this help message and exit- H      : Print the this Help the Message and Exit   
    
    [etc.][ Etc . ] 

    When we execute Python in script form, we can accept the parameters input from the command line. For specific usage, refer to Python 3 command line parameters .





  • 0 comments:

    Post a Comment

    New Research

    Attention Mechanism Based Multi Feature Fusion Forest for Hyperspectral Image Classification.

    CBS-GAN: A Band Selection Based Generative Adversarial Net for Hyperspectral Sample Generation.

    Multi-feature Fusion based Deep Forest for Hyperspectral Image Classification.

    ADDRESS

    388 Lumo Rd, Hongshan, Wuhan, Hubei, China

    EMAIL

    contact-m.zamanb@yahoo.com
    mostofa.zaman@cug.edu.cn

    TELEPHONE

    #
    #

    MOBILE

    +8615527370302,
    +8807171546477