A free lab with old Ziko python _python

Source: Internet
Author: User

In the student era, the envy of the laboratory, the teacher can tinker with all kinds of interesting things. When I was in college, finally have the opportunity to do a lot of experiments in the laboratory, because I was the Department of Physics, and met a very respectable teacher-miss Gao, let me in his laboratory, all the damaged experimental equipment to repair the assembly, and to do the test samples as required. After some toss, just understand, to do a good experiment, not only spend energy, but also expensive equipment costs. Later work, more feel the cost of experimental equipment is high, so do the experiment always be careful.

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

It's a long way off. Not far, now pull it back. Learning Python, you have to do experiments, which is to try to see what the meaning of a command is. Through the experiment, the research is clear, can be used in the programming practice.

How do you do python experiments?

Go into the Python lab

In the integrated development environment (IDE) chapter, when we introduce the Python IDE, we recommend idle, go into idle, see the >>> symbol, and then enter a line of instructions later. In fact, this is a very good laboratory.

Another lab is the shell of the UNIX operating system (which contains a variety of Linux and Mac OS X), and after you open the shell, enter Python, which appears as shown in the following figure:

If reader is using Windows, you can also use CMD to obtain a similar interface, still input python, then get the interface.

In either of these environments, you can enter instructions and hit the ENTER key to run and output the results.

You can experiment at your whim.

Experiment in interactive mode

In the preceding sections, the interactive mode is actually used. In accordance with the principle of gradual and circular rise, this talk should be a deep and systematic interactive mode.

Display its contents directly through a variable

Starting with the example:

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

When you assign a variable A to a string, the name of the variable can print out the string, and print a has the same effect. This is a feature of interactive mode, and if you are in file mode, you cannot print variable content only through print.

Indent

>>> if BOOL (a):
...   Print "I like Python"
... 
I like Python

For an if statement, in the previous journey from the IF Start statement, it has been noted that the execution statement below should indent four spaces. In some python textbooks, there is no need to indent in interactive mode, possibly for Python3 or other versions, I'm using python2.7 and really need to indent. The example above can be seen.

Reader to test on his own machine, is it necessary to indent?

Error

In an advertisement has seen such a word: The programmer's motto, "Do not ask for the best, only to complain." Error, not bad for programming. How to treat the error?

Be sure to read the error message that you are prompted to be careful.

Or the above example, if I write this:

>>> if BOOL (a):
... print ' I like Python '
 File ' <stdin> ', line 2
   print ' I like Python '
       ^
   indentationerror:expected an indented block

From the error message, we can see that the second line is wrong. Where is the mistake? Python is very humane right here, telling you where the error is:

Indentationerror:expected an indented block

It means that you need a indent block. That is, I don't indent the second line, I need to indent.

In addition, by the way also remind,>>> said that the following can be entered instructions, ... Indicates that the current instruction does not end. To finish and execute, you need to hit the ENTER key two times.

Explore

If reader is not familiar with an instruction, or if you want to try a certain operation is feasible, you can explore in the interactive mode, the cost of this exploration is very small, at best, the error. And from the error message, we can also get more valuable content.

For example, in the dazzling operator, the boolean operation is mentioned, in fact, in the type of variable, in addition to the aforementioned integer, String type, Boolean is also one, then Boolean variables have what characteristics? Here's how to explore:

>>> a
' Http://qiwsir.github.io '
>>> bool (a)   #布尔型, expressed in bool (), just like int (), str (), is a built-in function
true
>>> b= ""
>>> bool (b)
False
>>> bool (4>3)
true
>>> bool (4<3)
false
>>> M=bool (b)
>>> m
false
>>> Type (m)
<type ' bool ' >
>>> 

From the experiment above, you can see that if the object is empty, return false and, if not, return true, or False if the object is false. The above exploration can also be extended to other situations. Can reader, by exploring, sum up the characteristics of bool ()?

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.