Chapter One
The output of a string is simple:
print ' A '
Print 1+2
Anything else can be
If you want to print out a type of data you can use this:
Print Type (10)
Remember type must have braces on it
In Python, if you want to clean the screen then the statement you can use is this:
Under Windows
Import OS
Os.system ("CLS")
Under Linux
Import OS
Os.system ("clear")
In Pyton, a variable of type bool has two true and false
Connectors with and Or not
For Python to accept user data, this is generally the case:
A = input ()
Then the user enters, the specific data format is this:
1//Represents an int
1.1//represents float
"1231"//representation string
[1,23,2,3,23,2], [123,12,12,3, "Fdsaf"],[{1:1},{1:1}]//all represent the list; in fact, the array is generally said, in fact, the array elements in Python can be any type, will not be like the c+ + that also unifies the data type
{"A": [1231,12312,123]}//all represent dict; the logo is a dictionary
In fact, there is another way to enter in Python:
A = Raw_input ()
The deal with this thing is to handle everything you input as a string.
Enter the python editing interface in Windows just type in the command line: Python is OK, if you want to jump out of Python edit mode, then you can enter exit ().
A summary of the if,while in Python
[1]if A:
[2]if (a):
Both of these expressions are OK. In Python there is no {} to represent a block or something, but it is distinguished by a space or tab
All the above things should be written in the form of:
If a:
Print A;
Esle:
Print B;
The same pair is the same as the while.
While a:
Print a
The following things are more important than other languages.
From module name Import method name
Example:
From random import Randint
Remember that range (0,100) is an array from 0 to 100, but not 100. His type is List
Formatting of strings:
str = "123"
str1= "456"
STR + str1 string concatenation
The function that converts a number to a string is:
STR (16)//At the same time this str can convert any type of thing into a string type including List,dict,bool
You can format the output with the following expression in the function of print
print '%s '% ' FDA ' #这个就像c the same as printf in + +, but can be used if it is a parameter of multiple parameters
Print "%s%s"% ("FDAs", "FDSA") to complete
Type conversion:
int (x)
Float (x)
STR (x)
BOOL (x)
In Python, the following values are considered false and others are true.
The numbers for 0 include 0,0.0
Empty string: Includes ', ' '
Represents control of None
Empty collection (), [],{}
Everything else is considered true.
Definition of function:
def hell (a,b)//functions with two parameters defined
The operation of the list is the operation of the array
If you want to add an element to the list, you can use the method
a=[1,2,3,21,3,21,3]
A.append (10)
So now the value of a is: a=[1,2,3,21,3,21,3,10]
If you want to get the length of a, then you can use the method: Len (a)
If you want to delete one of these elements, the method you can use is: Del a[1]
Also, if you want to get an array of arrays in an array, you can identify:
a[:]//represents the entire array.
a[1:]//represents the array element from the first to the last
Many times we need to break the string down into an array or a list, so the method used in Python: a.split () This function returns a list. Where the default argument for split is a space, and you can also specify parameters for him
Of course, you can also combine arrays into strings when you decompose strings. The methods used are:
b = [1,12,3,21,3,12]
A = ', '
A.join (b)
So you can use it to connect to list B.
There is something very special in Python, but the string is something like list.
His operations are basically the same as the list.
A = "Fdfdsaffdsafdsafa";
“;'”. Join (a)
Output to be like this: f;d;f;d;s;a;f;f;d;s;a;f;d;s;a;f;a;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// Gorgeous dividing line
Here's how to learn file operations
[1] File read
I sent the Python file read and the C language file read almost
File = Open ("E:\\1.txt")/read files, error if file does not exist
File.read () indicates that the file is read
File.readline () represents a row of reads. He reads it like C. read one line this time
File.readlines () is read multiple rows, is read in the behavior unit, all get a behavior element of the list
The File.read () reads the entire file as a whole. So what gets is a string that represents the entire file
File.close ()///Remember read finish close file
[2] File write
File=open ("E:\\1.txt", "w");
This thing is the same as the C language, if the path does not have this file, then create a file on that path
File.write ("")/so written, but this time has not been formally written to the file, but also in the cache, you can use File.flush () directly to write files to the file
Remember File.close ()
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// Exception handling
Try
File = Open ("E:\\morefun_note.txt")
Except
print ' File not exist '
print "Done"
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// Dict Dictionary
The elements of the dictionary are very simple to add
a[' fd '] = "12312"
Deletion of dictionary elements:
Del a[' FD ']
It's just so simple.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// Module
Actually above from random import Randint
In fact, Random is a module, and Randint is a function inside
And there may be conflicts, you can also rename functions in a library, such as
From Math import pi as Math_pi
Then the MATH_PI in the future means pi.
If you need to see how many methods there are in math, you can use function dir to view
But if you use the functions in math, then you have to use
From the math import sqrt can be used, if you simply use import math seems to be problematic
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// Default parameters for functions
Def hell (a = ' FDA '):
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// Read from JSON
Import JSON
s = json.loads (strfile)//converts characters to Dict
str = json.dumps (s)//convert Dict to String
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// Object-oriented
Class MyClass:
Name = ""
def Hello (self, a = ' tan ') #这里注意一下他的函数必须带有一个参数, as in C + +, this argument self is a pointer to itself, although not in C + + code, but in the final generation of the resulting code. The same parameter also supports the default parameters
Print a
At the time of use:
A = MyClass ()
A.name
A.hello ("FDAFDSA")//although in the member function is defined as two parameters, but one is pointing to their own pointer, the system itself will be assigned value, we just consider the back of the argument on it
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////
The resolution of one of the above things:
Remember the above mentioned import math if you want to use one of the functions in math, you don't need the from math import sqrt every time, but that's true if you don't use the From math import sqrt, You can import math and use MATH.SQRT when you use it later.
OK, I learned to come here today.