知识内容:

1.运算符与表达式

2.for\while初步了解

3.常用内置函数

一、运算符与表达式

python与其他语言一样支持大多数算数运算符、关系运算符、逻辑运算符以及位运算符,并且有和大多数语言一样的运算符优先级。除此之外,还有一些是python独有的运算符。

1.算术运算符

a=10, b=20

2.比较运算符

a=10, b=20

 注:  在python3中不存在<>,只在python2中存在<>

3.赋值运算符

4.逻辑运算符

and两边条件都成立(或都为True)时返回True,而or两边条件只要有一个成立(或只要一个为True)时就返回True

not就是取反,原值为True,not返回False,原值为False,not返回True

 >>> a = 10
>>> b = 20
>>> a
10
>>> b
20
>>> a == 10 and b == 20
True
>>> a == 10 and b == 21
False
>>> a == 10 or b == 21
True
>>> a == 11 or b == 20
True
>>> a == 11 or b == 23
False
>>> a == 0
False
>>> not a == 0
True

5.成员运算符

6.身份运算符

7.位运算

8.运算符优先级

注:

(1)除法在python中有两种运算符: /和//,/在python2中为普通除法(地板除法),/在python3中为真除法,/和//在python2和python3中均为普通除法(地板除法)

示例:

(2) python中很多运算符有多重含义,在程序中运算符的具体含义取决于操作数的类型,将在后面继续介绍。eg: +是一个比较特殊的运算符,除了用于算术加法以外,还可以用于列表、元组、字符串的连接,但不支持不同类型的对象之间相加或连接;  *也是其中比较特殊的一个运算符,不仅可以用于数值乘法,还可以用于列表、字符串、元组等类型,当列表、字符串或元组等类型变量与整数就行*运算时,表示对内容进行重复并返回重复后的新对象

示例:

(3)python中的比较运算符可以连用,并且比较字符串和列表也可以用比较运算符

示例:

(4)python中没有C/C++中的++、--运算符,可以使用+=、-=来代替

 >>> a = 5
>>> a++
File "<stdin>", line 1
a++
^
SyntaxError: invalid syntax
>>> a += 1
>>> a
6
>>> a--
File "<stdin>", line 1
a--
^
SyntaxError: invalid syntax
>>> a-=1
>>> a
5

(5)python3中新增了一个新的矩阵相乘运算符@

示例:

 import numpy  # numpy是用于科学计算的python拓展库需要自行使用pip安装或手动安装

 x = numpy.ones(3)       # ones函数用于生成全1矩阵,参数表示矩阵大小
m = numpy.eye(3)*3 # eye函数用于生成单元矩阵
m[0, 2] = 5 # 设置矩阵上指定位置的值
m[2, 0] = 3
print(x @ m) # 输出结果: [6. 3. 8.]

9.表达式的概念

在python中单个任何类型的对象或常数属于合法表达式,使用上表中的运算符将变量、常量以及对象连接起来的任意组合也属于合法的表达式,请注意逗号(,)在python中不是运算符,而只是一个普通的分隔符

二、for\while初步了解

python中提供了for循环和while循环(注: 在Python中没有do..while循环)

1. for循环

for循环的格式:

for iterating_var in sequence:
statements(s)

2.while循环

while循环的格式:

while 判断条件:
执行语句……

示例:

 # for
# 输出从1到10的整数:
for i in range(1, 11): # range(1, 11)是产生从1到10的整数
print(i, end=' ')
print() # 换行 # while
# 计算1到100的和:
i = 0
number_sum = 0
while i < 100:
i = i + 1
number_sum = number_sum + i
print(number_sum) # 输出结果:
# 1 2 3 4 5 6 7 8 9 10
#

三、常用内置函数

1. 内置函数的概念

python中的内置函数不用导入任何模块即可直接使用,可以在命令行中使用dir(__builtins__)查看所有内置函数

