In the last blog post, we have set up the Python development environment on our Windows PC. In this blog post, I will start to learn the basic syntax of Python. Now the development environment of the practice is based on Windows. If some friends like Linux or MAC, I can provide corresponding blog introduction as long as there is demand. In fact, it is the same as before, and I will not bother to say if there is no demand.
Let me write v out front
Python if you don’t know it, it’s probably okay, but once you know it, you’ll love it, right
V Solemn promise
I admit that there are a lot of blog posts in the blogosphere right now, and in the last blog post there were some people who were skeptical about the Python series. I’m here to tell you that as long as the Python series doesn’t get lost in the blogosphere (at least, if Python does get lost in the blogosphere), I’ll just have to move on to other Python communities), and I’ll do my best to write about as much as I know. Don’t worry, all I can do is to update one or two articles a week. (After all, I can only make sure that I can publish one or two articles on weekends. I am afraid that I will have to work overtime on weekdays.
V Start of text: Basic Python syntax
1. Define variables:
Code body:
x=1
y=2
z=x+y
Copy the code
The way Python defines variables is very simple. This is the code above, and anyone with a bit of math will understand what this code means. In fact, now in many foreign universities are taking Python as the first language for computer language introduction, because Python language can be said to be human language, it is easy to get started, you can understand it at a glance (but most languages are like this, it is easy to get started but difficult to in-depth, to persevere).
Code explanation:
2. Judge statement:
Code body:
# coding=utf-8
score=90
if score>=90:
print("你真棒")
print("优秀")
elif score>=80:
print("良好")
elif score>=60:
print("及格")
Copy the code
The Python language is very easy to understand, even for judgment statements.
Code explanation:
It is worth mentioning that by default, there are Chinese characters in the code that need to be noticed, not only at runtime, but also after runtime, because the default output Chinese characters will be garbled. You can set it up here. File>>Settings>>Editor>>File Encodings>>Project Encodings change to UTF-8. Pycharm (PyCharm) Perfect solution to Python Chinese compilation errors
3. The cycle:
Code body:
For I in range(0,3): print(I) # print("Index"+ I) print("Index {0} {1}". Format (I,"cnblogs")) print("end")Copy the code
Python’s loop syntax may seem a little out of place to use, but it’s pretty easy to understand, with a few exceptions.
Code explanation:
4. Define function def:
Code body:
def HelloCNBlogs(): print("Hello cnblogs") def GetMax(x,y): if x>y: return x else: Return HelloCNBlogs y () print (GetMax (9, 3))Copy the code
Code explanation:
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()
Copy the code
Code explanation:
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();
Copy the code
Code explanation:
7. Introduce classes for other files:
Code body:
# S=FirstWork.SecondTest("CNBlogs"); # S.SayFirst() # S.SaySecond(); From FirstWork import SecondTest ST=SecondTest("CNBlogs"); ST.SayFirst() ST.SaySecond();Copy the code
In this code, I have introduced the SecondTest class from firstwork. py. There are two methods for introducing other classes. You may have noticed that using import in Python feels a lot like using a namespace in other languages when importing classes from other files. That’s right, import is importing a namespace
Code explanation:
VPycharm shortcuts
As you can see from the above demonstration, Python comments are used with # instead of //
Pycharm
- Ctrl+/ Comment (uncomment) the selected line
- Shift + Enter starts a new line
- Ctrl + Enter Intelligent line feed
- TAB Shift+TAB indent/unindent the selected row
- Ctrl + Alt + I automatically indents
- Ctrl + Y deletes the line where the current caret is
- Ctrl + D copies the current line or selected block
- Ctrl + Shift + J merges rows
- Ctrl + Shift + V paste from the nearest cache
- Ctrl + Delete deletes to the end of a character
- Ctrl + Backspace deletes to the beginning of a character
- Ctrl + NumPad+/- Expands or contracts the code block
- Ctrl + Shift + NumPad+ expand all code blocks
- Ctrl + Shift + NumPad- Shrink all code blocks
V Blog Summary
So much for the basic syntax of Python, feel free to comment if you have any questions or additions. I can’t say I’m updating this series once a day (after all, I’m a man and I have to code for my family), but as long as the Python series continues to grow in the community (at least in the community, and if Python does get lost in the blogosphere, I’ll have to move on to other Python communities), I’m going to stick to it (try to update at least 1 or 2 posts a week). Pycharm = pyCharm = PyCharm = PyCharm = PyCharm Pycharm can be used to encode multiple IDES, such as VS or Eclipse, making it easier for most IDE users of other languages. This compatibility is really good. If you really prefer visual Studio or want to use it, I’ll cover Visual Studio developing Python separately in a later blog post if you want to
About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \