栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python学习笔记-语句

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python学习笔记-语句

python学习笔记(九)之语句1
print
python2中,print是一个语句,python3中它是一个函数。
实例1:

>> print "hello,world!"
hello,world!
>> print "hello","world!"
hello world!
说明:print语句中,字符串后面会接一个n符号,即换行!但是,如果要在一个字符串后面跟着逗号,那么换行就取消了,如下:
实例2:
>> for i in [1,2,3,4]:
...     print i
...
1
2
3
4
>> for i in [1,2,3,4]:
...     print i,
...
1 2 3 4

import
实例3:

>> import math
>> math.pow(3,2)
9.0
或者是:
>> from math import pow  ##适合引入模块较少的情况。如果引入模块多了,可读性就下降了,会不知道那个函数来自那个模块,如实例4
或>>> from math import *
>> pow(3,2)
9.0

实例4:

>> from math import pow as pingfang  ##对pow重命名,使用pingfang()就相当于在使用pow()
>> pingfang(3,2)
9.0

引入多个函数,标准做法:
实例5:

>> from math import pow,e,pi
>> pow(e,pi)   ##e是欧拉数;pi是π(pai)
23.140692632779263

赋值语句
a = 3 就是一个赋值语句
实例6:

>> x,y,z = "wtf",1,["love","you"]
>> x
'wtf'
>> y
1
>> z
['love', 'you']
实例7:
>> a
('love', 'datagrand')
>> type(a)

if--条件语句
实例8:

>> a = 4
>> if a != 5:   ##冒号是必须的,只有返回的是True,才会执行下面的语句
...     print a  ##要缩进四个空格
...
4

if...elif...else
基本样式结构:
if 条件1:
语句块1
elif 条件2:
语句块2
elif 条件3:
语句块3
。。。
else:
语句块4
实例9:

cat shiyan.py

#!/usr/bin/env python
#coding:utf-8
print "Please enter any integer number:"
##raw_input() The number entered is a string
##int() Convert the string to an integer
number = int(raw_input())
if number == 10:
print "The number you entered is:%d" %number
print "you are great!"
elif number > 10:
print "The number you entered is:{}".format(number)
print "The number is more than 10."
elif number < 10:
print "The number you entered is:{}".format(number)
print "The number is less than 10."
else:
print "are you a human?"

三元操作符
语法:a = b if c else d
(1)如果c为真,那么执行a = b
(2)如果c为假,那么执行a = d
实例10:

>> name = "wtf" if "didi" else "zhao"  ##if "didi"相当于if bool("didi")
>> name
'wtf'
>> name = "wtf" if "" else "zhao"  ## if ""相当于if bool("") ps:两个引号之间没有空格
>> name
'zhao'

for循环
语法:
for 循环规则:
操作语句
实例11:
for循环--字符串

>> hello = "world"
>> for i in hello:
...     print i
...
w
o
r
l
d

实例12:
for循环--列表

>> wtf_python = ["data","grand","welcome you",""]
>> wtf_python
['data', 'grand', 'welcome you', '']
>> for world in wtf_python:
...     print world
...
data
grand
welcome you

>>

实例13:
for循环--元组

>> c = ("wtf","datagrand")        
>> type(c)

>> for i in c:
...     print i
...
wtf
datagrand

实例14:
for循环--字典

>> d = {"wtf":"python","didi":"data"}
>> type(d)

>> for i in d:
...     print i
...
didi
wtf

数字是不能进行for循环的
因为int对象是不可迭代的,即for循环所应用的对象,应该是可迭代的。
实例15:
for循环--数字(不可for循环)

>> for i in 123:
...     print i
...
Traceback (most recent call last):
File "", line 1, in
TypeError: 'int' object is not iterable

判断一个对象是否可迭代的方法:
实例16:

>> import collections
>> isinstance(123,collections.Iterable)
False   ##不可迭代
>> isinstance([1,2,3],collections.Iterable)
True    ##可迭代

range
语法:
range(start,stop[,step])
语法说明:
start:开始数值,默认为0,也就是如果不写这项,就是认为start=0
stop:结束的数值,必须要写的。
step:变化的步长,默认是1,也就是不写,就是认为步长为1.坚决不能为0.
range()函数特点:
(1)这个函数可以创建一个数字元素组成的列表;
(2)常用于for循环
(3)函数的参数必须是整数,默认从0开始。
(4)step默认是1.如果不写,就是按照此值;
(5)step不能为零,如果等于零就报错;
(6)如果step是正数,返回列表的最后的值不包含stop值,即start+istep这个值小于stop;如果step是负数,start+istep的值大于stop。

实例17:

>> range(5)
[0, 1, 2, 3, 4]
>> range(0,5)
[0, 1, 2, 3, 4]
>> range(0,5,1)
[0, 1, 2, 3, 4]
>> range(1,5)
[1, 2, 3, 4]
>> range(1,5,2)
[1, 3]
>> range(0,-9,-1)
[0, -1, -2, -3, -4, -5, -6, -7, -8]
实例18:
找出小于100的能够被3整除的正整数

cat range.py

#!/usr/bin/env python
#coding:utf-8
wtf = []
for n in range(1,100):
if n % 3 == 0:
wtf.append(n)
print wtf
执行:

python range.py

[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/225593.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号