目录
The hash sign (#) is a bit special in Python. When you put it in your code, everything to the right of it is ignored。
# Print the circumference of the circle:
print(2 * pi * radius)
The first line here is called a comment, which can be useful in making programs easier to understand—
both for other people and for yourself when you come back to old code.
If it was hard to write, it should be hard to read
Make sure your comments say significant things and don’t simply restate what is already obvious from the code. Useless, redundant comments may be worse than none. For example, in the following, a comment isn’t really called for:
# Get the user's name:
user_name = input("What is your name?")
Concatenating Strings
>>> "Let's say " '"Hello, world!"'
'Let\'s say "Hello, world!"'
I’ve simply written two strings, one after the other, and Python automatically concatenates them (makes
them into one string). This mechanism isn’t used very often, but it can be useful at times. However, it works
only when you actually write both strings at the same time, directly following one another.
>>> x = "Hello, "
>>> y = "world!"
>>> x y
SyntaxError: invalid syntax
In other words, this is just a special way of writing strings, not a general method of concatenating them.
Long Strings
If you want to write a really long string, one that spans several lines, you can use triple quotes instead of
ordinary quotes
print('''This is a very long string. It continues here.
And it's not over yet. "Hello, world!"
Still here.''')
You can also use triple double quotes, """like this""".
>>> 1 + 2 + \
4 + 5
12
Raw Strings
>>> print(r'C:\nowhere')
C:\nowhere
>>> print(r'C:\Program Files\fnord\foo\bar\baz\frozz\bozz')
C:\Program Files\fnord\foo\bar\baz\frozz\bozz
As you can see, raw strings are prefixed with an r.
A Quick Summary
Algorithms:
An algorithm is a recipe telling you exactly how to perform a task.When you program a computer, you are essentially describing an algorithm in a language the computer can understand, such as Python. Such a machinefriendly description is called a program, and it mainly consists of expressions and statements.
Algorithm is just a fancy word for a procedure or recipe—a detailed description of how to do something.
Variables:
A variable is a name that represents a value. New values may be assigned to variables through assignments such as x = 2. An assignment is a kind of statement.
Statements:
A statement is an instruction that tells the computer to do something. That may involve changing variables (through assignments), printing things to the screen (such as print("Hello, world!")), importing modules, or doing a host of other stuff.
Functions:
Functions in Python work just like functions in mathematics: they may take some arguments, and they return a result.
Modules:
Modules are extensions that can be imported into Python to extend its capabilities. For example, several useful mathematical functions are available in the math module.
Strings:
Strings are really simple—they are just pieces of text, with characters represented as Unicode code points. And yet there is a lot to know about them.
New Functions in This Chapter
What Now?
Now that you know the basics of expressions, let’s move on to something a bit more advanced: data
structures. Instead of dealing with simple values (such as numbers), you’ll see how to bunch them together
in more complex structures, such as lists and dictionaries.
来源:oschina
链接:https://my.oschina.net/u/4342388/blog/4319063