全局函数:
print(字符串) 将字符串直接输出控制台上
input() 将用户的信息从控制台上读取到代码中
int()
str()
python的书写规范
每一行代码写完之后换行,\n换行符是python语句结束的标识符
注意:;也可以使用,但是不推荐
变量的命名规范:
标识符的命名规范:
标识符:程序中用来描述一些事物的名称
1、标识符只能由数字、大小写字母、_这三种有效符号组成,注意:Python中$不是有效符号
2、数字不能开头!!!
3、不能以关键字或者保留字作为标识符
4、不要使用系统已经在全局定义好的变量或者函数名称
5、建议标识符命名有意义
6、一般建议大家使用_或者小驼峰
findUserGroupByUsername
find_user_group_by_username[店家推荐]
7、特殊情况:
1、类名称使用大驼峰法
UserAddress
2、python没有专门用来定义常见的关键字,是使用变量,所有单词都是大写就是常量
PI = 3.14
NAME = "china"
TIME = "UTC
python的关键字:
import keyword
keyword.kwlist
['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的全局函数:
import builtins
dir(builtins)
'abs', 'aiter', 'all', 'anext', 'any', 'ascii', 'bin', 'bool', 'breakpoint',
'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex',
'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate',
'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr',
'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int',
'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals',
'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord',
'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed',
'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod',
'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip'
python数据类型
基本数据类型
数值型(number)
|-- int 整型
|-- float 浮点型
|-- complex 复数|虚数
布尔类型(bool)
True
False
字符串(str)
"字符串"
'字符串'
""" 字符串 """
''' 字符串 '''
复合数据类型
万物皆对象
list
set
tuple
dict
object
……
数据类型转换:
自动类型转换
整型 浮点型 布尔类型
强制类型转换
int(字符串)
float(字符串)
str(值)
import os
os.system("command")
常见的运算符
算术运算符:
+
-
*
/
// 整除
%
** 幂次方
关系运算符
>
<
>=
<=
== <>
!=
逻辑运算符
and
or
not
所属运算符
in
not in
is运算符
is运算符比较两个变量的内存地址
==比较两个变量的值
is
is not
赋值运算符
=
+= # a += 1 a = a + 1
-=
*=
/=
//=
%=
**=
注意:python没有自加和自减运算符,可以使用赋值运算符完成
三目运算符
像C、java、js等编程语言中:
变量 = 表达式 ? 值1 :值2
python的三目运算符
变量 = 值1 if 表达式 else 值2
变量
具有名称的内存空间
变量的命名规范问题:
标识符的命名规范:
标识符:变量、常量、函数名称、模块名称、类名称、对象名称……
1、标识符只能由数字、大小写字母、_三种有效符号组成
2、不能以数字开头
3、不能以关键字或者保留字
4、不建议使用全局已经定义过去标识符
5、标识符命名尽量有意义
6、多个单词组成时,建议使用驼峰法或者下划线法[官方推荐]
7、类名称建议使用大驼峰、常量所有单词全部大写
python的关键字
import keyword
keyword.kwlist
['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']
全局模块 builtins
import builtins
dir(builtins)
数据类型
基本数据类型:
数值型(number):
|-- 整型 int
|-- 浮点型 float
|-- 复数 complex
布尔类型(bool)
True
False
字符串(str)
'字符串'
"字符串"
""" 字符串 """
''' 字符串 '''
None 没有,无 C语言 NULL
复合数据类型:
万物皆对象
list
set
dict
tuple
object
……
类型转换问题:
自动类型转换
int float bool 布尔类型
强制类型转换
int(字符串)
float(字符串)
str(类型)
常见的运算符
算术运算符:
+
-
*
/
// 整除,地板除法
%
** 幂次方
关系运算符
>
<
>=
<=
==
!=
逻辑运算符
与 && and
或 || or
非 ! not
所属运算符
in
not in
is运算符
is python中==判断是两个变量的值,is判断是两个变量的地址
is not
赋值运算符
=
+= # a += 3 a = a + 3
-=
*=
/=
//=
**=
python没有自加和自减运算符
i++
++i
i--
--i
三目运算符
变量 = 表达式 ? 值1: 值2;
python的三目运算符:
变量 = 值1 if 表达式 else 值2