Python程式設計:從入門到實踐—變數和簡單資料型別
變數的命名和使用
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
message ="Hello Python world!"
print(message)
在Python中使用變數時,需要遵守一些規則。
- 變數名只能包含字母、數字和下劃線。變數名可以字母或下劃線開頭,但不能以數字開頭,例如,可將變數命名為message_1,而不能為1_message.
- 變數名不能包含空格,但可使用下劃線來分割其中的單詞。例如:變數名greeting_message可行,但變數名greeting message會引起錯誤。
- 不能將Python關鍵字和函式名用作變數名,即不要使用Python保留用於特殊用途的單詞,如print
- 變數名應既簡短又具有描述性。例如name比n好,student_name比s_n好,name_length比length_of_persons_name好
- 慎用小寫字母l和大寫字母O,因為它們可能被人錯看成數字1和0
字串
使用方法修改字串的大小寫
name = "ada lovelace"
print(name)
print(name.title())
print(name.upper())
print(name.lower())
執行結果:
ada lovelace
Ada Lovelace
ADA LOVELACE
ada lovelace
合併(拼接)字串
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
first_name = "scott"
last_name = "lovelace"
full_name = first_name + " " + last_name
print(full_name)
print("Hello, " + full_name.title() + "!")
可以使用拼接建立訊息,把整條訊息都儲存在一個變數中:
message = "Hello, " + full_name.title() + "!"
print(message)
執行結果:
scott lovelace
Hello, Scott Lovelace!
Hello, Scott Lovelace!
使用製表符或換行符來新增空白
>>> print("\tPython")
Python
>>> print("Languages:\nPython\nC\nJavaScript")
Languages:
Python
C
JavaScript
>>> print("Languages:\n\tPython\n\tC\n\tJavaScript")
Languages:
Python
C
JavaScript
>>>
刪除空白
>>> favorite_language = 'python '
>>> favorite_language
'python '
>>> favorite_language.rstrip()
'python'
>>> favorite_language
'python '
要永久刪除這個字串的空白,必須將刪除操作的結果存回到變數中:
>>> favorite_language = 'python '
>>> favorite_language = favorite_language.rstrip()
>>> favorite_language
'python'
還可以剔除字串開頭的空白,或同時剔除字串兩端的空白:
>>> favorite_language = ' python '
>>> favorite_language.lstrip()
'python '
>>> favorite_language.rstrip()
' python'
>>> favorite_language.strip()
'python'
數字
整數:
>>> 2 + 3
5
>>> 3 - 2
1
>>> 2 * 3
6
>>> 3 / 2
1.5
>>> 3 ** 2
9
>>> 10 ** 6
1000000
>>> 2 + 3*4
14
>>> (2 + 3) * 4
20
浮點數:
>>> 0.1 + 0.1
0.2
>>> 0.2 + 0.2
0.4
>>> 2 * 0.1
0.2
>>> 2 * 0.2
0.4
>>> 0.2 + 0.1
0.30000000000000004
>>> 3 * 0.1
0.30000000000000004
使用函式str()避免型別錯誤
>>> age = 23
>>> message = "Happy " + age + "rd Birthday!"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly
>>> message = "Happy " + str(age) + "rd Birthday!"
>>> print(message)
Happy 23rd Birthday!
Python2中的整數
[email protected]:~$ python
Python 2.7.12 (default, Nov 12 2018, 14:36:49)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 3 / 2
1
>>> 3.0 / 2
1.5
>>> 3 / 2.0
1.5
>>> 3.0 / 2.0
1.5
Python之禪
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!