2.2 变量
message = "Hello Python world!"
print(message)
message = "Hello Python Crash Course World!"
print(message)
Hello Python world!
Hello Python Crash Course World!
2.2.1 变量的命名和使用
1.变量名只能包含字母、数字和下划线。变量名可以字母和下划线开头,但不能一数字开头。例如,可将变量名命名为message_1,但不能将其命名为1_message.
2.变量名不能包含空格,但可以使用下划线来分隔其中的单词。例如,变量名greeting_message可行,但变量名greeting message会引发错误。
3.不要将Python关键子和函数名用作变量名,即不要使用Python保留用于特殊用途的单词,如print
4.变量名应该既简短又具有描述性,例如,name比n好,student_name比s_n好,name_length比length_of_name好。
5.慎用小写字母l和大写字母O,因为他们可能错看成数字1和0.
2.2.2 使用变量时避免错误
message = "Hello Python Crash Course reader!"
print(mesage)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-c8f2adeaed02> in <module>
1 message = "Hello Python Crash Course reader!"
----> 2 print(mesage)
NameError: name 'mesage' is not defined
2.3 字符串
字符串就是一系列字符。在Python中,用引号括起来的都是字符串,其中的引号可以是’ '(单引号),也可以是" "双引号
print('This is a string')
print("This is also a string")
This is a string
This is also a string
2.3.1 使用方法修改字符串的大小写
name = "ada lovelace"
print(name.title())
Ada Lovelace
title( ) 以首字母大写的方式显示每个单词
name = "Ada Lovelace"
print(name.upper())
print(name.lower())
ADA LOVELACE
ada lovelace
upper( ) 显示大写字母
lower( ) 显示小写字母
2.3.2 合并(拼接)字符串
Python使用加号 (+) 来合并字符串
first_name = "ada"
last_name = "lovelace"
full_name = first_name + " " + last_name
print(full_name)
ada lovelace
first_name = "ada"
last_name = "lovelace"
full_name = first_name + " " + last_name
message = "Hello," + full_name.title() + "!"
print(message)
Hello,Ada Lovelace!
2.3.3 使用制表符和换行符来添加空白
在编程中,空白泛指任何非打印字符,如空格、制表符和换行符。
要在字符串中添加制表符,可以用字符组合 \t
要在字符串中添加换行符,可以用字符组合 \n
print("Python")
print("\tPython")
Python
Python
print("Languages:\nPython\nC\nJavaScript")
Languages:
Python
C
JavaScript
print("Languages:\n\tPython\n\tC\n\tJavaScript")
Languages:
Python
C
JavaScript
2.3.4 删除空白
在Python中,能够找出字符串中开头和末尾多余的空白。
要确保字符串的末尾没有空白,,可以使用方法rstrip( )
favorite_language = ' python '
favorite_language.rstrip()
' python'
要确保字符串的开头没有空白,,可以使用方法lstrip( )
favorite_language = ' python '
favorite_language.lstrip()
'python '
同时剔除字符串的两端的空白,可以使用方法strip( )
favorite_language = ' python '
favorite_language.strip()
'python'
2.3.5 使用字符串时避免语法错误
message = 'One of Python's strengths is its diverse community.'
print(message)
File "<ipython-input-14-a9250de55b39>", line 1
message = 'One of Python's strengths is its diverse community.'
^
SyntaxError: invalid syntax
message = "One of Python's strengths is its diverse community."
print(message)
One of Python's strengths is its diverse community.
2.4 数字
2.4.1 整数
在Python中,可以对整数执行加 ( + ) 减 ( - ) 乘 ( * ) 除 ( / ) 运算
2 + 3
5
3 - 2
1
2 * 3
6
3 / 2
1.5
在Python中使用两个乘号表示乘方运算
3 ** 2
9
3 ** 3
27
10 ** 6
1000000
在Python中可以使用 ( ) 来修改运算次序
2 + 3 * 4
14
(2 + 3) * 4
20
2.4.2 浮点数
在Python中带小数点的数字都称为 浮点数
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
2.4.3 使用函数str( )避免类型错误
age = 23
message = "Happy " + age + "rd Birthday!"
print(message)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-30-80a141e301d6> in <module>
1 age = 23
----> 2 message = "Happy " + age + "rd Birthday!"
3 print(message)
TypeError: can only concatenate str (not "int") to str
TypeError是类型错误,在示例中,使用了一个值为整数 (int) 的变量,这个表示的可能是数值23,也可能是字符2和3.为此,可调用函数 str( ) ,将非字符串值表示为字符串。
age = 23
message = "Happy " + str(age) + "rd Birthday!"
print(message)
Happy 23rd Birthday!
2.5 注释
2.5.1 如何编写注释
# 向大家问好
print("Hello Python people!")
Hello Python people!