Getting started with Python: basic syntax and basic python syntax

Source: Internet
Author: User

Getting started with Python: basic syntax and basic python syntax

Most of those transferred from other programming languages to Python are more or less unfamiliar. If you use the syntax of other programming languages to write Python code, it is inevitable.

This article is based on my two-hour official documentation (Python 2.7:Https://docs.python.org/2/tutorial/index.html,As for the reason why I learned Python2.x: work needs ),

Extract important details to avoid further pitfalls.

 

First, run the following Python file (Foo. py ):

#! Python #-*-coding: UTF-8-*-''' python comment: multi-line comment: composed of three pair single quotes or three double quotes (such as the current position) single line comment: Use # ''' # To define a foo function, which is used to print the specified msg count times "" params: count: int, the number of times it is printed msg: string, message content useFor: whether to use for loop for multiple printing times. Raise: ValueError: When count <0, "" def foo (count = 1, msg = 'hello' + ''+" Foo ", useFor = 1) is thrown ): if count <0: raise ValueError ('count <0') if useFor: # can be used as a bool decision print ("print use for, count =" + bytes (count) + ":") for I in range (count): # range (N), a list from 0 to N is generated, exactly the same as the index print (msg + ''+ bytes (I) else: print (" print use while, count = "+ bytes (count) + ":") I = 0 while I <count: print (msg + "" + bytes (I) I + = 1 # I ++ is not supported in python, if _ name _ = '_ main _': print ('main in Foo. py ') foo (2, useFor = 1) foo (useFor = 0, count = 3); foo (-1)

 

  • File Header
    • Specify the file Interpreter
    • Specify file encoding
  • Note
  • Code block, indent
  • Basic Data Type
    • String
    • Number
    • Boolean
  • Control statement
    • If, elif, else
    • Loop: for, while
  • Program entry: main Function

 

1. File Header

As a scripting language, it is very common to specify some execution environments in the file header. It is common in shell.

The Python script has the same content:

1.1 specify Interpreter

Script Language. Common configurations:

#! /Bin/sh shell script #! /Usr/bin/perl script #! /Usr/bin/python script #! /Usr/bin/python3 python3 script #! The/usr/bin/python2 python2 script is not clear about the full path name of the script interpreter, or the installation path of the development environment and running environment is different. To ensure compatibility, you can also write :#! /Usr/bin/env python3
In this way, the absolute path of the script interpreter is automatically searched during running.
1.2 specify the file encoding

The default encoding of The py file is ASCII. Many of the above scripts do not support ASCII, so you need to specify the file encoding. Format:

#-*- coding: encoding -*-

 

2. Notes

Python Annotations:

Multi-line comment: a pair of three single quotes or three double quotes (for example, the current position). Single line comment: Use #

3. code block and indentation

Code block: From the Foo. py file above, it is easy to see that the code block starts with a colon. Whether it is function definition, if, else, or loop (for, while)

All use: as the beginning of the code block. It does not use {} to include code blocks.

Indent: In a Python program, the code block is not included by {}, and the code block is maintained by indentation. Do not write a tab. Use four spaces for indentation.

 

4. Basic data type 4.1 String4.2 Number4.3 Boolean

Similar to JavaScript, Python's boolean is also changeable.

The following situations are false:
1. None
2. False
3. Any type of numbers 0, including 0, 0.0, 0L, 0j
4. Empty sequence or mapping objects
5. for user-defined objects, if the class defines the special _ nonzero _ () or _ len _ () methods and returns False or 0

For the last rule, there are several points to note:
1. If the class does not define any of the two methods, the True value of this type of object is always True during testing.
2. If the Classes define both _ nonzero _ () and _ len _ (), only the return values of _ nonzero _ () are referenced.

 

The following situations are false: 1. none2.False3. any type of number 0, including 0, 0.0, 0L, 0j4. empty sequence or mapping objects 5. for objects of the User-Defined type, if the class defines a special _ nonzero _ () or _ len _ () method and returns False or 0
The last rule must be described as follows: 1. if the class does not define either of these two methods, the true value of this type of object is always True2. if the class also defines _ nonzero __() and _ len _ (). Only the return values of _ nonzero _ () are referenced.

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.