03--Python 基本语法

2018-03-25  本文已影响0人  Roger田翔
@Author : Roger TX (425144880@qq.com)
@Link : https://github.com/paotong999

一、Python语法

任何一种编程语言都有自己的一套语法,编译器或者解释器就是负责把符合语法的程序代码转换成CPU能够执行的机器码,然后执行。Python作为一种计算机编程语言也不例外,Python的语法比较简单,采用缩进方式,写出来的代码就像下面的样子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-03-025 11:38:56

import os,re,pyperclip

class diffpwd():
    def diff(self,a):
        pwd1Reg = re.compile(r'[a-z]+')
        if re.search(pwd1Reg,a) == None or len(a) < 8:
            print("密码不符合规范")
            return None
        else:
            print("密码设置成功")
            return a

1、Python保留字

保留字即关键字,我们不能把它们用作任何标识符名称。Python 的标准库提供了一个 keyword 模块,可以输出当前版本的所有关键字:

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']

2、注释

3、行与缩进

total = item_one + \
        item_two + \
        item_three
total = ['item_one', 'item_two', 'item_three',
        'item_four', 'item_five']

4、空行

5、导入模块

在 Python 用 import 或者 from...import 来导入相应的模块。

6、大小写

Python程序是大小写敏感的,如果写错了大小写,程序会报错。

二、变量与常量

1、变量

a = 123 # a是整数
print(a)
a = 'ABC' # a变为字符串
print(a)

2、常量

PI = 3.14

3、赋值

4、数值大小

上一篇 下一篇

猜你喜欢

热点阅读