I study in the school is C, C + +, C #, internship when the company is used in Java, and then self-study of Java. In the words of my leader, "C + + to Java a few days time is enough." Recently, in learning the depth learning algorithm and natural language processing, many algorithms are written in Python, and then decided to learn Python. Spent the afternoon on the basic learning, the following is a rookie tutorial links.
Python2:http://www.runoob.com/python/python-tutorial.html
Python3:http://www.runoob.com/python3/python3-tutorial.html
Why give two sets of tutorials, there is a saying, "Java only one, and Python language has two, 2 and 3." Python 3.0 is designed in a way that does not bring in too much baggage, and Python2 in some ways, and is explained in detail below.
With the C++/java of learning Python is very fast, I read the tutorial when a lot of things are skipped, because it and other languages are the same. To reduce the time to look at the redundant content, it is important to sort out the coding aspects of Python and c++/java, so that people who teach themselves to Python can learn faster.
At the end of the article there is python and its IDE resources OH. first, the basic grammar 1. Indent
The biggest difference between Python and C++/java is that you use indentation to represent code blocks instead of curly braces ({}). The boundaries of a module in Python are determined by how many spaces are in front of the line, and C and Java are braces {}. For example, the following procedures:
If True:
#可以在同一行中使用多条语句, semicolons are used between statements (;) split.
print ("true");p rint ("true")
else:
print ("False")
So if this line of code is longer, the line does not fit. We can use a backslash (\) to implement the
Total = Item_one + \
Item_two + \
item_three
Python's empty lines have no effect, plus and no error, with the right space to separate the module can make your code level clearer. 2, Notes
A single-line comment in Python begins with a #, and a multiline comment uses three single quotes ("') or three double quotes (" "). For example:
#!/usr/bin/python
#-*-coding:utf-8-*-
# filename: test.py
# first comment
print ("Hello, python!") # The second annotation
'
is a multiline comment, using single quotes.
This is a multiline comment, using single quotes. "" "
This is a multiline comment, using double quotes.
This is a multiline comment, using double quotes.
"""
ii. Types of data
1, the declaration of the variable
When you define a variable in Python, you do not need to declare its data type, but you must assign a value and assign it a corresponding data type when assigned. For example:
counter = 100 # integer variable
miles = 1000.0 # floating-point variable
name = ' ABCDEFG ' # string
print (counter)
print (miles)
Print (name)
In Python, a variable is a variable, it has no type, and what we call "type" is the type of the object in memory that the variable refers to, and the variable is simply a reference to it and can be deleted with Del counter.
Here, Python2 and Python3 print are quite different, and the print default output is newline.
Print counter #这是Python2
print (counter) #这是Python3
#如果要实现不换行:
print Counter, #Python2: Add a comma
at the end of a variable Print (counter,end= "") #Python3: Add end= "" At the end of the variable
2. Data type
There are six standard data types in Python3: Number (numeric) string (string) list (list) Tuple (tuple) set (set) Dictionary (dictionary).
There is no char in Python, and single characters are also treated as strings.
Python2 has no set (set). (1) number
int, float, bool, complex (plural).
In Python 3, there is only one integer type int, expressed as a long integer, without a long in python2.
There is no Boolean in Python2, which uses the number 0 to indicate False and 1 to True. To Python3, True and False are defined as keywords, but their values are 1 and 0, and they can be added to the number. (2) String:
Strings in Python are enclosed in single quotes (') or double quotes ("), and use backslashes (\) to escape special characters.
The syntax for intercepting a string is as follows: variable [header subscript, trailing subscript]
The index value starts with 0, and 1 is the starting position from the end.
A plus sign (+) indicates a connection string, and an asterisk (*) indicates that the current string is copied and the number that follows is the number of copies. Examples are as follows:
str = ' abcdef '
print (str) # output string
print (str[0:-1]) # Output The first to the penultimate of all characters
print (str[0]) # output string first character
print (Str[2:5]) # Output from the third start to the fifth character
print (str[2:]) # output from the third start of all characters
print (str * 2) # output string two times
print (str + "GHI") # connection string
Performing the above program will output the following results:
ABCdef
ABCDE
a
cde
cdef
abcdefabcdef
Abcdefghi
(3) List (listing)
Lists are the most frequently used data types in Python, and are more like the list in Java.
But the elements in the Python list can be of different types, such as numbers, and strings can even contain lists.
The list is a comma-separated list of elements written between brackets ([]).
As with strings, lists can also be indexed and intercepted
#!/usr/bin/python3
list = [' ABCD ', 123, 4.56, ' Efghij ', 78.9]
tinylist = [123, ' Efghij ']
print (list) # Output full list
print (list[0]) # output list first element
print (List[1:3]) # from the second start to the third element
print (list[2:]) # Output all elements starting with the third element
Print (Tinylist * 2) # output two times list
print (list + tinylist) # connection list
Output results:
[' ABCD ', 123, 4.56, ' Efghij ', 78.9]
ABCD
[123, 4.56]
[4.56, ' Efghij ', 78.9]
[123, ' Efghij ', 123, ' Efghij ']
[' ABCD ', 123, 4.56, ' Efghij ', 78.9, 123, ' Efghij ']
(4) META Group:
Tuples (tuple) are similar to lists, except that the elements of a tuple cannot be modified. The tuples are written in parentheses (()), and the elements are separated by commas.
#!/usr/bin/python3
tuple = (' ABCD ', 786, 2.23, ' Runoob ', 70.2)
tinytuple = (123, ' Runoob ')
print (tuple) # Output full tuple
print (tuple[0]) # The first element of the output tuple
print (Tuple[1:3]) # output from the second element to the third element,
print (tuple[2:]) # Output all element print (Tinytuple * 2) with the start of the third element
# output Two-tuple
print (tuple + tinytuple) # Join tuples
Output results:
(' ABCD ', 786, 2.23, ' Runoob ', 70.2)
ABCD
(786, 2.23)
(2.23, ' Runoob ', 70.2)
(123, ' Runoob ', 123, ' Runoob ')
(' ABCD ', 786, 2.23, ' Runoob ') , 70.2, 123, ' Runoob ')
(5) collection (set)
A collection (set) is a sequence of unordered and distinct elements, as set in Java.
The basic function is to conduct member relationship testing and delete duplicate elements.
You can use the curly braces {} or the set () function to create a collection, and note that creating an empty collection must be set () instead of {} because {} is used to create an empty dictionary.
#!/usr/bin/python3
student = {' Tom ', ' Jim ', ' Mary ', ' Tom ', ' Jack ', ' Rose '}
print (student) # Output collection, duplicate elements are automatically removed
# member Test
if (' Rose ' in Student '):
print (' Rose is in the collection ')
else:
print (' Rose is not in the collection ')
# Set can perform set operations
a = set (' Abracadabra ')
B = Set (' Alacazam ')
print (a) print (
a-b) # A and B of the set of difference
print (a | b) # A and B of the set
print (A & B) # A and B intersection
print (a ^ b) # A and B in the absence of elements
Output results:
{' Mary ', ' Jim ', ' Rose ', ' Jack ', ' Tom '}
Rose in the collection
{' B ', ' A ', ' C ', ' R ', ' d '}
{' B ', ' d ', ' r '}
{' L ', ' r ', ' A ', ' C ', ' Z ', ' m ', ' B ', ' d '}
{' A ', ' C ' }
{' L ', ' r ', ' Z ', ' m ', ' B ', ' d '}
Note: There is no set (6) Dictionary in Python2 (dictionary)
The dictionary in Python (dictionary) is the equivalent of a map in Java, created with "{}", and is a unordered key (key): A value pair collection.
Key must use an immutable type, and in the same dictionary, the key must be unique.
Dict = {}
dict[' one '] = ' 1-abcd '
dict[' one ' = ' 1-ABCD ' #相当于java中Map的put, you can add or modify dictionary elements by assigning
dict[2] = "2 -EFGH "
tinydict = {' name ': ' Tom ', ' Age ':"
print (dict[' one ')] # output key ' one ' value
print (dict[2)) # Output key value of 2
print (tinydict) # Output full dictionary print (
tinydict.keys ()) # output all key
print (Tinydict.values () ) # Output All values
Output results:
1-ABCD
2-efgh
{' name ': ' Tom ', ' Age ':}
Dict_keys ([' Name ', ' age '])
dict_values ([' Tom ', 18])
(7) data type conversion
Directly to the data type as a function name can be very convenient. Such as:
A= "12345"
b=int (a)
print (b)
(8) Summary
Data types are both digital and non-numeric.
The digital type includes integral type, long integer type, floating point type and plural type.
Non-numeric types include strings, lists, tuples, and dictionaries;
The list uses "[]" to identify an array similar to the C language;
Tuples are identified with "()". Internal elements are separated by commas. However, tuples cannot be assigned two times, which is equivalent to a read-only list;
The dictionary is identified with "{}". The dictionary consists of the index key and its corresponding value, equivalent to the map in Java.
The common denominator of non-digital: All can use the correlation operation such as slice, link (+), repetition (*), Value (a[));
Different points of the Non-numeric type: The list can be assigned directly, the tuple cannot be assigned, and the dictionary is assigned in the Dict[k]=v way. third, operator 1, arithmetic operator
Both Python and Java are:
+-*/(subtraction)% (modulo)
Python new addition:
* * (Power)//(divisible)
Note: In Python2, as in division (/) and java,c++, integer/integer = integer
Division (/) in Python3 always returns a floating-point number, to get the integer use (//) operator
Print (2/4) # Division, get a floating-point number (Python3)
0.5
print (2//4) # Division, get an integer
0
2. Comparison Operators
Both Python and Java are:
= = (equal to)!= (not equal to) > (greater than) < (less than) >= (greater than or equal) <= (less than or equal)
Note: <> (not equal) in Python2, no 3, assignment operator in Python3
Both Python and Java are:
= (Assignment) = = (addition Assignment)-= (subtraction Assignment) *= (multiplication Assignment)/= (Division Assignment)%= (modulo assignment)
Python new addition:
**= (Power Assignment)//= (assign value to divide)
Python removal:
+ + (self-increasing operator)-(self-subtraction operator) 4, bitwise operator
Both Python and Java are:
& (by bit and) | (bitwise OR) ^ (bitwise XOR) ~ (bitwise reverse) << (move left) >> (move right) 5, logical operator
Python self-contained:
and (Boolean and) or (Boolean or) not (Bulfei) 6, Member operators
Python self-contained:
In (returns False if the value found in the specified sequence returns True)
Not IN (returns False if no value is found in the specified sequence) 7, the identity operator
Python self-contained:
Is (to determine whether two identifiers are referenced from one object)
Is isn't (is to determine whether two identifiers are referenced from different objects)
Note: is used to determine whether two variable reference objects are the same, = = to determine whether the value of a reference variable is equal. 8. Priority Level
Basically the same as Java:
[Power]>[to take the reverse]>[*,/,%,//]>[+-]>[>>,<<]>[&]>[\^,|] >[<=,<,>,>=]>[<>,==,!=]>[assignment operator]>[identity operator]>[member operator]>[logical operator]
Not to be continued ...
Python website Download: https://www.python.org/
The compiler I use is Eclipse+pydev
Eclipse website Download: https://www.eclipse.org/downloads /
Eclipse+pydev Environment Setup Tutorial: http://jingyan.baidu.com/article/cd4c2979101f02756f6e6064.html