Some basic knowledge of getting started with Python programming

Source: Internet
Author: User
Python has many similarities with Perl,c and the Java language. There are, however, some clear distinctions between languages. The purpose of this chapter is to allow you to quickly learn the syntax of Python.
First Python program:
Interactive mode Programming:

The call interpreter does not pass the script file as a parameter and displays the following prompt:

$ Pythonpython 2.6.4 (#1, Nov, 13:34:43) [GCC 4.1.2 20120704 (Red Hat 5.6.2-48)] on Linux2type "help", "copyright", "Credits" or "license" for more information.>>>

Type the following text at the Python prompt, and then press ENTER:

>>> print "Hello, python!";

If you are running a new Python version, then you need to use the PRINT statement brackets like print ("Hello, python!");. However, in Python version 2.6.4, this produces the following results:

Hello, python!.

Script Mode Programming:

Invokes the interpreter and script as a parameter, and starts executing the script and continues until the script finishes. When the script is complete, the interpreter is no longer active.

Let's write a simple Python program in the script. All Python files will have a. py extension. So, write the following code in a test.py file.

Print "Hello, python!";

Here, I assume that you have set up the Python interpreter in the path variable. Now, try running this program as follows:

$ python test.py

This will produce the following results:

Hello, python!.

Let's try another way to execute a python script. The following is the modified test.py file:

#!/usr/bin/pythonprint "Hello, python!";

Here, assume that the Python interpreter is available in the/usr/bin directory. Now, try running this program as follows:

$ chmod +x test.py # This was to make   file executable$./test.py

This will produce the following results:

Hello, python!.

Python identifier:

A python identifier is a name used to identify a variable, function, class, module, or other object. An identifier begins with the letter A to Z or A?z or followed by 0 or more letter underscores (_), underscores, and numbers (0?9).

Punctuation is not allowed within identifiers in Python, such as @,$ and%. Python is a case-sensitive programming language. Therefore, Manpower and Manpower are two different identifiers in Python.

Here is the Python identifier naming convention:

    • The class name is in uppercase letters and all other identifiers in lowercase letters.
    • An identifier that begins with a single leading underscore is represented by the identifier convention meaning private.
    • The identifier that begins with two leading underscores represents a strong private identifier.
    • If the end of the identifier also has two underscores at the end, the identifier is a language-defined special name.

Reserved words:

The following is a list of reserved words in Python. These reserved words cannot be used as constants or variables, or as any other identifier. All Python keywords contain only lowercase letters.

Line and indent:

When a programmer learns Python, the first area to be aware of is the class and function definition block or process control that does not use parentheses to represent the code. Code blocks are indented by lines, which are strictly performed representations.

The number of indents is variable, but all statements in the block must be indented the same amount. In this example, two function blocks are well-used:

If true:  print "True" Else:print "False"

However, the second block in this embodiment will produce an error:

If true: print "  Answer"  print "True" Else:  print "Answer" print "False"

As a result, the number of spaces indented in Python for all successive lines will also be formed into blocks. The following are examples of various statement blocks:

Note: Do not attempt to understand the logic used or the different functions. Just make sure you understand that even if they have various modules without parentheses.

#!/usr/bin/pythonimport systry: # Open File Stream file = open (file_name, "w") except Ioerror:print "There is an error WR Iting to ", file_name sys.exit () print" Enter ", File_finish,print" ' When finished "while file_text! = File_finish:file_te XT = Raw_input ("Enter text:") if File_text = = file_finish:  # Close the file  file.close break  file.write (file_  Text) file.write ("\ n") file.close () file_name = raw_input ("Enter filename:") If Len (file_name) = = 0:print "Next Time" Enter Something "sys.exit () try:file = open (file_name," R ") except Ioerror:print" There is an error reading file "Sys.ex It () File_text = File.read () file.close () print File_text

Multi-line statements:

Python statements usually end with a new line. However, Python allows you to use the continuation character (\) to indicate that the line should continue (across rows). For example:

Total = Item_one + \    Item_two + \    item_three

Statements contained within [],{} or () brackets do not need to use a continuation character. For example:

days = [' Monday ', ' Tuesday ', ' Wednesday ',    ' Thursday ', ' Friday ']

Python quotes:

Python accepts single quotes ('), double quotation marks ("), and three (" or "") references to denote string constants as long as they are the same type of quotation marks that start and end the string.

Sanchong quotes can be used for strings that span multiple lines. For example, all of the following are legal:

Word = ' word ' sentence = "This is a sentence." Paragraph = "" "This is a paragraph. It ismade up of multiple lines and sentences. "" "

Python Comments:

A pound sign (#), which is not a comment at the beginning of a string literal. After the "#" sign and to the physical line are part of the comment, the Python interpreter ignores them.

#!/usr/bin/python# First Commentprint "Hello, python!"; # second Comment

This will produce the following results:

Hello, python!.

Comments may be expressed in a declaration or after the same line:

Name = "Madisetti" # this is again comment

You can use multiple lines of comments as follows:

# This was a comment.# this is a comment, too.# this is a comment, and too.# I said that already.

Use blank lines:

A row contains only spaces, may have comments, and if it is a blank line then Python completely ignores it.

In an interactive interpreter session, you must enter an empty physical line to terminate multiple lines of statements.
Waiting for the User:

The following line of the program shows the prompt, press ENTER to exit, waiting for the user to press ENTER:

#!/usr/bin/pythonraw_input ("\n\npress the Enter key to exit.")

Here, "\ n \ nthe" is used to create two line breaks before the actual row is displayed. The program ends as soon as the user presses the key. This is a good technique to keep a console window open until the user finishes running the application.
Multiple statements in a row:

a semicolon (;) allows multiple statements to be written in a single line, regardless of whether the statement starts a new block of code. The following is an example of using semicolons:

Import SYS; x = ' Foo '; Sys.stdout.write (x + ' \ n ')

Multiple statement groups as suites:

A separate set of statements, in Python, a single code block is called a sequence. Complex statements, such as if, while, Def, and class, require a header row and a suite.

The header line begins with the declaration (with the keyword), and terminates with a colon (:)), followed by one or more lines that make up the package. For example:

If expression:   suiteelif expression:   Suite else:   Suite

Command-line arguments:

We may have seen, for example, that many programs can run, and they provide some basic information about how to run them. You can do this in Python using-H:

$ python-husage:python [option] ... [-C cmd |-M mod | file |-] [Arg] ... Options and arguments (and corresponding environment variables):-C Cmd:program passed in As String (terminates option Li ST)   -D: Debug output from parser (also pythondebug=x)-e   : Ignore environment variables (such as PYTHONPATH)-H   : Print this help message and exit[etc.]

You can also set up your script, which should accept various options in this way. Command-line arguments are an advanced topic and will be learned later when you pass other Python concepts.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.