by Bruce Eckel, ANSI C++ Comitee member
Python的创始人为吉多·范罗苏姆(Guido van Rossum)。1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承。之所以选中Python作为程序的名字,是因为他是BBC电视剧——蒙提·派森的飞行马戏团(Monty Python's Flying Circus)的爱好者。
Python的设计哲学是“优雅”,“明确”,“简单”。Python开发者的哲学是“用一种方法,最好是只有一种方法来做一件事”。
——出自Wikipedia的Python
print("Hello, world!")
import antigravity
import this
Python can tell you what type a value has.
>>> means what you input in the IPython console. The first column below is the line number.
>>> type(4)
int
>>> type('Hello, World!')
str
>>> type(3.2)
float
>>> type('17')
str
>>> type('3.2')
str
>>> print('happy')
happy
If we want to print the single quotation marks?
>>> print("'happy'")
'happy'
If we want to print the double quotation marks?
>>> print('\"happy\"')
"happy"
If we want to print the slash?
>>> print('\\happy\\')
\happy\
If we want to print the double slashes?
>>> print(r'\\happy\\')
\\happy\\
Line break
>>> print('I\'m learning\nPython.')
I'm learning
Python.
Tab
>>> print('I\'m learning\tPython.')
I'm learning Python.
>>> print('\\\n\\')
>>> print('\\\t\\')
>>> print(r'\\\t\\')
A variable is a name that refers to a value.
An assignment statement creates new variables and gives them values.
>>> message = 'something'
>>> n = 17
>>> pi = 3.1415926535897931
To display the value of a variable, you can use a print statement.
>>> print(n)
17
>>> print(pi)
3.141592653589793
The type of a variable is the type of the value it refers to.
>>> type(message)
str
>>> type(n)
int
>>> type(pi)
float
Note that $=$ means "assignment", not equation.
>>> x = 15
x = x + 10
Python is a Dynamically Typed Language. What is Dynamically Typed Language?
>>> apple = 123
# apple is an integer
>>> print(apple)
123
>>> apple = 'ABC'
# Now apple turns to be a string
>>> print(apple)
ABC
We can assign the value of a variable to another variable.
>>> a = 'ABC'
>>> b = a
>>> print(b)
ABC
>>> 76trombones = 'big parade'
SyntaxError: invalid syntax
>>> more@ = 1000000
SyntaxError: invalid syntax
>>> class = 'Advanced Theory'
SyntaxError: invalid syntax
and del from None True
as elif global nonlocal try
assert else if not while
break except import or with
class False in pass yield
continue finally is raise
def for lambda return
You would be better to use recognizable names. Check the following names.
>>> a = 35.0
>>> b = 12.50
>>> c = a * b
>>> print(c)
>>> hours = 35.0
>>> rate = 12.50
>>> pay = hours * rate
>>> print(pay)
>>> x1q3z9ahd = 35.0
>>> x1q3z9afd = 12.50
>>> x1q3p9afd = x1q3z9ahd * x1q3z9afd
>>> print(x1q3p9afd)
Which ones are better?
$+,-,*,/,**$(exponentiation), %, //
Order of operations
>>> minute=50
>>> minute/20
2.5 # float
>>> minute=50
>>> minute//20
2 # floored integer
>>> minute=50
>>> minute%20
10 # remainder. useful!
>>> student_number=2017003425
>>> student_number//1000000
2017
>>> student_number%10000
3425
String operations
>>> first=10
>>> second=15
>>> print(first+second)
25
>>> first='100'
>>> second='150'
>>> print(first + second)
100150
input() and print()
>>> x = input()
Some silly stuff
>>> print(x)
Some silly stuff
You can pass a string to input to be displayed to the user before pausing for input
>>> name = input('What is your name?\n')
What is your name?
Chuck # This is what you input
>>> print(name)
Chuck # The type is string.
If you expect the user to type an integer, you can try to convert the return value to integer using the int() function
>>> prompt = 'What is the speed?\n'
>>> speed = input(prompt)
What is the speed?
17
>>> int(speed)
# or float(speed)
17
>>> int(speed) + 5
22
It may cause error, if the value cannot be converted.
>>> speed = input(prompt)
What is the speed?
I do not know. #This is what you input.
>>> int(speed)
ValueError: invalid literal for int() with base 10:
To write or display long program, Editor will be used. Green box shows the output of a sequence of code. Red box shows the error message. Basically, the Editor is the same with IPython console.
age = 23
message = "Happy " + age + "rd Birthday!"
print(message)
Traceback (most recent call last):
File "birthday.py", line 2, in <module>
message = "Happy " + age + "rd Birthday!"
TypeError: Can't convert 'int' object to str implicitly
age = 23
message = "Happy " + str(age) + "rd Birthday!"
print(message)
Happy 23rd Birthday!