Python Learning Notes (1)--Basic knowledge __python

Source: Internet
Author: User

Python rookie one, this article is a basic primer, mainly notes, and add their own practical experience. See all the information that has been referenced. Thank you here for your generous sharing and detailed analysis of the great God, and I also ask you to enlighten me.

A brief introduction to the version and platform. Python version 2.7.11, integrated development environment Pycharm5.0.4, System WINDOWS8. Chinese and English output

#-*-coding:utf-8–*-
print "Hello python!";
Print "Hello, world";

English output is not difficult. However, it is particularly noted that the first line of Utf-8 is added for the Chinese output. ASCII characters are used in the python2.x version, which contains both uppercase and lowercase letters and punctuation marks, expressed in one byte. Chinese is used in two bytes. Unicode is used in the python3.x version and does not need to be added to the first line.

There are several ways to export Chinese in legend:

# CODING=GBK #
coding=gb2312 #
Coding=big5

It is said to support Chinese, but even if the platform compiled to pass, the output is garbled. The reason is temporarily unknown. Basic Grammar programming method

Python is divided into interactive programming and scripted programming. Interactive programming is similar to the MATLAB Command Line window programming way, entered a line of commands and press ENTER to output the results. Scripted programming is the traditional completion of the entire program compiled after writing. Identifier

All identifiers can include English, numbers, and underscores (_), but cannot begin with a number.
Identifiers are case-sensitive. The python reserved characters include:
and exec not
Assert finally or
Break for Pass
Class from print
Continue global raise
def if return
Del Import try
Elif in while
else is with
Except lambda yield line and indent

Python's logical structure is expressed in the same format, with the benefit of omitting the annoying braces {} in C and in various languages, but improving the programmer's requirements for writing specifications. Common errors that the compiler has for this point are: indentationerror:unexpected indent (indent not aligned), Indentationerror:unindent does not match any outer Indentation level (indentation is inconsistent, should be changed to consistent). A conditional structure is judged as follows.

U=0
if u>0:
    print "True"
else:
    print "False"
Multi-line statement

In a python statement, a new line is generally used as the Terminator of the statement. Use a slash (\) to split a line of statements into multiple lines.

U=1+\
    3+\
    4
print U

You do not need to use a multiline connector if the statement contains [], {}, or () parentheses.

days = [' Monday ', ' Tuesday ', ' Wednesday ',
        ' Thursday ', ' Friday ']
Comments

Python single-line comments start with ' # ', and multiline comments use three single quotes ("') or three double quotes (" ").

String

print ' Hello, world! '
Print "Hello, world!"

The above two lines of code use single and double quotes respectively, but the output is the same. So what's the difference between the two output modes? Look at this line.

print ' Let ' s go! '

The error is: Syntaxerror:invalid syntax. The reason is well understood. The compiler is compiled with a ' start string, ' The end of the string, read from left to right, only let is included in the string, the rest of the S go! ' is a statement without a statute. Modified as follows.

Print "Let ' s go!" #法一, using the double quote
print ' let\ ' go! ' #法二, using the escape character

So how to stitch strings. is the "addition" operation.

Print "Hello" + ", world!"

Say two commonly used functions, str and repr. Examples and output are as follows.

Print repr (' Hello, world! ')
Print str (' Hello, world! ')
Print repr (10000L)
print str (10000L)

' Hello, world! '
Hello, world!.
10000L
10000
The 10000L of the long integer is converted to 10000. The Netizen has various explanations, the expression all can not understand. It would be good to know the output result.

Again, two commonly used functions. Input and Raw_input. Input assumes that the user will enter a legitimate Python expression, while raw_input the user's input as raw data.

Name=input ("What ' s your name?")
Print "Hello," +name+ "!"

What ' s your name? ' Vicky '
Hello, vicky!.

Name=raw_input ("What ' s your name?")
Print "Hello," +name+ "!"

What ' s your name? Vicky
Hello, vicky!.
The difference is that the former needs to take into account the python's input and output rules (plus single quotes) for various variables (here is a string), and the latter does not. The latter is generally used. reference materials

Http://www.runoob.com/python/python-tutorial.html
Http://www.cnblogs.com/yuxc/archive/2011/04/27/2030702.html
Http://www.cnblogs.com/rollenholt/archive/2011/08/01/2123889.html
Http://www.cnblogs.com/way_testlife/archive/2011/03/29/1999283.html
The Python Basics tutorial, People's posts and telecommunications press

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.