Python——基本语法(一)

一、程序和⽤户交互

python 中 使⽤ input 函数实现

复制代码
input("这⾥写提示信息, 必须使⽤引号引起来")

二、变量

复制代码
In [1]: n = input("输入数字")
输入数字8

In [2]: n
Out[2]: '8'

1.变量命名潜规则:

  • 不要以单下划线和双下划线开头;如:_user或 __user

  • 变量命名要易读;如:user_name,⽽不是username

  • 不⽤使⽤标准库中(内置)的模块名或者第三⽅的模块名

  • 不要⽤这些 Python 内置的关键字:

    In [4]: keyword.kwlist
    Out[4]:
    ['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']

2.如何理解 python 的变量赋值

复制代码
s = 'hello'
  • hello 这个对象会在内存中先被创建,之后再把变量名 s 分配给这个对象。
  • 所以要理解 Python 中的变量赋值,应该始终先看 等号右边 。
  • 对象是在右边先被创建或者被获取 ,在此之后左边的变量名才会被绑定到对象上,这就像是为对象贴上了⼀个标签。
  • ⼀个对象可以有多个标签或者名字 。 ⽐如: 我们⾃⼰就有很多名字,身份证上的名字,⽹络昵称等。

示例:

复制代码
In [5]: a = 1

In [6]: b = a

In [7]: a = 2

In [8]: b
Out[8]: 1

3.多元赋值

字符串以及后⾯讲的列表、元组都⽀持这种操作,也要元组解包

复制代码
In [1]: n1, n2 = 1, 2

In [2]: n1
Out[2]: 1

In [3]: n2
Out[3]: 2

In [4]: s1, s2 = '12'

In [5]: s1
Out[5]: '1'

In [6]: s2
Out[6]: '2'

In [7]: num, s = [10, 'hello']

In [8]: num
Out[8]: 10

In [9]: s
Out[9]: 'hello'

三、判断条件

复制代码
In [10]: n = 10

In [11]: n == 10
Out[11]: True

In [12]: n != 10
Out[12]: False

In [13]: n > 10
Out[13]: False

In [14]: n < 10
Out[14]: False

In [15]: n <= 10
Out[15]: True

In [16]: n >= 10
Out[16]: True

示例

复制代码
In [17]: n = input("请输入一个数字:")
请输入一个数字:10

In [18]: n == 10
Out[18]: False

会发现返回 False 在编程语⾔中 ,数据是有类型之分的。input() 接收到的任何数据都会成为 字符串类型(str),就是普通的字符串 ⽽ 我们等号 右边的 10 是整型(int)

四、 数据类型

1.查看数据的类型,使⽤ type

复制代码
In [19]: n
Out[19]: '10'

In [20]: type(n)
Out[20]: str

In [21]: type(10)
Out[21]: int

2.基本的数据类型

整型(int)

复制代码
In [21]: type(10)
Out[21]: int

In [22]: type(0)
Out[22]: int

In [23]: type(-1)
Out[23]: int

In [24]: type(1)
Out[24]: int

浮点型(带⼩数点的⼩数)

复制代码
In [25]: type(1.1)
Out[25]: float

In [26]: type(-1.1)
Out[26]: float

布尔型

复制代码
In [27]: type(True)
Out[27]: bool

In [28]: type(False)
Out[28]: bool

字符串(str)

复制代码
In [29]: type('10')
Out[29]: str

In [30]: type('hello')
Out[30]: str

In [31]: type('-1.1')
Out[31]: str

⼆进制(bytes)

复制代码
In [32]: type(b'hello')
Out[32]: bytes

五、类型转换

转换为 int

复制代码
In [33]: int('10')
Out[33]: 10

In [34]: int('-10')
Out[34]: -10

In [36]: int(1.9)
Out[36]: 1

转换为 float

复制代码
In [37]: float(1)
Out[37]: 1.0

In [38]: float(-1)
Out[38]: -1.0

In [39]: float('1.1')
Out[39]: 1.1

转换为 str

复制代码
In [40]: str(111)
Out[40]: '111'

In [41]: str(111.1)
Out[41]: '111.1'

In [42]: str(b'hello', encoding='utf-8')
Out[42]: 'hello'

转换为⼆进制

复制代码
In [43]: bytes('亮', encoding='utf-8')
Out[43]: b'\xe4\xba\xae'

In [44]: b = bytes('亮', encoding='utf-8')

In [45]: b
Out[45]: b'\xe4\xba\xae'

In [47]: str(b, encoding='utf-8')
Out[47]: '亮'

In [48]: type(b)
Out[48]: bytes

