1. 程式人生 > >Python編程:從入門到實踐—變量和簡單數據類型

Python編程:從入門到實踐—變量和簡單數據類型

javascrip may ref 2.7 info HERE script 變量名 結果

變量的命名和使用

#!/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中的整數

elon@vhost1:~$ 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!

Python編程:從入門到實踐—變量和簡單數據類型