Python basics and python Basics

Source: Internet
Author: User

Python basics and python Basics
Python features

  • 1. Easy to learn:Python has a relatively small number of keywords, and the structure is simple. It is easier to learn with a clearly defined syntax.

  • 2. Easy to read:Python code definition is clearer.

  • 3. Easy Maintenance:Python's success lies in that its source code is quite easy to maintain.

  • 4. A wide range of standard libraries:One of the biggest advantages of Python is its rich library, cross-platform, and compatible with UNIX, Windows, and Macintosh.

  • 5. Interactive Mode:With the support of interactive mode, you can enter the language for executing code and obtaining results from the terminal, interactive testing and debugging code snippets.

  • 6. Portability:Based on its open source code features, Python has been ported (that is, to make it work) to many platforms.

  • 7. Scalability:If you need a piece of key code that runs very quickly, or want to write some algorithms that are unwilling to be opened, you can use C or C ++ to complete that part of the program, and then call it from your Python program.

  • 8. database:Python provides interfaces for all major commercial databases.

  • 9. GUI programming:Python supports GUI creation and porting to many system calls.

  • 10. Embedding:You can embed Python into a C/C ++ program, so that users of your program can gain the "scripted" capability.

Python Development History

Python was designed by Guido van rosum at the National Institute of mathematics and computer science in the Netherlands at the end of 1980s and the beginning of 1990s.

Python itself has evolved from many other languages, including ABC, Modula-3, C, C ++, Algol-68, SmallTalk, Unix shell and other scripting languages.

Like Perl, Python source code also complies with the GPL (GNU General Public License) protocol.

Currently, Python is maintained by a core development team, and Guido van rosum still plays a crucial role in guiding its progress.

Basic Python syntax

1. The first python Program

Print ('Hello python ')

Program output result: hello python

2. Keep characters in python

The following list shows reserved words in Python. These reserved words cannot be used as constants, variables, or any other identifier names.

All Python keywords only contain lowercase letters.

And Exec Not
Assert Finally Or
Break For Pass
Class From Print
Continue Global Raise
Def If Return
Del Import Try
Elif In While
Else Is With
Except Lambda Yield

3. Lines and indentation

The biggest difference between learning Python and other languages is that the Python code block does not use braces {} to control classes, functions, and other logic judgments. Python uses indentation to write modules.

The number of white spaces for indentation is variable, but all code block statements must contain the same number of white spaces for indentation, which must be strictly executed. As follows:

4. python quotation marks

Python can use quotation marks ('), Double quotation marks ("), Three quotation marks ('''Or"""The start and end of the quotation marks must be of the same type.

The three quotation marks can be composed of multiple lines. They can be used to compile the quick syntax for multiple lines of text. common syntax document strings are used as comments in specific locations of files. As follows:

Print ('Hello python') can use single quotation marks or double quotation marks.

Print ("today is 'good 'day") the printed content contains single quotation marks, and double quotation marks are used outside.

Output result: today is 'good 'day

Print ('I like "you") contains double quotation marks.

Output result: I like you very much.

Print ("Today is 'Friday', tomorrow you don't need to" go to work ") the printed content contains single quotation marks and double quotation marks. In this case, you need to use three quotation marks.

Output: Is today 'Friday'? I don't need to "go to work" tomorrow.

5. python Variables

The value of the variable stored in the memory. This means that a space is opened in the memory when a variable is created.

For variable-based data types, the interpreter allocates the specified memory and determines what data can be stored in the memory.

Therefore, variables can specify different data types, which can store integers, decimals, or characters.

The type declaration is not required for variable assignment in Python, as follows:

A = 1 # assign an integer value

B = 'hello' # string

C = 10.99 # Floating Point

6. python conditional statements

A Python Condition Statement is a code block that is determined by the execution result (True or False) of one or more statements.

  The Python programming language specifies that any non-0 and non-null (null) values are true.

  In python programming, if statements are used to control program execution. The basic form is:

If condition:
Execute the statement .....
Else:
Execute the statement .....

 

7. python loop statement-while

Python provides a for loop and A while LOOP (there is no do... while loop in Python)

In Python programming, the while statement is used to execute a program cyclically. That is, under a certain condition, a program is executed cyclically to process the same task that needs to be processed repeatedly. The basic form is:

While judgment condition:

Execute the statement .....

The execution statement can be a single statement or statement block. The condition can be any expression, and any non-zero or non-null value is true. When the condition is false, the loop ends.

Program output result:

 

When the while statement is run, there are two other important commands, continue. break is used to skip the loop, and break is used to exit the loop. The usage is as follows:

Continue is used to skip this loop, and break is used to exit the loop. The usage is as follows:

  Endless loop:

If the condition determination statement is always true, the loop will be executed infinitely, as shown in the following example:

8. python loop statement-

A for loop can traverse projects in any sequence, such as a list or a string. The basic format is as follows:

For iterating_var in sequence:

Statements (s)

You can also run the "continue" and "break" command to skip the loop. The "break" command is used to exit the loop. The usage is as follows:

Continue is used to skip this loop. Its usage is as follows:

 

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.