In the Python Basics series, prepare to list some of the basics I learned about Python, including:
Basic syntax control statement built-in data structure module and function string file processing object-oriented exception handling before writing machine learning algorithm like to use the Matlab language, contact Python, feel python a lot of places or more convenient than Matlab, each have their own advantages, in the company or make With Python more, here's a list of some of the basics of Python.
I. Coding rules
1. Naming rules
Variable names are usually composed of letters and underscores, package names, and module names are usually made up of lowercase letters.
The first letter of the class name is in uppercase, the other letters are lowercase, the object name is lowercase, the private variable of the class, and the private method is prefixed with two underscores.
Class Person: #定义一个Person类
__name = "#定义一个私有变量
__age = ' #定义一个私有变量
def __init__ (self, Name, age):
self . __name = name
Self.__age = Age
def getName (self): return
self.__name
def getage (self):
return Self.__age
Function names are usually in lowercase letters.
2, code indentation and colon
In Python, code indents and colons are all syntax, with the {} syntax in java,c/c++ and the indentation and colon used in Python.
As in Java, the following judgments are implemented:
if (x < ten)
{
y = x + 1;
} else{
y = x * 2;
}
The writing in Python is:
If x < #冒号
y = x + indent
else:
y = y * 2
3, the introduction of the module
A module is a collection of classes or functions. Like the import in Java, there are two ways to import in Python:
Import ... from ... import ... If you want to use the Rangrange function in the random module. There are two ways to do this:
From ...
Import random
num = random.randrange (1,9) #使用randrange函数
Print num
From ... import ...
From random import randrange
num = Randrange (1,9) #使用randrange函数
Print num
Note: The difference between these two ways, from ... The whole module is imported, and import ... import ... Only partial content is imported, and references to imported objects are created and can be used directly. In the import ... To create an alias for a module in the mode, use as, as
Import random as rd# create alias for random rd
num = rd.randrange (1,9) #使用randrange函数
Print num
4, Notes
The annotation in Python uses the #, #号后面为注释的内容, as in the example above.
There are a few other special notes to note here.
Chinese Note: #coding: UTF-8 cross platform annotation: #. /usr/bin/python
5, Statement separation
You don't have to use it in Python, but you use it when you write multiple statements on a single line;
Such as
x = 1; y = 2; Print x + y
6, variables and constants
A variable name consists of a letter, a number, or an underscore, where the first character must be a letter or an underscore.
Note the definition and use of global variables:
# define global variables at the beginning of the file
= _a global variable, underlined to differentiate
_b = 2
def add ():
global _a
_a = 3 return
_a + _b
To use the Global keyword.
There are no keywords in python that directly define constants.
7. Data type
The main data types are: numbers, strings, Ganso, lists, and dictionaries.
Number Direct definition
i = 1
View the address of I:
i = 1
print ID (i) #查看i的地址
To view the type of a variable
i = 1
j = 1.
Print type (i)
print type (j)
<type ' int ' >
<type ' float ' >
8, operator
| Arithmetic operators |
An arithmetic expression |
Description |
| + |
X + y |
Addition |
| - |
X-y |
Subtraction |
| * |
X * y |
Multiplication |
| / |
x/y |
Division |
| % |
X% y |
Model finding |
| ** |
X * * y |
exponentiation |
Note: 1/2 to do floating-point operations, you need to import the Division module
From __future__ Import Division
Relational operators
| Relational operators |
Relationship expressions |
Description |
| < |
X < y |
Less than |
| > |
X > Y |
Greater than |
| <= |
X <= y |
Less than or equal to |
| >= |
X >= y |
Greater than or equal to |
| == |
x = = y |
Equals |
| != or <> |
x!= y or X<>y |
Not equal to |
logical operators
| logical operators |
Logical expression |
| and |
X and Y |
| Or |
X or Y |
| Not |
Not X |