Beginning Python chapter 1 Instant Hacking The Basics 2: Comments Strings

目录

Concatenating Strings

Long Strings

Raw Strings

A Quick Summary

Algorithms:

Variables:

Statements:

Functions:

Modules:

Strings:

New Functions in This Chapter

What Now?


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.
 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值