自学Python一定要知道的变量与内置函数

1,107 阅读5分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

1.内置函数

在 Python 中,我们有很多内置函数。内置函数可供您全局使用,这意味着您无需导入或配置即可使用内置函数。一些最常用的 Python 内置函数如下:print()、len()、type()、int()、float()、str()、input()、list()、dict()、min()、max()、sum()、sorted()、open()、file()、help()和dir(). 在下表中,您将看到取自python 文档的 Python 内置函数的详尽列表。

image.png

让我们打开 Python shell 并开始使用一些最常见的内置函数。

image.png 让我们通过使用不同的内置函数来练习更多

image.png 从上面的终端可以看出,Python 有保留字。我们不使用保留字来声明变量或函数。我们将在下一节介绍变量。

我相信,现在您已经熟悉内置函数了。让我们再做一次内置函数的练习,我们将继续下一节。

image.png

2.变量

image.png 变量将数据存储在计算机内存中。推荐在许多编程语言中使用助记变量。助记变量是易于记忆和关联的变量名称。变量是指存储数据的内存地址。命名变量时不允许以数字开头、特殊字符、连字符。变量可以有一个简短的名称(如 x、y、z),但强烈建议使用更具描述性的名称(名字、姓氏、年龄、国家/地区)。

Python 变量名规则

  • 变量名必须以字母或下划线字符开头
  • 变量名不能以数字开头
  • 变量名称只能包含字母数字字符和下划线(Az、0-9 和 _ )
  • 变量名区分大小写(firstname、Firstname、FirstName 和 FIRSTNAME)是不同的变量)

让我们设置有效的变量名



年龄
国家
城市


首都_城市
_if #如果我们想使用保留字作为变量
年_2021
2021
当前_year_2021
出生年
编号 1
数量 2

无效的变量名

名
名
第一个$name
数字 1
1号

我们将使用许多 Python 开发人员采用的标准 Python 变量命名风格。Python 开发人员使用蛇形大小写(snake_case)变量命名约定。对于包含多个单词的变量(例如 first_name、last_name、engine_rotation_speed),我们在每个单词后面使用下划线字符。下面的例子是一个标准的变量命名示例,当变量名超过一个单词时需要下划线。

当我们为变量分配某种数据类型时,称为变量声明。例如,在下面的示例中,我的名字被分配给变量 first_name。等号是赋值运算符。赋值意味着将数据存储在变量中。Python 中的等号与数学中的等号不同。

例子:

#在Python变量
FIRST_NAME  =  'Asabeneh'
姓氏 =  'Yetayeh'
国家 =  '芬兰
城市 =  '赫尔辛基'
年龄 =  250 
is_married  = 真
技能 = [ 'HTML''CSS''JS''阵营'' Python' ]
 person_info  = {
    'firstname' : 'Asabeneh' ,
    'lastname' : 'Yetayeh' ,
    '国家''芬兰',
    '城市' : '赫尔辛基' 
   }

让我们使用print()和len()内置函数。打印函数采用无限数量的参数。参数是我们可以传递或放入函数括号内的值,请参见下面的示例。

例子:

print ( 'Hello, World!' ) # 文本 Hello, World! 是一个参数
print ( 'Hello' , ',' , 'World' , '!' ) # 它可以接受多个参数,已经传递了四个参数
print ( len ( 'Hello, World!' )) # 它只需要一个争论

让我们打印并找出在顶部声明的变量的长度:

例子:

打印('名字长度:',first_name)
打印('名字长度:'len(first_name))
打印('姓氏:',last_name)
打印('姓氏长度:'len(last_name))
打印('国家:',国家)
打印('城市:',城市)
打印('年龄:',年龄)
打印('已婚:' , is_married )
打印( '技能: ' ,技能)
打印( '个人信息: ' , person_info )

2.1在一行中声明多个变量

也可以在一行中声明多个变量:

例子:

first_name , last_name , country , age , is_married  =  'Asabeneh' , 'Yetayeh' , 'Helsink' , 250 , True

打印(FIRST_NAME,姓氏,国家,年龄,is_married)
打印('名字:',FIRST_NAME)
打印('姓:',姓氏)
打印('国家:',国家)
打印('年龄:',年龄)
打印('已婚:',is_已婚)

使用input()内置函数获取用户输入。让我们将从用户那里获得的数据分配给 first_name 和 age 变量。
例子:

first_name  =  input ( '你叫什么名字:' )
 age  =  input ( '你多大了?')

打印(名字)
打印(年龄)

2.2数据类型

Python 中有多种数据类型。为了识别数据类型,我们使用type内置函数。我想请你专注于很好地理解不同的数据类型。说到编程,全都与数据类型有关。我一开始就介绍了数据类型,它又来了,因为每个主题都与数据类型有关。我们将在它们各自的部分中更详细地介绍数据类型。

2.3检查数据类型和转换

  • 检查数据类型:要检查某些数据/变量的数据类型,我们使用类型
    示例:
# 不同的python 数据类型
# 让我们声明不同数据类型的变量

first_name  =  'Asabeneh'      # str 
last_name  =  'Yetayeh'        # str 
country  =  'Finland'          # str 
city =  'Helsinki'             # str 
age  =  250                    # int,这不是我的真实年龄,别担心

# 打印类型
print ( type ( 'Asabeneh' ))      # str 
print ( type ( first_name ))      # str 
print ( type ( 10 ))              # int 
print ( type ( 3.14 ))            # float 
print ( type ( 1  +  1j ) )          # complex 
print ( type ( True ))            # bool 
print ( type([ 1 , 2 , 3 , 4 ]))      # list 
print ( type ({ 'name' : 'Asabeneh' , 'age' : 250 , 'is_married' : 250 }))     # dict 
print ( type (( 1 , 2 )))                                               # 元组
打印( type ( zip ([ 1 , 2 ],[ 3 , 4])))                                    # 设置
  • 转换:将一种数据类型转换为另一种数据类型。我们使用int() , float() , str() , list , set
    当我们进行算术运算时,字符串数字应该首先转换为 int 或 float
    ,否则会返回错误。如果我们把一个数字和一个字符串连接起来,这个数字应该首先被转换成一个字符串。我们将在字符串部分讨论连接。

例子:

# int to float 
num_int  =  10 
print ( 'num_int' , num_int )          # 10 
num_float  =  float ( num_int )
 print ( 'num_float:' , num_float )    # 10.0

# 浮到 int
重力 =  9.81
打印( int ( gravity ))              # 9

# int to str 
num_int  =  10 
print ( num_int )                   # 10 
num_str  =  str ( num_int )
 print ( num_str )                   # '10'

# str to int or float 
num_str  =  '10.6' 
print ( 'num_int' , int ( num_str ))       # 10 
print ( 'num_float' , float ( num_str ))   # 10.6

# str to list 
first_name  =  'Asabeneh' 
print ( first_name )                # 'Asabeneh' 
first_name_to_list  =  list ( first_name )
 print ( first_name_to_list )             # ['A', 's', 'a', 'b', 'e', ' n', 'e', 'h']

2.4数字

Python 中的数字数据类型:

  1. 整数:整数(负、零和正)数 示例:… -3, -2, -1, 0, 1, 2, 3 …
  2. 浮点数(十进制数) 示例:… -3.5, -2.25, -1.0, 0.0, 1.1, 2.2, 3.5 …
  3. 复数示例:1 + j、2 + 4j、1 - 1j

更多有关Python学习经验分享,可以关注我,持续更新。