《Python编程:从入门到实践》-1 变量和简单数据类型

2022-08-20  本文已影响0人  Yayamia

记录一些对自己重要的要点

运行.py文件:切换到对应路径

python3 hellp_world.py

关于变量名

关于字符串

first = "ada"
second = "haha"
full_name = f"{first} {second}"
print(full_name)
print(f"hello,{full_name.title()}")

ada haha
hello,Ada Haha

对于python3.6以前的版本,需要使用 format()格式,可在圆括号内列出要在字符串中使用的变量,对于每个变量都需要通过一对花括号来引用

full_name ="{} {}".format(first, second)

关于制表符和换行符

>>> print("hello")
hello
>>> print("\thello")
        hello
>>> print("hello\nnext line\ttab\t\nnext and tab")
hello
next line       tab     
next and tab
>>> print("hello\nnext line\ttab\n\tnext and tab")
hello
next line       tab
        next and tab

删除空白

>>> fu = "   aaa    "
>>> fu
'   aaa    '
>>> fu.rstrip()
'   aaa'
>>> fu.lstrip()
'aaa    '
>>> fu.strip()
'aaa'
>>> fu = " aaa aaa aa"
>>> fu.strip()
'aaa aaa aa'

运算符号

浮点数

数字中的下划线:仅方便易于读取

>>> big_num = 12_000_000_000
>>> big_num
12000000000
>>> print(big_num)
12000000000

同时给多个变量赋值

>>> x, y, z = 1, 2, 3
>>> y
2

这样做时需要用逗号将变量名分开,对于要赋给变量的值,也需要同样处理,有顺序。

对于常量

一般使用全大写在指出将某个变量设为常量

MAX_VALUE = 5000
上一篇 下一篇

猜你喜欢

热点阅读