Basic Python Syntax: getting started with python [2] and getting started with python

Source: Internet
Author: User

Basic Python Syntax: getting started with python [2] and getting started with python

In the previous blog, we set up a python development environment in Windows. Getting started with python, we have already set up a python development environment on our own windows computer, in this blog, I started to learn the basic syntax of Python. Now, the development environment for the exercises is based on windows. If a friend prefers linux or mac, I will give a blog post about it as long as I have a requirement, I don't have to bother talking about it if I don't need it.

V Preface

Python: You don't know it. It may be okay. Once you know it, you will fall in love with it.

V solemnly promises

I admit, there are a lot of blog posts in the garden, and in the previous blog, park friends have doubts about the Python series. Here I will tell you, as long as the blog posts in the python series do not sink in the Garden (at least some friends of the garden are concerned, if python does not open in the blog garden, I will have to switch to other python communities ), I will do my best to write to the largest area I know. Don't worry. What I can do is Update 1 ~ every week ~ Article 2 (after all, I can only make sure that one or two articles can be published on weekends. I am afraid that I will work overtime on weekdays. IT is also hard for me. You know)

V: Basic Python syntax

1. Define variables:

Code body:

x=1y=2z=x+y

The Python variable definition method is very simple. It is the code above. I believe that anyone who knows a little bit about mathematics can understand the meaning of this Code. In fact, many foreign universities use Python as the first language to get started with computer languages, because python can be said to be a human language and is easy to use, it can be understood at a Glance (but most languages are like this, so it is easy to get started and difficult to get started, so you must persevere .)

Code Description:

2. Judgment statement:

Code body:

# Coding = utf-8score = 90if score> = 90: print ("awesome") print ("excellent") elif score> = 80: print ("good ") elif score> = 60: print ("pass ")

The Python language is very easy for everyone to understand, even the judgment statement.

Code Description:

It is worth mentioning that by default, Chinese characters in the code should be noted, not only during running, but also after running, because the default output of Chinese characters will be garbled. You can set it here. File> Settings> Editor> File Encodings> Project Encodings changed to UTF-8 details see: perfect solution to python output Chinese garbled; solve pycharm Chinese garbled, solve pycharm Chinese compilation error; perfect solution to python Chinese compilation errors

3. Loop:

Code body:

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

Python's loop syntax may make everyone feel a little unaccustomed, but I believe it is easy to understand, just a little difference.

Code Description:

4. Define 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 Description:

5.00 object-oriented class:

Code body:

class FirstTest:    def __init__(self,name):        self._name=name    def SayFirst(self):        print("Hello {0}".format(self._name))F = FirstTest("CNBlogs")F.SayFirst()

Code Description:

6. Inheritance:

Code body:

class FirstTest:    def __init__(self,name):        self._name=name    def SayFirst(self):        print("Hello {0}".format(self._name))class SecondTest(FirstTest):    def __init__(self,name):        FirstTest.__init__(self,name)    def SaySecond(self):        print("Good {0}".format(self._name))S=SecondTest("CNBlogs");S.SayFirst()S.SaySecond();

Code Description:

7. Introduce other file classes:

Code body:

# Method 1 introduced # import FirstWork # S = FirstWork. secondTest ("CNBlogs"); # S. sayFirst () # S. saySecond (); # Method 2: from FirstWork import SecondTestST = SecondTest ("CNBlogs"); ST. sayFirst () ST. saySecond ();
In the above code, I introduced FirstWork in 6. the SecondTest class in The py file. We can see that there are two methods to introduce the classes of other files. As for their differences, we will not talk about them here, you can select one based on your actual needs. I believe you have also found that when you introduce classes in other files in Python, the use of import is quite similar to the use of namespaces in using in other languages. That's right. In fact, import is the introduction of namespaces.

Code Description:

VPycharm shortcut

In the above demonstration, we can see that the python annotation usage is # Instead //

Pycharm common shortcut keys:

  • Ctrl +/comment (uncomment) the row selected
  • Shift + Enter start new line
  • Ctrl + Enter smart line feed
  • TAB Shift + TAB indent/unindent selected row
  • Ctrl + Alt + I automatic contraction
  • Ctrl + Y Delete the row where the current Delimiter is located
  • Ctrl + D copy the current row or the selected block
  • Ctrl + Shift + J merge rows
  • Ctrl + Shift + V paste from the nearest cache area
  • Ctrl + Delete to the end of a character
  • Ctrl + Backspace Delete to start of character
  • Ctrl + NumPad +/-expand or contract the code block
  • Ctrl + Shift + NumPad + show all code blocks
  • Ctrl + Shift + NumPad-shrink all code blocks
V blog Summary

I have introduced so much about the basic syntax of python. If you have any questions or want to supplement it, I would like to make a speech. I can't say that this series is a new article in a day (after all, the same man needs to support his family), as long as the python series of blog posts do not sink in the Garden (at least some garden friends pay attention to it, if python cannot be opened in the blog Park, I have to switch to other python communities.) I will definitely finish it (try to ensure that the minimum update is 1 ~ a week ~ 2 ). In the previous blog, many park friends mentioned that they want to use visual studio 2013/visual studio 2015 python for demo. Here, we have a slight note. In fact, for "Learning python", we recommend that you use pycharm, pycharm can be configured with multiple IDE codes such as VS or eclipse, making it easier for most users who prefer to use IDE in other languages. This compatibility is indeed very good. If you are more familiar with or want to use visual studio, I will introduce visual studio development python in my blog later.

 


Author:Please call my first brother
Exit: http://www.cnblogs.com/toutou/
About the author: focused on Microsoft platform project development. If you have any questions or suggestions, please kindly advise me!
Copyright Disclaimer: The copyright of this article is shared by the author and the blog site. You are welcome to reprint this article. However, you must keep this statement without the author's consent and provide the original article link clearly on the article page.
We hereby declare that all comments and private messages will be replied immediately. You are also welcome to correct the mistakes and make progress together. Or directly send a private message to me.
Support blogger: If you think the article is helpful to you, click the bottom right corner of the article[Recommended]. Your encouragement is the greatest motivation for the author to stick to originality and continuous writing!

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.