In [49]: s = str(b, encoding='utf-8')

In [50]: s
Out[50]: '亮'

In [51]: type(s)
Out[51]: str

六、if 判断语句

语法⼀:

复制代码
if 判断条件: # 冒号必须的
 如果判断条件为真,执⾏这⾥的代码,这⾥的代码必须缩进4个空
格
 并且每⼀⾏代码的缩进要⼀致

示例

复制代码
In [52]: n = input("请输入一个数字:")
请输入一个数字:18

In [53]: int(n)
Out[53]: 18

In [54]: type(n)
Out[54]: str

In [55]: n = int(n)

In [56]: type(n)
Out[56]: int

In [57]: if n == 18:
    ...:     print("相等")
    ...: 
相等

语法⼆:

复制代码
if 判断条件:
 如果判断条件为真,执⾏这⾥的代码
else: # 这⾥的冒号也是必须的
 如果判断条件为假,执⾏这⾥的代码,这⾥的代码必须缩进4个空
格
 并且每⼀⾏代码的缩进都要⼀致

In [58]: n = 19

In [59]: if n == 18:
    ...:     print("相等")
    ...: else:
    ...:     print("不相等")
    ...: 
不相等

语法三:

复制代码
if 判断条件:
 如果判断条件添加为真,执⾏这⾥的代码,这⾥的代码必须缩进4
个空格
 并且每⼀⾏代码的缩进要⼀致
elif 判断条件: # 这⾥同样需要加条件
 如果判断条件添加为真,执⾏这⾥的代码,这⾥的代码必须缩进4
个空格
 并且每⼀⾏代码的缩进要⼀致
else: # 这⾥的冒号也是必须的
 如果判断条件为假,执⾏这⾥的代码,这⾥的代码必须缩进4个空
格
 并且每⼀⾏代码的缩进都要⼀致        

elif 根据需求可以出现多个

示例

复制代码
In [61]: n = 17

In [62]: if n == 18:
    ...:     print("相等")
    ...: elif n > 18:
    ...:     print("大了")
    ...: elif n < 18:
    ...:     print("小了")
    ...: 
小了

七、Python 程序

通常我们会把程序的代码写的⼀个⽂件种,这个⽂件就成为 Python 的⼀个程序⽂件,⽂件名⼀般都是以 .py 为结尾,有时候也成为 ⼀个 python 的程序。

需求:使⽤ vi 编辑器,写猜数游戏的⼩程序

示例:

复制代码
[root@192 ~]# vi hello.py

#!/usr/bin/env python3
# file name hello.py

print("猜数游戏开始")

n = input("请输⼊⼀个数字")
n = int(n)

if n == 18:
    print("猜对了")
elif n > 18:
    print("⼤了")
else:
    print("⼩了") 

第⼀⾏不是注释,和 shell 脚本⼀样,是在声明这个脚本默认使⽤的解释器

执⾏ python 程序

复制代码
[root@192 ~]# python3 hello.py 
猜数游戏开始
请输⼊⼀个数字19
⼤了
[root@192 ~]# python3 hello.py 
猜数游戏开始
请输⼊⼀个数字8
⼩了
[root@192 ~]# chmod +x hello.py     #给权限
[root@192 ~]# ./hello.py 
猜数游戏开始
请输⼊⼀个数字8
⼩了
相关推荐
冰橙子id15 分钟前
linux系统安全
linux·安全·系统安全
stark张宇20 分钟前
VMware 虚拟机装 Linux Centos 7.9 保姆级教程(附资源包)
linux·后端
Johny_Zhao27 分钟前
Ubuntu系统安装部署Pandawiki智能知识库
linux·mysql·网络安全·信息安全·云计算·shell·yum源·系统运维·itsm·pandawiki
小陈phd39 分钟前
李宏毅机器学习笔记——梯度下降法
人工智能·python·机器学习
悲伤小伞41 分钟前
linux_git的使用
linux·c语言·c++·git
kk爱闹1 小时前
【挑战14天学完python和pytorch】- day01
android·pytorch·python
Blossom.1181 小时前
机器学习在智能建筑中的应用:能源管理与环境优化
人工智能·python·深度学习·神经网络·机器学习·机器人·sklearn
亚力山大抵1 小时前
实验六-使用PyMySQL数据存储的Flask登录系统-实验七-集成Flask-SocketIO的实时通信系统
后端·python·flask
showyoui1 小时前
Python 闭包(Closure)实战总结
开发语言·python
amazinging2 小时前
北京-4年功能测试2年空窗-报培训班学测开-第四十一天
python·学习·appium