变量,字符,数字

变量定义

message = "hello world !!"
print(message)

message = "hello world ,python  !!"
print(message)

命名和使用

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_persons_name好
5,慎用小写字母l和大写字母O,因为它们可能被人错看成数字1和0

注意:应使用小写的Python变量名

字符串

字符串就是一系列字符。在Python中,用引号括起的都是字符串,其中的引号可以是单引号,也可以是双引号

'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." 

字符串的方法修改:
1,首字母大写 title()

name = "ada lovelace"
print(name.title()) 

2,大小写

name = "Ada Lovelace"
print(name.upper())
print(name.lower()) 

3,字符串拼写:使用+ 号合并字符串

first_name = "ada"
last_name = "lovelace"
full_name = first_name + " " + last_name
print(full_name) 

4,制表符换行符添加空白

 print("\tPython") 
 print("Languages:\nPython\nC\nJavaScript")

5,删除空白
字符串末尾没有空白 rstrip() : 只是显示时暂时删除,再次输出时依然包括空白

name = "jjjjvv    "
print(name.rstrip())

删除开头空白: nmae.lstrip()
两端空白: name.strip()

全是临时删除空白。

数字

整数:

对整数执行加(+)减(-)乘(*)除(/)运算 两个乘号表示乘方运算

浮点数:

>>> 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 = 34 
message = "happpy " + str(age) + "  happy "
print(message)

注释

#此为注释

猜你喜欢

转载自www.cnblogs.com/g2thend/p/11729471.html