第二章 变量和简单数据类型
In [1]:
print("Hello Python world!")
Hello Python world!
2.2 添加变量
将一条消息赋给变量,并将其打印出来。
In [2]:
message="Hello Python world!"
print(message)
Hello Python world!
In [3]:
message="Hello Python world!"
print(message)
message="Hello Python Crash Course world!"
print(message)
Hello Python world!
Hello Python Crash Course world!
运行代码块:快捷键ctrl+enter 在下方新增代码区块:单击当前代码区块左侧→【B】 在上方新增代码区块:单击当前代码区块左侧→【A】 取消代码改为文本:单击当前代码区块左侧→【M】、标题变蓝【#】
变量名只能包含字母/数字/下划线。
变量:存储值的盒子。
变量名只能以字母/下划线开头,不能以数字开头。 eg.变量名message_1可行, 1_message不可行
变量名不能包含空格使用下划线分割其中的单词。 eg.变量名greeting_message可行, greeting message不可行。
变量名需简单且具备描述性。 eg.name比n好, student比s_n好, name_length比length_of_persons_name好。
慎用小写字母l和大写字母O,因为容易与数字1和数字0混淆。
避免细小错误: eg.拼写错误mesage
2.3 字符串
用引号括起的都是字符串,可以是单引号可以是双引号。 这种灵活性可以让你在字符串中包含引号和撇号。
eg.'I told my friend,"Python is my favorite language!"' "The language'Python' is named after Monty Python,not the snake." "One of Python's strengths is its diverse and supportive community."
In [4]:
name="ada lovelace"
print(name.title())
Ada Lovelace
name后面的句点(.)让Python对变量name执行方法title()指定的操作。
name:变量
方法title():将每个单词的首字母都改为大写
字符串全大写:方法upper() 字符串全小写:方法lower()
In [5]:
name="Ada Lovelace"
print(name.upper())
print(name.lower())
ADA LOVELACE
ada lovelace
f():Python3.6引进的,Python3.5或更早的版本,需要使用format()全称。 f()或format():设置格式
In [6]:
first_name="ada"
last_name="lovelace"
full_name=f"{first_name}{last_name}"
print(full_name)
adalovelace
In [7]:
first_name="ada"
last_name="lovelace"
full_name=f"{first_name} {last_name}"
print(full_name)
ada lovelace
In [8]:
first_name="ada"
last_name="lovelace"
full_name=f "{first_name} {last_name}"
print(full_name)
#f后面不能空格
File "<ipython-input-7-bdb768459141>", line 3 full_name=f "{first_name} {last_name}" ^SyntaxError: invalid syntax
Type Markdown and LaTeX: ��2α2
In [10]:
first_name="ada"
last_name="lovelace"
full_name=f"{first_name} {last_name}"
print(f"hello,{full_name.title()}!")
hello,Ada Lovelace!
制表符:\t
#tab 换行符:\n
#new line
In [12]:
print("python")
print("\tpython")
python
python
In [13]:
print("laguages:\npython\nC\nJavaScript")
laguages:
python
C
JavaScript
In [14]:
print("laguages:\tpython\tC\tJavaScript")
laguages: python C JavaScript
In [15]:
print("laguages:\n\tpython\n\tC\n\tJavaScript")
#先换行再制表
laguages:
python
C
JavaScript
In [16]:
print("laguages:\t\npython\t\nC\t\nJavaScript")
laguages:
python
C
JavaScript
剔除函数: lstrip():剔除字符串开头空白 rstrip():剔除字符串结尾空白 strip():剔除字符串两边空白
2.4 数
In [18]:
2+3
Out[18]:
5
In [19]:
3-2
Out[19]:
1
In [20]:
3*2
Out[20]:
6
In [21]:
3/2
Out[21]:
1.5
In [22]:
3**2
Out[22]:
9
In [23]:
3**3
Out[23]:
27
In [24]:
2+3*4
Out[24]:
14
In [26]:
(2+3)*4
#不能是中文括号
Out[26]:
20
In [27]:
0.1+0.1
Out[27]:
0.2
In [28]:
0.2+0.1
#结果包含的小数位数可能是不确定的,正常现象
Out[28]:
0.30000000000000004
书写很大的数字时,可使用下划线将其中的数字分组,使其清晰易懂。 eg.universe_age=14_000_000
1_000和10_00没什么不同。
In [29]:
universe_age=14_000_000
print(universe_age)
#python不会打印下划线
14000000
In [30]:
x,y,z=1,2,3
#同时给多个变量赋值
2.5 注释
用井号(#)编写注释,井号(#)后面的内容都会被Python解释器忽略。
2.6指导原则
In [31]:
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!