Python basic syntax, Python primer to Mastery [II]

Source: Internet
Author: User

In the previous blog Windows Build Python development environment, Python primer to master [a] we have built a python development environment on our own Windows computers, this blog I began to learn the basic syntax of Python. Now the practice of the development environment is based on windows, if a friend like Linux or Mac, as long as there is a need, I follow can give the corresponding blog post, in fact, there is no need I would not bother to say.

vWrite in front

Python you don't know it, it's probably nothing, and once you get to know it, you'll love it.

vSolemn commitment

I admit that there are now a series of blog posts in the garden, in the last blog there are also friends of the Python series in question, here I tell you, as long as Python this series of blog posts in the garden is not heavy (at least the interest of the park friends, if Python really not work in the blog park, I had to go to the other Python community, and I would definitely try my best to write the biggest area I know. You do not have to worry, I can do is to update one or two a week (after all, I can only guarantee that the weekend to one or two, working days afraid of overtime, the same as bitter force it, you know)

vBody start: Python basic syntax

1. Define variables:

Code Body:

X=1y=2z=x+y

Python defines the way the variable is very simple, is the above code, I believe that as long as a little bit of math can understand the meaning of this code. In fact, many universities in foreign countries are the first language of the introduction of Python as a computer language, because the Python language can be said to be human language, it is easy to use, at a glance can understand (but most of the language is so, the entry is easy to go into difficult, to persevere. )

Code Explanation:

2. Judgment statement:

Code Body:

#Coding=utf-8Score=90ifScore>=90:    Print("you're so good .")    Print("Excellent")elifScore>=80:    Print("Good")elifScore>=60:    Print("Pass")

The Python language is very easy to understand, even for judgment statements.

Code Explanation:

It is worth mentioning that, by default, Chinese in the code need to be noted, not only at run time, after the run is also required to set, because the default output Chinese will be garbled. You can set it here. File>>settings>>editor>>file encodings>>project encodings to UTF-8 details see: The perfect solution python output Chinese garbled Solve Pycharm Chinese garbled, solve pycharm Chinese compiler error; Perfect for Python Chinese compilation Errors

3. Cycle:

Code Body:

 for  in range (0,3):    print(i)    #  print ("Index" +i)    Print ("Index {0} {1}". Format (i,"cnblogs"))  Print("end")

Python's looping syntax may make you feel a little bit unaccustomed, but it's easy to understand that there's just a little bit of access.

Code Explanation:

4. Define the function def:

Code Body:

def hellocnblogs ():     Print ("Hello cnblogs") def Getmax (x, y    ): if x>y:        return  x    Else:          return  yhellocnblogs ()print(Getmax (9,3))

Code Explanation:

5.00 Object-Oriented class:

Code Body:

class firsttest:     def __init__ (self,name):        self._name=name    def  Sayfirst (self):          Print("Hello {0}"= firsttest ("cnblogs " ) F.sayfirst ()

Code Explanation:

6. Inheritance:

Code Body:

classfirsttest:def __init__(self,name): Self._name=namedefSayfirst (self):Print("Hello {0}". Format (self._name))classsecondtest (firsttest):def __init__(self,name): Firsttest.__init__(Self,name)defSaysecond (self):Print("Good {0}". Format (self._name)) S=secondtest ("Cnblogs"); S.sayfirst () S.saysecond ();

Code Explanation:

7. Classes that introduce additional files:

Code Body:

 #   The first introduced method    import firstwork  ##   S=firstwork.secondtest ("Cnblogs");  #   S.sayfirst ()  #   S.saysecond ();  #   from  Firstwork import   secondtestst  = Secondtest (  " ); St. Sayfirst () ST. Saysecond ();  
In the above code I introduced 6 inside the firstwork.py file of the Secondtest this class, here we can see, the introduction of other files of the class there are two ways, as for their differences are also very obvious, here is not much to say, we can choose according to the actual needs. I believe you have also discovered that when you introduce classes from other files in Python, the import uses a sense of using a namespace with a bit of the other language. Yes, import is actually introducing namespaces

Code Explanation:

vPycharm shortcut keys

As you can see in the demo above, the use of Python annotations is no longer a//

Pycharm Common shortcut keys:

    • ctrl+/Comments (uncomment) the selected line
    • Shift + ENTER to start a new line
    • Ctrl + Enter Smart line break
    • TAB shift+tab indent/un-indent selected rows
    • Ctrl + Alt + I auto indent
    • Ctrl + y Deletes the row where the current caret is located
    • Ctrl + D Copies the current line, or blocks selected
    • Ctrl + Shift + j Merge rows
    • Ctrl + Shift + v paste from the nearest buffer
    • Ctrl + DELETE Delete to end of character
    • Ctrl + BACKSPACE Delete to start of character
    • Ctrl + numpad+/-Expand or shrink blocks of code
    • Ctrl + Shift + numpad+ expands all blocks of code
    • Ctrl + Shift + numpad-shrinks all blocks of code
vBlog Summary

Python basic syntax, Python primer to Mastery [II]

Related Article

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.