Python自动化运维开发----基础(一)

前言:环境是python3python

1.第一个python程序(在学任何一门语言的时候第一程序好像都是hello world),下边咱们用python的解释器去输出一个hello worldide

>>> print("hello world")
hello world
>>>

2.python2和python3中 / 的区别函数

python2中的 / 是整除spa

>>> a = 5
>>> b = 2
>>> a / b
2
>>>

python3中的 / 是除法
code

>>> a = 5
>>> b = 2
>>> a / b
2.5
>>>

从以上两个例子中能够看出来python2中和python3中的 / 的区别orm

python3中的整除是 / /unicode

>>> a = 5
>>> b = 2
>>> a / b
2.5
>>> a // b
2
>>>

从以上结果能够看出python3中的整除是 //字符串

3.python3中的变量的定义和使用
input

(1)变量的定义it

  字符串变量定义的时候加单引号,变量输出的时候直接写变量名字就能够

  如下分别定义一个int、float、str类型的变量

>>> a = 1
>>> type(a)
<type 'int'>
>>> b = 1.3
>>> type(b)
<type 'float'>
>>> c = 'abc'
>>> type(c)
<type 'str'>

(2)变量的命名规范

 A.只能由unicode字符、数字、下划线组成

 B.不能数字开头

 C.避免和python保留字和关键字冲突

 D.避免和python中模块名称,内置函数,内置变量名冲突

 E.避免和使用的第三方模块名冲突

 如何查看python关键字?

>>> import keyword
>>> keyword.kwlist
['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']
>>>

 在python3中是能够定义中文变量的,以下定义一个中文变量并输出中文变量(python2中是不可使用中文变量)

python3

>>> 李宽 = 'likuan'
>>> print(李宽)  
likuan
>>>

python2

>>> 李宽 = 'likuan'
  File "<stdin>", line 1
    李宽 = 'likuan'
    ^
SyntaxError: invalid syntax
>>>

4.输入语句、输出语句

输入:使用input函数从键盘输入一个变量

输出:使用print函数输出输入的变量

>>> name = input("请输入你的名字:")
请输入你的名字:李宽
>>> print("你的名字是:",name)
你的名字是: 李宽
>>>
相关文章
相关标签/搜索