5.1 print和import的更多信息

1. print()3.0之后print不再是语句,而是函数,

>>> print('udg',12,13)   udg 12 13

>>> name='yanliang'  >>> print(name)  yanliang

2. import 把某件事当做另外一件事导入

import somemodule

from somemodule import somefunction

from somemodule import somefunction1,somefunction2,somefunction3.。。。

from somemodule import *

(1)为导入的模块提供别名:>>> import math as foo  >>> foo.sqrt(4)   2.0

(2)也可以为函数提供别名:>>>from module import function as function1

5.2 赋值魔法

1. 序列解包

(1)多个赋值同时进行:>>> x,y,z=1,2,3  >>> print(x,y,z)  1 2 3

(2)>>> x,y=y,x  >>> print(x,y)  2 1

2. 链式赋值

(1)>>> x=y=2 相当于 >>> y=2  >>> x=y

3. 增量赋值

(1)>>> x+=3 类似于C++中的

5.3 语句块:缩排的乐趣

5.4 条件和条件语句

1. bool值是True配合False  ,bool()函数可以用来转换其它的值。

2. if语句

 name=input("what is your name!")
if name.endswith('liang'):
print('hello,yanliang')

3.else子句

 name=input("what is your name!")
if name.endswith('liang'):
print('hello,yanliang')
else:
print("hello stringer")

4. elseif

5. 嵌套代码块

6. 更复杂的条件

(1)比较运算符:例如x<y , x<=y ,0<x<100也可以等

(2)相等运算符:>>> "foo"=="foo"  True  >>> "foo"=="fod"  False

(3)is: 同一性运算符:

>>> x=y=[1,2,3]  >>> x is y  True  这里x,y被绑定到同一个对象上,所以具有同一性

>>> z=[1,2,3]  >>> z is y  False      z虽然与y是相等的但不是同一个对象所以不具有同一性

(4)in 成员资格运算符

(5)字符串和序列的比较:按照字母顺序排列

(6)bool运算符:and or not

 name=int(input("input the number"))
if name<10 and name>1:
print("OK")
else:
print("wrong")

(7)断言 assert 当不满足条件时直接崩溃退出

5.5 循环

1. while循环

 name=''
while not name:
name=input("please input your name")
print("hello %s !" % name)

2. for循环

 for number in range(10):
print(number)

3. 循环遍历字典元素

 d={"a":1,"b":2,"c":3}
for key in d:
print(key,"corrsponds to",d[key])

4. 一些迭代工具

(1)并行迭代:zip()可以将两个序列合成一个字典对应起来

 key1=['a','b','c']
value2=[1,2,3]
mapa=zip(key1,value2)
for name,age in mapa:
print(name,'is',age)

(2)编号迭代

将字符串数组中的包含‘yan’的自字符串全部替换成‘yanliang’

一种方法:

 strings=['jhsf','yansgd','gd']
print(strings,'\n')
index=0;
for string in strings:
if 'yan' in string:
strings[index]='yanliang'
index+=1
print(strings,'\n') 

第二种方法:采用enumerate函数 enumerate(strings)可以返回索引-值对

 strings=['jhsf','yansgd','gd']
print(strings,'\n')
index=0;
for index,string in enumerate(strings):
if 'yan' in string:
strings[index]='yanliang'
print(strings,'\n')

(3)翻转和排序迭代

sorted和reserved 返回排好序或翻转后的对象

sort和reserve 在原地进行排序或翻转

5. 跳出循环

(1)break

(2)continue

(3)while True  。。。break

6. 循环中的else 语句

5.6 列表推导式——轻量级的循环

