Python basic syntax and number types

Note content: Python basic syntax and number types

Date of Note: 2017-10-19

Python basic syntax

coding

In python3, by default, the encoding of the source file is UTF-8, and all strings are Unicode strings. In python2, it is ASCII encoding. If you use python2, you need to set the encoding to UTF-8. This should be distinguished. Python3 is quite different from python2, and the two are not compatible.

The specific difference between the two can refer to the following articles:

http://www.runoob.com/python/python-2x-3x.html

Identifier

Like most other programming languages, python's identifier is also the following rules:

  1. Must start with a letter or underscore'_'.

  2. The other parts of the identifier consist of letters, numbers and underscores.

  3. Case sensitive

In python3, identifiers other than ASCII encoding can be used

Reserved word

Reserved words are keywords. Keywords cannot be used as identifiers. Use the keyword module to output all keywords of the current version. Code example:

Notes

In python, single-line comments use #, multi-line comments, use three single quotes ''' or three double quotes """ to enclose the comment Example:

# This is a single line comment
print("Hello World")'''
This is a multi-line comment, with three single quotes
This is a multi-line comment, with three single quotes
This is a multi-line comment, with three single quotes
'''
print("Hello, World!")"""
This is a multi-line comment, with three double quotes
This is a multi-line comment, with three double quotes
This is a multi-line comment, with three double quotes
"""
print("Hello, World!")--------------------------------------

Line and indentation

As we all know, the most distinctive aspect of python is to indicate code blocks by indentation, unlike most other programming languages that require braces. The number of spaces for indentation is not specified. You can define by yourself, but the number of spaces in the same code block must be the same.

Code example:

Examples of errors:

If it runs, the following error message will appear:

Multi-line statement

Python usually writes one line of code. If the code is very long, you can use backslash \ to implement multi-line statements. Example:

total = item_one + \

item_two + \

item_three

Multi-line statements in [], {}, or () do not need to use backslashes (), for example:

string=["this","is",

"test","233"]

Number type

There are four data types in Python: integer, long integer, floating point and complex number

Integer: That is, a positive integer or a negative integer.

Long integer: relatively large integers, billions of dollars, Python integers have no size limit, and integers in some languages have a size limit based on their storage length. In theory, you can store as much memory as you have.

Floating point: It is also a decimal number. The reason why it is called a floating point number is because when expressed in scientific notation, the position of the decimal point of a floating point number is variable. Python's floating point number does not have a size limit, but it is directly beyond a certain range. Expressed as inf (infinite)

Complex number: such as 1 + 2j, 1.1 + 2.2j

Blank line

In Python, the use of blank lines to separate functions or methods of classes indicates the beginning of a new code. The class and function entry are also separated by a blank line to highlight the beginning of the function entry.

Blank lines are different from code indentation, which is not part of Python syntax. No blank lines are inserted when writing, and the Python interpreter runs without error. But the function of the blank line is to separate two sections of code with different functions or meanings, so as to facilitate the maintenance or reconstruction of the code in the future.

Remember: blank lines are also part of the program code.

Input sentence

The input() function is used to receive user input, just like scanf in C language.

Code example, \n is a newline character:

input("\nPress the enter key to exit.")

operation result:

Display multiple statements on the same line

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

import sys; x = 'runoob'; sys.stdout.write(x + '\n')

Multiple statements form a code group

A group of statements with the same indentation constitutes a code block, which we call a code group.

For compound statements like if, while, def, and class, the first line starts with a keyword and ends with a colon (: ), and one or more lines of code after this line constitute a code group.

We call the first line and the following code group a clause.

Code example:

if expression :

suite

elif expression :

suite

else :

suite

Print statement

The Print() function is used to print information in the console. The default output of print is line break. If you want to achieve no line break, you need to add end="" at the end of the variable:

Code example:

operation result:

import and from...import

Use import or from...import to import the corresponding module in python.

Import the entire module (somemodule) in the format: import somemodule

Import a function from a module, the format is: from somemodule import somefunction

Import multiple functions from a module, the format is: from somemodule import firstfunc, secondfunc, thirdfunc

Import all functions in a module, the format is: from somemodule import *

Recommended Posts

Python basic syntax and number types
Python basic syntax (1)
Python3 basic syntax
Python basic syntax generator
Python basic data types
Basic syntax of Python
Python basic data types
Python basic syntax iteration
Python basic syntax list production
Python learning-variable types
02. Python data types
Python and Go
Python basic summary
The difference between the syntax of java and python
Python introspection and reflection
Python basic drawing tutorial (1)
[python] python2 and python3 under ubuntu
Python function basic learning
Basics of Python syntax
python_ crawler basic learning
Basic knowledge of Python (1)
Python3 configuration and entry.md
Python automated operation and maintenance 2
Python introduction and environment installation
Python know crawler and anti crawler
centos7 install python3 and ipython
Python basic knowledge question bank
ubuntu18.04 compile and install python3.8
Centos 6.10 reinstall python and yum
Python open read and write
GitLab installation and basic use
CentOS7 install python3 and pip3
Python automated operation and maintenance 1
Python data structure and algorithm
Python multi-process and multi-thread basics
CentOS 6.9 compile and install python
CentOS 6 compile and install python 3
Generators and iterators in Python