查看所有内置函数:

 # __author__ = "wyb"
# date: 2018/3/7 for item in dir(__builtins__):
print(item)
# python3.6上运行的结果:
# ArithmeticError
# AssertionError
# AttributeError
# BaseException
# BlockingIOError
# BrokenPipeError
# BufferError
# BytesWarning
# ChildProcessError
# ConnectionAbortedError
# ConnectionError
# ConnectionRefusedError
# ConnectionResetError
# DeprecationWarning
# EOFError
# Ellipsis
# EnvironmentError
# Exception
# False
# FileExistsError
# FileNotFoundError
# FloatingPointError
# FutureWarning
# GeneratorExit
# IOError
# ImportError
# ImportWarning
# IndentationError
# IndexError
# InterruptedError
# IsADirectoryError
# KeyError
# KeyboardInterrupt
# LookupError
# MemoryError
# ModuleNotFoundError
# NameError
# None
# NotADirectoryError
# NotImplemented
# NotImplementedError
# OSError
# OverflowError
# PendingDeprecationWarning
# PermissionError
# ProcessLookupError
# RecursionError
# ReferenceError
# ResourceWarning
# RuntimeError
# RuntimeWarning
# StopAsyncIteration
# StopIteration
# SyntaxError
# SyntaxWarning
# SystemError
# SystemExit
# TabError
# TimeoutError
# True
# TypeError
# UnboundLocalError
# UnicodeDecodeError
# UnicodeEncodeError
# UnicodeError
# UnicodeTranslateError
# UnicodeWarning
# UserWarning
# ValueError
# Warning
# WindowsError
# ZeroDivisionError
# __build_class__
# __debug__
# __doc__
# __import__
# __loader__
# __name__
# __package__
# __spec__
# abs
# all
# any
# ascii
# bin
# bool
# 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

2.python中常见及常用的内置函数

注:

dir()函数可以查看指定模块中包含的所有成员或者指定对象类型所支持的操作;help()函数则返回指定模块或函数的说明文档

常用内置函数示例:

 # __author__ = "wyb"
# date: 2018/3/7 number = -3
print(abs(number)) # 输出结果: 3 print(all([0, -8, 3])) # 输出结果: False
print(any([0, -8, 3])) # 输出结果: True value = eval("3+2") # 计算字符串中表达式的值并返回
print(value) # 输出结果: 5 help(print) # 输出结果: print的文档(帮助信息) x = 3
y = float(x) # 将其他类型转换成浮点型
z = str(x) # 将其他类型转换成字符串类型
print(x, y, z) # 输出结果: 3 3.0 3
print(id(x), id(y), id(z)) # 输出x,y,z的地址信息 s = "Hello, python!"
n = len(s) # 求字符串的长度
print(n) # 输出结果: 14 p = pow(3, 2) # 求3的2次方
print(p) # 输出结果: 9 # zip(): 将可迭代的对象作为参数,将对象中对应的元素
# 打包成一个元组,然后返回这些元组组成的列表,实例如下:
a = [1, 2, 3]
b = [4, 5, 6]
zipped = zip(a, b) # zipped -> [(1,4),(2,5),(3,6)]
print(zipped)

