"Python" Java Programmer learning Python (iii)-Basic primer

Source: Internet
Author: User

A twinkle twinkle sparkling, all over the sky are small stars, hanging in the sky to put light, like many small eyes. Don't ask me why I like this song, I will not tell you because someone used the harmonica to listen to me.

I. Python learning documents and materials

Generally, the document is always the most authoritative, most comprehensive, but very few people can seriously the document or help manual to look at it, after all, too boring, just like the Java API documentation, the correct usage of course is to use it as a reference book, think of what to go to check, of course, it is best to see it again, It is also convenient to have an impression of finding something in this mind.

The most authoritative of course is the official document, address in: https://docs.python.org/3/. Recommended to read English, but if you really want to see the Chinese is also a place: http://python.usyiyi.cn/translate/python_352/index.html. If you want to read, then recommend a book I think is more suitable: "Python programming quick to get Started", this book is really good.

Of course, according to the Order of my blog reading is also possible, this is my learning process.

Ii. basic knowledge of Python

Although learning things should be a step-by-step, but a lot of knowledge to the deep will be related to each other, so first from the appearance of a simple introduction of all the knowledge points, and then in-depth, this article is just a generalization.

2.1 Global functions (built-in functions)

I like to call it a global function, because it can be called anywhere, and from this point of view, Python and JavaScript are somewhat similar and have some global functions. Python also has many direct uses, such as writing print (1) directly to print a 1 in the console.

2.2 Basic data types

Python is not like Java is a strongly typed language to determine the variable type when used, this is still similar to JS, Python data types are divided into, integer, floating point, String, Boolean, object.

2.2.1 Integral type

Python's integral type has the same operations as Java.

1232,0,-10,0x22

Like the above are integers, hexadecimal also starts with 0x.

2.2.2 Floating point type

The floating-point type is similar to the JS language.

3.17,-12.9,2.3e10,6.2e-8

Floating-point types are also supported by scientific notation, with decimals.

2.2.3 String

Strings are similar to JS and can be caused by single or double quotes or three quotation marks (three single quotes or three multiple quotes). Of course, strings are inherently non-variable groups of characters.

' Hello ','yiwangzhibujian '

Look at personal habits, single and double quotes can be used.

2.2.4 Boolean type

Python's Boolean notation is special, true and false, the first letter is capitalized, which is very strange, and then the Boolean and or non-operator is, and, or, not

2.2.5 None Value

This is a value that does not represent any value

2.3 Variables

You do not need to define in advance, nor do you need to specify a type. It's so convenient. Of course, variable names are also required:

    • Can only be a word
    • can only contain alphanumeric and underscore
    • Cannot start with a number
Name='yiwangzhibujian'print('hello', name) ======== console output ========Hello Yiwangzhibujian

No definition is more cool than JS.

2.3 Classes and objects

Python is also a class and object, familiar with Java know that most of the time to learn Java is to learn how to use the class.

2.3.1 List

Lists are the list in Java, but the representations are a bit different, with a series of elements drawn in brackets:

[1,2,3,4,5]['yiwangzhibujian','laizhezhikezhui ' , 35]

The basic methods are similar to Java, which will be explained in detail later.

2.3.2 Tuple

Immutable list, using a pair of parentheses:

('hello','yiwangzhibujian')

The action is the same as list, except that it cannot be modified.

2.3.3 Set

A non-repeatable collection and a set of Java properties. In addition to using construction methods, you can use curly braces to represent:

{'hello','yiwangzhibujian'}

There is also an immutable set called Frozenset, which, like the relationship between list and tuple, is no longer described separately.

2.3.4 Dict

A dictionary table is a map in Java, a collection of key-value pairs that behave like JSON and are all key-value pairs enclosed in curly braces:

{'name':'yiwangzhibujian','num':  '3','sex':'no' }

The use method is still similar to map, followed by detailed introduction.

2.3.5 Other objects

such as IO, time and other objects will be in the follow-up study, but also in their own reference to learn the basic usage of the document.

2.4 Control Flow

When it comes to controlling the flow of Python, it is important to understand that the python's scope is not caused by two curly braces, but by using a uniform indentation representation, where the colon is to be indented, although it does not fit, but the habit is good. For example:

if True:     Print ('true') Else :     Print ('false')

Basic usage like Java, note the format.

2.4.1 Branch

Branching is the most common flow of control, and a Boolean expression is used to determine the direction of the program.

2.4.2 Cycle

Familiar with while and for, examples of continue and break,while and for are also supported

2.5 Functions or methods

Using Def to define, as JS uses function to define, the basic example is as follows:

def my_add (x, y    ): return X+y

This is a very important point of knowledge and will be explained later.

2.6 Scopes

The scope is simply divided into global scope and local scope, and basic usage rules are the same as Java.

2.7 Exception Handling

Python also has exception handling, which is implemented using try and exception. A simple example is given below, and if the exception is not caught, the exception is thrown and the program stops running:

Print (1/0)
Print (1/1)======= console output =======Traceback (most recent call last) :"D:\workspace\ eclipse_neon\python-hello\src\test.py" in <module> 1/0ZeroDivisionError : Division by Zero

If you are capturing:

Try :     Print (1/0) except zerodivisionerror:     Print (' divisor cannot be 0') Print (1/1)========= console output ========= divisor cannot be 01.0

Using the same method as Java, the capture is caught, and should not be captured is thrown.

2.8 Comments and indents

All that begin with # are treated as comments, and multiline annotations can be implemented using three quotation marks:

# This is a comment """ This is a multiline comment, which is a multiline comment. """

Write a program with more comments, or a period of time will forget to write this is what to do with.

2.9 Modules

In order to facilitate the organization of different functions of the code, are used to organize the module, the reference place is also used import to introduce, for example:

Import sys

Briefly introduced, the basic knowledge is only to understand the general situation of the language, will be described in detail later.

"Python" Java Programmer learning Python (iii)-Basic primer

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.