博客
关于我
phthon基本语法——温习
阅读量:382 次
发布时间:2019-03-05

本文共 1701 字,大约阅读时间需要 5 分钟。

Python基础语法总结

1. 注释

单行注释:#

多行注释:""""''''

2. 算数运算

+-*/// 整除 % 余数 **

注意:

  • 变量名在第一次出现时才被定义
  • 变量名=值

3. 数据类型

(1)数字型
  • bool:非零为真,0为假
  • int:整数
  • float:浮点数
  • complex:复数(a=complex(real, imag))
(2)非数字型
  • 字符串
  • 列表
  • 元组
  • 字典

注意:

  • 类型转换函数:int(), float(), complex()
  • 字符串拼接:+*

4. 变量命名

  • 标识符:字母、数字、下划线,不能以数字或下划线开头,不能为关键字
  • 命名规则:
  • 所有字母小写,中间用下划线分隔:hello_world
  • 小驼峰命名:helloWorld(首字母小写,后续首字母大写)
  • 大驼峰命名:HelloWorld(每个单词首字母大写)

5. if判断语句

(1)比较运算符

==, !=, >, <, >=, <=

(2)逻辑运算符

and, or, not

(3)if语句进阶
  • if-elif-else结构
  • 嵌套使用
(4)随机整数

import random

random.randint(a, b)

6. 循环

(1)while循环
  • 初始化条件设置
  • while条件:while 条件:
(2)for循环
  • 遍历列表、元组、字符串、字典
(3)break和continue
  • break:退出当前循环
  • continue:跳过当前循环
(4)循环嵌套
  • 使用print默认换行,end=""换行
(5)打印小矩形
row = 1while row <= 10:    col = 1    while col <= row:        print("*", end="")        col += 1    print("")    row += 1

7. 数据结构

(1)列表
  • 创建:[]
  • 方法:insert(), append(), extend(), del, remove(), pop(), clear(), count(), sort(), reverse()
  • 遍历:for循环
(2)元组
  • 创建:( )
  • 元素不可修改
  • 方法:index(), count(), len()
(3)字典
  • 创建:{}
  • 方法:pop(), update(), clear(), keys(), values(), items()
  • 遍历:for循环

8. 字符串

(1)字符串操作
  • 去掉空格:strip()
  • 查找替换:count(), replace()
  • 判断:isdecimal(), isdigit(), isnumeric(), isalpha(), isspace(), istitle()
  • 分割连接:split(), join()
  • 截取:[索引]
  • 大小写转换:lower(), upper(), capitalize(), title()
  • 文本对齐:ljust(), rjust(), center()
(2)字符串格式化
  • % 格式化
  • format()方法

9. 公共方法

(1)切片
  • 列表、字符串、元组
  • 拼接:* +
(2)in 和 not in
  • 元素存在判断

10. 变量进阶

(1)不可变类型
  • int, bool, float, complex, str, tuple
(2)可变类型
  • list, dict
(3)引用
  • id() 获取引用地址

11. 哈希

hash() 方法

12. 局部变量和全局变量

  • 局部变量:函数内创建,执行结束后回收
  • 全局变量:global声明

13. 函数

(1)函数定义
  • def语句
  • 返回值:return
(2)参数
  • 缺省参数:def demo(name, gender=True)
  • 多值参数:*args, **kwargs
  • 递归调用
(3)函数调用
  • 传递可变类型:num_list.append(88) 会修改外部数据

14. 元组和字典的拆包

  • 元组:*args
  • 字典:**kwargs

15. 技术文档撰写规范

转载地址:http://ufnwz.baihongyu.com/

你可能感兴趣的文章
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>