玩命加载中 . . .

python-变量


2.1变量

In [2]:

message="Hello Python World"
print(message)
Hello Python World

2.2.1 变量的命名和使用

变量名只能包含字母、数字和下划线
注:

1、变量名可以以字母或下划线为开头,但不能以数字为开头,例:变量名可以为message_1,但不可为1_message;
2、变量名不可包含空格,但可用下划线分割其中的单词,例:变量名message_rander可行,但message rander会引发错误;
3、关键字与函数名不可用作变量(不可使用python中留作特殊用途的字符),例:print;
4、变量命名时应注意简短又具有描述性,例:name比n好,student_name比s_n好;
5、慎用小写字母l和大写字母O,防止被误看作为1和0.

2.2.2 避免变量命名的错误

In [3]:

message="Hello python reader!"
print(mesage)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-c63e888b4f93> in <module>
      1 message="Hello python reader!"
----> 2 print(mesage)

NameError: name 'mesage' is not defined

2.3 字符串

在python中,单、双引号中的内容,均为字符串

In [4]:

print("this is a string")


print('this is also a string')
this is a string
this is also a string

2.3.1使用方法修改字符串大小写

In [6]:

message="edgar love python"

print(message.title())
Edgar Love Python

In [7]:

message="EDGAR LOVE PYTHON"

print(message.lower())
edgar love python

In [8]:

message="edgar love python"

print(message.upper())
EDGAR LOVE PYTHON

2.3.2 合并字符串

In [9]:

message1="edgar"

message2="love"

message3="python"

message=message1+message2+message3

print(message)
edgarlovepython

2.3.3 使用制表符或换行符来添加空白

In [12]:

print("\tpython")

print("python")
    python
python

In [14]:

print("Languages:\nC\nPython\nJava")
Languages:
C
Python
Java

In [15]:

print("Languages:\n\tC\n\tPython\n\tJava")
Languages:
    C
    Python
    Java

2.3.4 删除空白

In [19]:

favorite_language=" python "

favorite_language.rstrip()

Out[19]:

' python'

In [20]:

favorite_language=" python "

favorite_language.lstrip()

Out[20]:

'python '

2.3.5 使用时避免语法错误

In [21]:

message='One of Python's strength is its diverse community.'

print(message)
  File "<ipython-input-21-67bbb0f75f99>", line 1
    message='One of Python's strength is its diverse community.'
                           ^
SyntaxError: invalid syntax

In [22]:

message="One of Python's strength is its diverse community."

print(message)
One of Python's strength is its diverse community.

2.4.1 整数

In [23]:

2+3

Out[23]:

5

In [24]:

5/2

Out[24]:

2.5

In [25]:

3**2

Out[25]:

9

In [26]:

8**12

Out[26]:

68719476736

2.4.2 浮点数

In [27]:

0.1+0.1

Out[27]:

0.2

In [28]:

0.2+0.1

Out[28]:

0.30000000000000004

2.4.3 使用函数str避免类型错误

In [29]:

age = 23

message = "Happy " + 23 + "rd Birthday!"

print(message)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-a1b5e512e2b0> in <module>
      1 age = 23
      2 
----> 3 message = "Happy " + 23 + "rd Birthday!"
      4 
      5 print(message)

TypeError: can only concatenate str (not "int") to str

In [30]:

age = 23

message = "Happy " + str(23) + "rd Birthday!"

print(message)
Happy 23rd Birthday!

2.5.1 如何编写注释

In [31]:

#向大家问好
print("Hello Python World!")
Hello Python World!

文章作者: Edgar
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Edgar !
评论
  目录