知识内容:

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. SpringMVC开发小结

    1. 自动封装返回对象为JSON 1).在spring配置文件中添加如下配置: <mvc:annotation-driven> <mvc:message-converters> ...

  2. linux平台模拟生成CAN设备

    前言 使用socketCan的过程中有时候没有can接口设备,但是需要测试一下can接口程序是否有问题, 此时需要系统模拟生成can设备,本文介绍linux平台模拟生成CAN设备的方法. 实现步骤 1 ...

  3. css3动画与js动画的区别

    css与 js动画 优缺点比较   我们经常面临一个抉择:到底使用JavaScript还是CSS动画,下面做一下对比 JS动画 缺点:(1)JavaScript在浏览器的主线程中运行,而主线程中还有其 ...

  4. [HRBUST-1688]数论中的异或(思维题)

    数论中的异或 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 75(41 users) Total Accepted: 35(30 us ...

  5. WIN 10系统下,在DOS窗口输入Java或者javac出现乱码的解决方法

    昨天在WIN10系统下完成了Java环境的安装配置,配置完成后验证环境的时候出了一个小插曲—输入java后窗口内中文字符出现乱码,如下图. 在经过一番google之后,发现,原来是我cmd窗口的代码页 ...

  6. poj2387 最短路

    题意:给出一堆双向路,求从N点到1点的最短路径,最裸的最短路径,建完边之后直接跑dij或者spfa就行 dij: #include<stdio.h> #include<string. ...

  7. 设置tab标签页 遮挡部分

    效果如下: 主要代码: <div class="need-detail"> <div class="top-title"> <sp ...

  8. ES6 — 箭头函数

    一 为什么要有箭头函数 我们在日常开发中,可能会需要写类似下面的代码 const Person = { 'name': 'little bear', 'age': 18, 'sayHello': fu ...

  9. hasura graphql-engine v1.0.0-alpha26 版本新功能试用

      hasura graphql-engine v1.0.0-alpha26 已经发布了,有好多新的变动,测试使用docker 环境,同时pg 数据库使用了citus citus 是一个方便扩展的pg ...

  10. OASGraph 转换rest api graphql 试用

    创建rest api lb4 appdemo 参考提示即可 安装 OASGraph git clone https://github.com/strongloop/oasgraph.git cd oa ...