Python基本语法

1.程序和用户交互

Python中,使用input函数实现

input("这里写提示信息, 必须使用引号引起来")

2.变量

用户的输入可以使用一个变量来接收

s = input("请输入一个数字")

例:
In [1]: n = input("请输入一个数字:")
请输入一个数字:9

In [2]: n
Out[2]: '9'
变量命名规则:

①不要以单下划线和双下划线开头

②变量名不得使用标准库(内置)的模块或者第三方的模块名

③不要使用Python内置的关键字命名

In [3]: import keyword

In [4]: keyword.kwlist
Out[4]:
['False',
 'None',
 'True',
 'and',
 'as',
 'assert',
 'async',
 'await',
 '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']

Python中变量的赋值过程

        s = 'hello'

变量名s分配给hello这个对象;----Python中一切皆对象

hello这个对象会在内存中显先被创建,之后再把变量名s分配给这个对象。

对象时在右边先被创建或者获取,在此之后左边的变量名才会被绑定到对象上,就相当于为对象贴上了一个标签。

一个对象可以有多个标签或者名字。

例如:

In [5]: a = 1

In [6]: b = a

In [7]: a = 2

In [8]: print(b)
1

多元赋值

多元赋值:同时给多个变量进行赋值


In [9]: n1, n2 = 1, 4

In [10]: n1
Out[10]: 1

In [11]: n2
Out[11]: 4

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

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

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

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

In [16]: num
Out[16]: 10

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

3.Python中的判断条件


In [18]: n = 10 #进行赋值

In [19]: n == 10 #等于
Out[19]: True    #条件为真,则返回True

In [20]: n != 10 #不等于
Out[20]: False    #条件为假,则返回False

In [21]: n > 10    #大于
Out[21]: False

In [22]: n < 10    #小于
Out[22]: False

In [23]: n >= 10    #大于等于
Out[23]: True

In [24]: n <= 10    #小于等于
Out[24]: True

例如·

In [30]: n = input("请输入一个数字:")
请输入一个数字:78

In [31]: n == 78
Out[31]: False


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

这里返回False,是因为数据类型 。

在编程中,数据是有类型之分的,input()接收到的任何数据都会成为字符串类型(str)

4.数据类型

查看数据的类型——使用type

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

In [37]: type (n)
Out[37]: str
整型(int)
In [39]: type(0)
Out[39]: int

In [40]: type(-1)
Out[40]: int
浮点数(float)

带小数点的小数

In [41]: type(1.241)
Out[41]: float

In [42]: type(-24432531.2423)
Out[42]: float
布尔型(bool)

In [44]: type(True)
Out[44]: bool

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

字符串(str)

引号引起来的都是字符串

In [46]: type('3124')
Out[46]: str

In [47]: type('hello')
Out[47]: str
二进制(bytes)

用b开头的字符串

In [48]: type(b'hello')
Out[48]: bytes
类型转换
转换为int

In [1]: int('10')
Out[1]: 10

In [2]: int(12.25)
Out[2]: 12
转换为float

In [3]: float(1)
Out[3]: 1.0

In [4]: float(0)
Out[4]: 0.0
转换为str

所有都能转换为字符串

In [1]: str(214)
Out[1]: '214'


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

二进制转换为字符串的时候,需要指定字符编码

转换为二进制

In [3]: bytes('羊羊', encoding=('utf-8'))
Out[3]: b'\xe7\xbe\x8a\xe7\xbe\x8a'

字符串转二进制的时候,需要指定字符编码

5.if语句

语法:

if 条件1:
    # 当条件1成立时执行这里的代码块
elif 条件2:
    # 当条件2成立时执行这里的代码块
elif 条件3:
    # 当条件3成立时执行这里的代码块
else:
    # 当以上条件都不成立时执行这里的代码块
In [10]: if 19 == 18:
    ...:     print("相等")
    ...:     print("猜对了")
    ...: elif 17 > 18:
    ...:     print("大了")
    ...: elif 19 <  18:
    ...:     print("小了")
    ...: else:
    ...:     print("以上条件都不满足")
    ...:
以上条件都不满足

6.循环

语法:

while 条件表达式

        条件表达式为,就执行这里的代码

7.Python程序执行

vim 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("小了")


执行
[root@localhost python_code]# python3 hello.py
猜数游戏开始
请输入一个数字5
小了


[root@localhost python_code]# chmod +x hello.py
[root@localhost python_code]# ./hello.py
猜数游戏开始
请输入一个数字6
小了

相关推荐

  1. python基本语法

    2024-07-21 17:46:03       42 阅读
  2. python基本语法

    2024-07-21 17:46:03       28 阅读
  3. 详解python基本语法

    2024-07-21 17:46:03       17 阅读
  4. Python基本语法

    2024-07-21 17:46:03       18 阅读
  5. Python基础语法基本数据类型(列表)

    2024-07-21 17:46:03       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 17:46:03       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 17:46:03       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 17:46:03       45 阅读
  4. Python语言-面向对象

    2024-07-21 17:46:03       55 阅读

热门阅读

  1. GPU算力租用平台推荐

    2024-07-21 17:46:03       20 阅读
  2. 文本溢出显示省略号

    2024-07-21 17:46:03       15 阅读
  3. 简单回顾C++常用的数据类型

    2024-07-21 17:46:03       18 阅读
  4. https和http有哪些区别?

    2024-07-21 17:46:03       18 阅读
  5. stable diffusion Sampler 的选择

    2024-07-21 17:46:03       18 阅读
  6. 读书记 | 《李光耀观天下》

    2024-07-21 17:46:03       16 阅读
  7. resultMap

    2024-07-21 17:46:03       16 阅读
  8. Python编程防止计算机休眠,保持唤醒状态

    2024-07-21 17:46:03       14 阅读
  9. 力扣题解(盈利计划)

    2024-07-21 17:46:03       18 阅读
  10. Mysql在linux安装报错

    2024-07-21 17:46:03       17 阅读