变量的命名和使用
#!/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!
原文地址:https://www.cnblogs.com/elontian/p/9998502.html