Learning Python from Lao Qi, a free lab

Source: Internet
Author: User
Experiment is also required to learn Python, that is, to test the meaning of a command. In the chapter "integrated development environment (IDE)", we introduced the Python IDE and recommended the IDLE to you. enter the IDLE to see the symbol, you can enter a line of commands. In fact, in the age of students, we envy the lab, where teachers can drum up all kinds of interesting things. When I went to college, I finally had the opportunity to do a lot of experiments in the lab, because I was a physics department and met a very distinguished teacher, Mr. Gao, let me repair and assemble all the worn and damaged experimental instruments in his lab, and complete the lab sample as required. After some hard work, I realized that it is not only time-consuming but also expensive equipment costs to complete the experiment. Later, when I was working, I felt that the cost of the experiment equipment was high. Therefore, I had to be careful when doing the experiment.

Later, we finally found that computer was the best laboratory. The cost of doing experiments here is really low.

Let's go. Not far away. let's get it back now. Experiment is also required to learn Python, that is, to test the meaning of a command. Through experiments and research, we can use it in programming practice.

How to conduct a Python experiment?

Go to the Python lab

In the chapter "integrated development environment (IDE)", we introduced the Python IDE and recommended the IDLE to you. In this example, we can see the >>> symbol, you can enter a line of commands. In fact, this is a very good lab.

Another lab is the shell of the UNIX operating system (including various Linux and Mac OSx). after opening the shell, input python, as shown in:

If you use windows, you can also use cmd to obtain a similar interface. you can still enter python and then get the interface.

In any of the above environments, you can enter commands, press enter to run and output the results.

Here you can experiment as you like.

Experiment in interactive mode

In the previous sections, the interactive mode is used. Based on the principle of gradual progress and rising cycle, this article describes how to make an in-depth and systematic interaction model.

Directly display its content through variables

Starting from the example:

>>> a="http://qiwsir.github.io">>> a'http://qiwsir.github.io'>>> print ahttp://qiwsir.github.io

After a variable a is assigned to a string, input the variable name to print the string, which has the same effect as print. This is a feature of the interactive mode. if it is in the file mode, it cannot. the variable content can be printed only through print.

Indent

>>> if bool(a):...   print "I like python"... I like python

For the if statement, in the previous lecture "journey from the if statement", we have noticed that the execution statement under the if statement should be indented by four spaces. In some python textbooks, there is no need to indent in interactive mode. it may be for python 3 or other versions. I use python2.7 and do need to indent it. The above example shows that.

Check whether the officer needs to indent his machine to test it?

Error

I saw this sentence in an advertisement: the programmer's motto: "I don't want the best, I just want to report an error ". Reporting errors is not a bad thing for programming. How to handle the error?

Read the error message carefully.

In the above example, if I write:

>>> if bool(a):... print "I like python" File "
 
  ", line 2   print "I like python"       ^IndentationError: expected an indented block
 

We can see from the error message that the second line is wrong. Where is the error? Python is very user-friendly here, telling you where the error is:

IndentationError: expected an indented block

This means that an indent block is required. That is, I didn't indent the second line and needed to indent it.

In addition, we also need to remind you that >>> indicates that the command can be entered later, and... indicates that the current command has not ended. To end and run the command, you need to press the Enter key twice.

Exploration

If the viewer does not know a certain instruction, or wants to test whether an operation is feasible, he or she can explore it in the interactive mode. the cost of this exploration is very small, and at best it is an error. In addition, we can get more valuable content from the error message.

For example, in the dazzled operators, we mention Boolean operations. In fact, in variable types, apart from the integer and string types mentioned above, Boolean is also a type, what are the characteristics of Boolean variables? Next we will explore:

>>> A' http: // qiwsir. github. io '> bool (a) # Boolean type, represented by bool (), similar to int (), str (), is a built-in function: True >>> B = "" >>> bool (B) False >>> bool (4> 3) True >>> bool (4 <3) false >>> m = bool (B) >>> mFalse >>> type (m)
 
  
>>>
 

As shown in the preceding experiment, if the object is empty, False is returned. if not, True is returned. if the object is False, False is returned. The above exploration can also be extended to other situations. Can the viewer summarize the characteristics of bool () through exploration?

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.