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
⼩了
相关推荐
fasewer6 分钟前
第五章 linux实战-挖矿 二
linux·运维·服务器
wxin_VXbishe19 分钟前
springboot合肥师范学院实习实训管理系统-计算机毕业设计源码31290
java·spring boot·python·spring·servlet·django·php
ITenderL25 分钟前
Python学习笔记-函数
python·学习笔记
zmjia11127 分钟前
全流程Python编程、机器学习与深度学习实践技术应用
python·深度学习·机器学习
楚灵魈32 分钟前
[Linux]从零开始的网站搭建教程
linux·运维·服务器
小小不董34 分钟前
《Linux从小白到高手》理论篇:深入理解Linux的网络管理
linux·运维·服务器·数据库·php·dba
_.Switch1 小时前
Python机器学习:自然语言处理、计算机视觉与强化学习
python·机器学习·计算机视觉·自然语言处理·架构·tensorflow·scikit-learn
JUNAI_Strive_ving1 小时前
番茄小说逆向爬取
javascript·python
彤银浦1 小时前
python学习记录7
python·学习
这可就有点麻烦了1 小时前
强化学习笔记之【TD3算法】
linux·笔记·算法·机器学习