如何区分注释、变量数据类型及其基本输入输出操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1536个文字,预计阅读时间需要7分钟。
Python中的注释、变量数据类型与基本输入输出
一、注释
注释用于解释代码,方便他人阅读或将来自己回顾。在Python中,注释可以使用两种方式:
1. 单行注释:使用#符号开始,直到该行结束。
2.多行注释:使用三个引号(`'''` 或 ``)开始和结束。
例如:
python
单行注释print(这是单行注释)'''多行注释第一行第二行'''
二、变量数据类型
在Python中,变量不需要声明数据类型,变量会根据赋值自动推断类型。Python支持多种数据类型,包括:
1. 数字(int, float, complex)
2.字符串(str)
3.列表(list)
4.元组(tuple)
5.集合(set)
6.字典(dict)
7.布尔值(bool)
例如:
python
num=10 # 整数float_num=3.14 # 浮点数str_num=Hello # 字符串list_num=[1, 2, 3] # 列表tuple_num=(1, 2, 3) # 元组set_num={1, 2, 3} # 集合dict_num={name: 张三, age: 20} # 字典bool_num=True # 布尔值三、基本输入输出
1. 输入:使用`input()`函数,提示用户输入数据。
2.输出:使用`print()`函数,输出内容。
例如:
python
输入name=input(请输入您的名字:)输出print(Hello, + name + !)
python中的注释、变量数据类型与基本输入输出
一、注释
1.单行注释
\# #开头标识单行注释,注释的内容不会被运行# print("hello word")
# ctrl + / 是快捷键。
2.多行注释
"""这是被注释的内容
这是被注释的内容
使用三引号进行换行的多行注释,
"""
二、变量
对于重复使用,并且经常需要修改的数据,可以定义为变量,来提高编程效率。
定义变量的语法为: 变量名 = 变量值 。 (这里的 = 作用是赋值。 )
定义变量后可以使⽤用变量名来访问变量值。
1.变量的定义
a = 1c = 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'>
布尔值
TrueFalse
>>> 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.变量的命名规则(必须执行)
>>> 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 classcontinue 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 = 18name = '晓廖'
print("我的名字叫做 %s,我今年%d岁"% (name,age))
# %d 只能接收字符串
本文共计1536个文字,预计阅读时间需要7分钟。
Python中的注释、变量数据类型与基本输入输出
一、注释
注释用于解释代码,方便他人阅读或将来自己回顾。在Python中,注释可以使用两种方式:
1. 单行注释:使用#符号开始,直到该行结束。
2.多行注释:使用三个引号(`'''` 或 ``)开始和结束。
例如:
python
单行注释print(这是单行注释)'''多行注释第一行第二行'''
二、变量数据类型
在Python中,变量不需要声明数据类型,变量会根据赋值自动推断类型。Python支持多种数据类型,包括:
1. 数字(int, float, complex)
2.字符串(str)
3.列表(list)
4.元组(tuple)
5.集合(set)
6.字典(dict)
7.布尔值(bool)
例如:
python
num=10 # 整数float_num=3.14 # 浮点数str_num=Hello # 字符串list_num=[1, 2, 3] # 列表tuple_num=(1, 2, 3) # 元组set_num={1, 2, 3} # 集合dict_num={name: 张三, age: 20} # 字典bool_num=True # 布尔值三、基本输入输出
1. 输入:使用`input()`函数,提示用户输入数据。
2.输出:使用`print()`函数,输出内容。
例如:
python
输入name=input(请输入您的名字:)输出print(Hello, + name + !)
python中的注释、变量数据类型与基本输入输出
一、注释
1.单行注释
\# #开头标识单行注释,注释的内容不会被运行# print("hello word")
# ctrl + / 是快捷键。
2.多行注释
"""这是被注释的内容
这是被注释的内容
使用三引号进行换行的多行注释,
"""
二、变量
对于重复使用,并且经常需要修改的数据,可以定义为变量,来提高编程效率。
定义变量的语法为: 变量名 = 变量值 。 (这里的 = 作用是赋值。 )
定义变量后可以使⽤用变量名来访问变量值。
1.变量的定义
a = 1c = 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'>
布尔值
TrueFalse
>>> 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.变量的命名规则(必须执行)
>>> 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 classcontinue 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 = 18name = '晓廖'
print("我的名字叫做 %s,我今年%d岁"% (name,age))
# %d 只能接收字符串