>>> [x*x for x in range(10)]  [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

>>> [x*x for x in range(10) if x%3==0]  [0, 9, 36, 81]

5.7 pass del exec

pass: 程序什么也不做

del: 不仅会删除对象的引用也会删除对象的名称,但是那个指向的对象的值是没办法删除的

exec: 执行一个字符串语句

最好是为这个exec语句提供一个命名空间,可以放置变量的地方

eval:

>>> eval(input('please input the number \n'))

please input the number

1+2+3

6

Python基础教程笔记——第5章:条件,循环和其他语句的更多相关文章

  1. Python基础教程之第5章 条件, 循环和其它语句

    Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 #Chapter 5 条件, 循环 ...

  2. Python基础教程笔记——第7章:更加抽象(类)

    下面进入Python的面向对象: 对象的魔力: 多态:---可以对不同类的对象使用同样的操作 封装:---对外部隐藏对象内部的工作方式 继承:---以普通的类为基础建立专门的类对象 (1)多态: is ...

  3. Python基础教程笔记——第4章:字典

    字典 字典是Python唯一内建的数学映射类型,字典中的值没有特殊的顺序,键可以是数字,字符串,甚至是元组 字典的创建: 字典由键值对构成,字典中键是唯一的,而值不唯一.>>> a_ ...

  4. Python基础教程笔记——第2章:列表和元组

    python shell 里重复上一次的命令:Alt+p 2.3 列表:Python的苦力 (1)list函数 (2)列表赋值,不蹦蹦为一个元素不存在的位置赋值 (3)删除元素,del name[1] ...

  5. Python基础教程笔记——第1章

    1.8 函数 pow(x,y) x^y abs(x)          取数的绝对值 round(x)   会把浮点数四舍五入为最接近的整数 floor(x)     向下取整的函数,但是需要先imp ...

  6. Python基础教程笔记——第6章:抽象(函数)

    (1)计算裴波那契数列: fbis=[0,1] num=int(input("please input the number")) for i in range(num-2): f ...

  7. Python基础教程笔记——第3章:使用字符串

    字符串是不可修改的,标准序列操作(索引,分片,判断成员资格,求长度,取最大值 最小值)对字符串都是有效的. 格式化字符串,类似于C语言的输出是的感觉. >>> format=&quo ...

  8. python基础教程笔记—即时标记(详解)

    最近一直在学习python,语法部分差不多看完了,想写一写python基础教程后面的第一个项目.因为我在网上看到的别人的博客讲解都并不是特别详细,仅仅是贴一下代码,书上内容照搬一下,对于当时刚学习py ...

  9. python基础教程笔记—画幅好画(详解)

    今天写一下基础教程里面的第二个项目,主要使用python来做一个pdf的图,比较简单. 首先我们需要安装用到的模块pip install reportlab即可. 书上是用urlopen从往上下了一个 ...

随机推荐

  1. BZOJ1132: [POI2008]Tro(叉积 排序)

    题意 世上最良心题目描述qwq 平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和 N<=3000 Sol 直接模拟是$n^3$的. 考虑先枚举一个$i$,那么我们要算的就是$\sum_ ...

  2. 洛谷P2761 软件补丁问题(状压DP,SPFA)

    题意 描述不清... Sol 网络流24题里面怎么会有状压dp?? 真是狗血,不过还是简单吧. 直接用$f[sta]$表示当前状态为$sta$时的最小花费 转移的时候枚举一下哪一个补丁可以搞这个状态 ...

  3. java.lang.NoSuchMethodError: org.hibernate.cfg.Environment.verifyProperties

    我在使用jpa2+spring4+hibernate4 的时候,报错java.lang.NoSuchMethodError: org.hibernate.cfg.Environment.verifyP ...

  4. Java编译时根据调用该方法的类或对象所属的类决定

    class Base{     int x = 1;     static int y = 2; } class Subclass extends Base{     int x = 4;     i ...

  5. 关于dzzoffice 破解版

    最近看到很多人在搜索dzzoffice破解版,其实dzzoffie是一款全开源的产品,开放的功能是与演示站中一摸一样的,所以并不会有人破解这种全开源的系统.那么为什么会有人搜索这样的关键词呢? 可能大 ...

  6. 云原生技术图谱 (CNCF Landscape)

    转自:https://raw.githubusercontent.com/cncf/landscape/master/landscape/CloudNativeLandscape_latest.jpg

  7. iview 的 Carousel 走马灯 焦点图 不能用 建议换/vue-awesome-swiper

    https://www.npmjs.com/package/vue-awesome-swiper

  8. c++调用com口操作autocad

    #include "stdafx.h" #include <atlcomcli.h> #import "D:\\C++test\\FirstCom\\Rele ...

  9. bind的使用

    bind: 改变this的指向,返回一个新函数(它不会立即执行,需要调用新函数才能执行:apply call方法是立即执行) let obj = { name: 'jason888'} functio ...

  10. python基础一 day3 列表方法

    ls=['a','b','c','d','a','b','c','d']lst=['e','f','g','h']# 增加# ls.append('a') 将元素a添加至列表ls的尾部# ls.ext ...