注释、变量数据类型与基本输入输出

python中的注释、变量数据类型与基本输入输出

一、注释

1.单行注释

\# #开头标识单行注释,注释的内容不会被运行

# print("hello word")

# ctrl + / 是快捷键。

2.多行注释

"""
这是被注释的内容
这是被注释的内容

使用三引号进行换行的多行注释,
"""

二、变量

对于重复使用,并且经常需要修改的数据,可以定义为变量,来提高编程效率。
定义变量的语法为: 变量名 = 变量值 。 (这里的 = 作用是赋值。 )
定义变量后可以使⽤用变量名来访问变量值。

1.变量的定义

a = 1
c = 2
b = 123456

name = 'sheldon'

2.变量的数据类型

整形

# 使用type()函数进行变量数据类型的查看

>>> num = 10
>>> print(type(num))
<class 'int'>

浮点型

>>> pie = 3.14159
>>> type(pie)
<class 'float'>

complex 复数

>>> a = (-1) ** 0.5
>>> type(a)
<class 'complex'>

布尔值

True
False

>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>

字符串

# 字符串的定义使用单引号或者是双引号去定义。

>>> name = 'hahha'
>>> type(name)
<class 'str'>
>>> name1 = "fffgg"
>>> type(name1)
<class 'str'>

列表

>>> names = ['sheldon','john','frank',123]
>>> type(names)
<class 'list'>

字典

>>> dict = {"a":1,"b":2}
>>> type(dict)
<class 'dict'>

元组

>>> vars = ("夕阳","河水")
>>> type(vars)
<class 'tuple'>

集合

>>> names = {"a",1,"123","gg"}
>>> type(names)
<class 'set'>

3.变量的命名规则(必须执行)

  1. 标识符由字⺟母、下划线和数字组成,且数字不不能开头。
  2. 严格区分⼤大⼩小写。
  3. 不不能使用关键字。 比如如果布尔值True这个值就不能定义为变量名,会直接报错。
# 区分大小写
>>> m = 1
>>> print(M)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'M' is not defined
    
    
# 正确的命名方式
>>> _a =1
>>> A_b = 1 
>>> 3b  = 12   # 不能用数字开头
  File "<stdin>", line 1
    3b  = 12
     ^
SyntaxError: invalid syntax

常用的冲突关键字

False None True and as assert break class
continue def del elif else except finally for
from global if import in is lambda nonlocal
not or pass raise return try while with
yield

4.命名规范(遵从规范)

小驼峰式命名法(lower camel case):

​ 第⼀个单词以小写字母开始;第二个单词的首字母大写,
​ 例如: myName、 aDog

大驼峰式命名法(upper camel case):

​ 每一个单字的首字母都采⽤用大写字母,例如:
​ 例如:FirstName、 LastName

还有一种命名法是⽤用下划线 _ 来连接所有的单词,比如sendbuf. Python的命令规则遵循PEP8标
准:

变量量名,函数名和⽂文件名全⼩小写,使⽤用下划线连接; 类名遵守⼤大驼峰命名法; 常量名全大写;

三、输出与输入

1.print格式化输出

print("我今年18岁")
print("我今年18岁")
print("我今年18岁")

2.print帮助

# 按住ctrl + 鼠标左键点击函数

def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
    """
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
    """
    pass
    
sep    # 分隔符
end    # 结尾定义 默认“\n”

# 定义分隔符
>>> a = "hahhah"
>>> b = "ggggg"
>>> print(a,b,sep='-------------')
hahhah-------------ggggg


# 定义结尾符号
>>> a = 'aa'
>>> b = 'bb'
>>> c = 'cc'
>>> print(a)
aa
>>> print(b,end='**')
bb**>>> print(c)    # 在bb的后面输出的是** 就没有换行了。
cc
>>>

3.格式化输出

age = 18
name = '晓廖'
print("我的名字叫做 %s,我今年%d岁"% (name,age))

# %d   只能接收字符串
posted @ 2022-04-23 12:30  Gshelldon  阅读(102)  评论(0编辑  收藏  举报