徐锦洪 发表于 2024-7-22 06:22:09

Python根本语法



1.程序和用户交互

Python中,利用input函数实现
input("这里写提示信息, 必须使用引号引起来") 2.变量

用户的输入可以利用一个变量来吸收
s = input("请输入一个数字")

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

In : n
Out: '9'
变量定名规则:

①不要以单下划线和双下划线开头
②变量名不得利用标准库(内置)的模块或者第三方的模块名
③不要利用Python内置的关键字定名
   In : import keyword
In : keyword.kwlist
Out:
['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 : a = 1

In : b = a

In : a = 2

In : print(b)
1

多元赋值

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

In : n1, n2 = 1, 4

In : n1
Out: 1

In : n2
Out: 4

In : s1, s2 = '12'

In : s1
Out: '1'

In : s2
Out: '2'

In : num, s =

In : num
Out: 10

In : s
Out: 'hello'
3.Python中的判断条件


In : n = 10 #进行赋值

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

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

In : n > 10    #大于
Out: False

In : n < 10    #小于
Out: False

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

In : n <= 10    #小于等于
Out: True
例如·
In : n = input("请输入一个数字:")
请输入一个数字:78

In : n == 78
Out: False


In : type (n)
Out: str   这里返回False,是因为数据类型 。
在编程中,数据是有类型之分的,input()吸收到的任何数据都会成为字符串类型(str)
4.数据类型

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


In : type(10)
Out: int

In : type (n)
Out: str 整型(int)

In : type(0)
Out: int

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

带小数点的小数
In : type(1.241)
Out: float

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


In : type(True)
Out: bool

In : type(False)
Out: bool

字符串(str)

引号引起来的都是字符串
In : type('3124')
Out: str

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

用b开头的字符串
In : type(b'hello')
Out: bytes
类型转换

转换为int


In : int('10')
Out: 10

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


In : float(1)
Out: 1.0

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

全部都能转换为字符串
In : str(214)
Out: '214'


In : str(b'hello',encoding='utf-8')
Out: 'hello'
   二进制转换为字符串的时候,需要指定字符编码
转换为二进制
In : bytes('羊羊', encoding=('utf-8'))
Out: b'\xe7\xbe\x8a\xe7\xbe\x8a'
   字符串转二进制的时候,需要指定字符编码

5.if语句

语法:
if 条件1:
    # 当条件1成立时执行这里的代码块
elif 条件2:
    # 当条件2成立时执行这里的代码块
elif 条件3:
    # 当条件3成立时执行这里的代码块
else:
    # 当以上条件都不成立时执行这里的代码块 In : 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("猜对了")
elifn > 18:
    print("大了")
else:
    print("小了")


执行
# python3 hello.py
猜数游戏开始
请输入一个数字5
小了


# chmod +x hello.py
# ./hello.py
猜数游戏开始
请输入一个数字6
小了


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Python根本语法