Python Learning notes (i)

Source: Internet
Author: User

I. Definition and configuration

Python is an easy-to-learn, easy-to-use language. Because his code is clear and easy to understand, it can help students to enjoy the fun of programming without being entangled in the code complex syntax, and it is an object-oriented language, and its extensibility and portability are very good. You can pick the code from one module, or you can read the property from another module. Many "seniors" have developed a rich and colorful expansion library, and no matter what application you want to develop, you may find similar code modules or even "Plug and play". Generic software written in Python on any platform can be modified to run on other platforms with little or no modification.


Visit the official website of python: http://python.org, download and install, then open IDLE, where you can write code, and also equipped with a Python Shell to show the results of the code running.

Python shells can be interacted with. For example, all the input after your program is running is inside the Python Shell.


Ii. Basic Concepts

Look at a piece of code first

myString = "Sin_geek" myNum1 = 24mynum2 = 0x92mynum3 = 1234567890987654321Lprint mynum1print mynum2print mynum3print Mystri Ng
Very concise, the whole code is to assign a value to the variable, and then print the OUTPUT variable ~ ~ (er, compared to the C language, do not need to include the header file, do not need to write the main function, it is important not to write variable type, do not write a semicolon!) The output is also concise! ), where the assignment is convenient, whether it is an integer, a string, or another type of value, it can be assigned directly to the variable.

If the value is 8, 16, or long, simply mark it in front or behind, and see the code above.

Long integers in Python are different from long integers in C or other compiled languages, and do not require a range of 32-bit or 64-bit. The long length of Python is related to the size of the (virtual) memory your machine supports. In other words, as long as the content is sufficient, it can represent a very large number of values.

Then as a high-level language there must be several "default" data types, as follows

MyList = [1, 2, "python", 1.2]mytuple = (1, 2, "Python", 1.2) mydict = {' name ': ' Sin_geek ', ' height ': ' + ' ' age ': 23}print my Listprint Mytupleprint mydict
If you look at English, you will know that the corresponding list (arrays in other languages), tuples and dictionaries.

Then there is the loop syntax divided into while and for

Counter = 0while Counter < 5:    Print counter    counter + = 1

Items = [1, 2, 3, 4, 5]for item in items:    print Item,

As you can see from the preceding paragraphs, Python does not have curly braces, but it requires that you use 4 empty glyd to represent each level of indentation (but you can customize the number of spaces in the actual writing, but to meet the equal number of spaces per level of indentation). As the indentation depth increases, the level of the code block is deepened, and the code block without indentation is the highest level. This indentation is strictly controlled, and the interpreter determines which part of the current code is in terms of indentation, which means that the indentation replaces the curly brace.

The next step is to describe how the function is written, first, before the function name, def indicates that it is followed by a function, and then a colon at the end of the parenthesis, and the indentation part is the function content.

def printmyname (name): Print Nameprint MyName ("Sin_geek")
Five more operations.

u = 1v = 24print "u+v=", U+vprint "u-v=", U-vprint "u*v=", U*vprint "u/v=", U/vprint "u%v=", U%v
Run it can be found that the division of the resulting integer, if you want to get a decimal, you need to introduce an external module to implement this function, as follows

From __future__ import Divisionprint 24/7
Note that the underscore on both sides of the future is two ~ ~

Then there are strings, single quotes, double quotes, and three quotes, which are used in different cases, in general, single and double quotes, but it is best to use double quotes to indicate that there are single quotes in a string, and of course you can use the \ escape character to indicate a single quotation mark in a string. And the three quotes are used when the string is too long for the branch to write.

Not to be continued ~




Python Learning notes (i)

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.