python中的运算符及表达式及常用内置函数的更多相关文章

  1. Python中冷门但非常好用的内置函数

    Python中有许多内置函数,不像print.len那么广为人知,但它们的功能却异常强大,用好了可以大大提高代码效率,同时提升代码的简洁度,增强可阅读性 Counter collections在pyt ...

  2. python的学习笔记之——time模块常用内置函数

    1.Python time time()方法 Python time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数). time()方法语法: time.time() 举例: #! ...

  3. Python【map、reduce、filter】内置函数使用说明(转载)

    转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...

  4. Python【map、reduce、filter】内置函数使用说明

    题记 介绍下Python 中 map,reduce,和filter 内置函数的方法 一:map map(...) map(function, sequence[, sequence, ...]) -& ...

  5. Python常用模块中常用内置函数的具体介绍

    Python作为计算机语言中常用的语言,它具有十分强大的功能,但是你知道Python常用模块I的内置模块中常用内置函数都包括哪些具体的函数吗?以下的文章就是对Python常用模块I的内置模块的常用内置 ...

  6. python中常用内置函数和关键词

    Python 常用内置函数如下: Python 解释器内置了很多函数和类型,您可以在任何时候使用它们.以下按字母表顺序列出它们. 1. abs()函数 返回数字的绝对值. print( abs(-45 ...

  7. python之迭代器 生成器 枚举 常用内置函数 递归

    迭代器 迭代器对象:有__next__()方法的对象是迭代器对象,迭代器对象依赖__next__()方法进行依次取值 with open('text.txt','rb',) as f: res = f ...

  8. PYTHON语言之常用内置函数

    一 写在开头本文列举了一些常用的python内置函数.完整详细的python内置函数列表请参见python文档的Built-in Functions章节. 二 python常用内置函数请注意,有关内置 ...

  9. Python的常用内置函数介绍

    Python的常用内置函数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.取绝对值(abs) #!/usr/bin/env python #_*_coding:utf-8_ ...

随机推荐

  1. 【c++基础】如何获取工程项目当前路径

    工程项目当前路径 #include <direct.h> int main( ) { ]; _getcwd(buffer, ); std::cout << buffer < ...

  2. [LeetCode&Python] Problem 104. Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  3. .NET 中什么样的类是可使用 await 异步等待的?

    我们已经知道 Task 是可等待的,但是去看看 Task 类的实现,几乎找不到哪个基类.接口或者方法属性能够告诉我们与 await 相关. 而本文将探索什么样的类是可使用 await 异步等待的? D ...

  4. 有锁Iphone 3GS 6.0.1 降级刷到4.2.1 完美越狱+解锁

    2012-12-20 百度空间所写 前言:由于之前的3GS升级到6.0.1后会有重启之后无法打电话的情况,同学觉得这样很烦,搞得手机不像个手机了.但是这也是没办法的,毕竟是不完美越狱加软解锁的.于是, ...

  5. FutureTask的用法及两种常用的使用场景 + FutureTask的方法执行示意图

    from:  https://blog.csdn.net/linchunquan/article/details/22382487 FutureTask可用于异步获取执行结果或取消执行任务的场景.通过 ...

  6. YUV和RGB之间的转换方法

    yCbCr<-->rgb Y’ = 0.257*R' + 0.504*G' + 0.098*B' + 16 Cb Cr R) G) - 0.392*(Cb'-128) B) 参考: htt ...

  7. graphql-modules 企业级别的graphql server 工具

    graphql-modules 是一个新开源的graphql 工具,是基于apollo server 2.0 的扩展库,该团队 认为开发应该是模块化的. 几张来自官方团队的架构图可以参考,方便比较 a ...

  8. AtomicStampedReference、AtomicMarkableReference 区别

    AtomicMarkableReference 描述的是更加简单的是与否的关系,它的定义就是将数据变换为true 或 false,通常ABA问题只有两种状态,AtomicMarkableReferen ...

  9. webpack 的插件 DllPlugin 和 DllReferencePlugin

    在项目中,引入了比较多的第三方库,导致项目大,而每次修改,都不会去修改到这些库,构建却都要再打包这些库,浪费了不少时间.所以,把这些不常变动的第三方库都提取出来,下次 build 的时候不再构建这些库 ...

  10. LOJ 121 「离线可过」动态图连通性——LCT维护删除时间最大生成树 / 线段树分治

    题目:https://loj.ac/problem/121 离线,LCT维护删除时间最大生成树即可.注意没有被删的边的删除时间是 m+1 . 回收删掉的边的节点的话,空间就可以只开 n*2 了. #i ...