Python学习3——for语句

# python code by zzw
# time:2015-7-27
# for语句注意与其他语言的区别,感觉跟shell有点像

words=[‘dog‘,‘windows‘,‘Linux‘];
for w in words:
    print(‘the length of ‘,w,‘ is ‘,len(w));

运行结果

the length of  dog  is  3
the length of  windows  is  7
the length of  Linux  is  5
# python code by zzw
# time:2015-7-27
# for语句注意与其他语言的区别,感觉跟shell有点像

for i in range(10):
    print(i);

运行结果

0
1
2
3
4
5
6
7
8
9
# python code by zzw
# time:2015-7-27
# for语句注意与其他语言的区别,感觉跟shell有点像
# 这里表示从0开始,步长为3,并且最后一个值小于20

for i in range(0,20,3):
    print(i);

运行结果:

0
3
6
9
12
15
18
时间: 2024-08-06 12:15:09

Python学习3——for语句的相关文章

Python学习笔记8—语句

条件语句 有的程序里写的是 /usr/bin Python,表示 Python 解释器在/usr/bin 里面.但是,如果写成 /usr/bin/env,则表示要通过系统搜索路径寻找 Python 解释器.不同系统,可能解释器的位置不同,所以这种方式能够让代码更将拥有可移植性. #/usr/bin/env python #coding=utf-8 number = int(raw_input("请输入任意一个整数:")) if number == 10: print "您输入

Python学习:if语句

先举例: age = 20 if age >= 18 : print 'Adult' elif age < 18 : print 'Nonage' 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= 18: print 'your age is', age print 'adult' 根据Python的缩进规则,如果if语句判断是True,就把缩进的两行pr

Python学习 之 switch语句

1.python并没有提供switch语句,python可以通过字典实现switch语句的功能,实现方法分为两步 —首先,定义一个字典 —其次,调用字典的get()获取相应的表达式 通过字典调用函数 {1:case1,2:case2}.get(o)(x,y,*args,**kwargs) 2.示例 from __future__ import division def jia(x,y): return x+y def jian(x,y): return x-y def cheng(x,y): r

python学习之结构语句

一 循环语句: 1.1 for x in rang(n) :#rang(n)生成左闭右开区间的序列 1.2 while x 条件n: 二条件语句: if 条件表达式: elif 表达式: elif 表达式: ... else: 三 break continu 四:函数: 4.1 定义:def func (参数列表): if not isinstance(x, (int, float)):#参数检查相当于断言 raise TypeError('bad operand type') return 参

Python学习笔记——基本语句

1.程序输出——print语句 >>> myString = 'Hello World!' >>> print myString Hello World! >>> myString 'Hello World!' 注意:在仅用变量名的时候,输出的字符串是用单引号括起来的 2.程序输入——raw_input()内建函数 >>> user = raw_input('Enter your name:') Enter your name:roo

Python学习笔记(语句)

print和import的更多信息 使用逗号输出 >>> print 'age:', 42age: 42 >>> 1,2,3(1, 2, 3)>>> print 1,2,31 2 3>>> print (1,2,3)(1, 2, 3) >>> name='Gumby'>>> salutation='Mr.'>>> greeting='Hello,' >>> pri

python学习之if语句

1. 判断输入的字符是否一致''' SIZE="hello" char=input("输入字符名称:") if char == SIZE:     print("字符名称%s"%char,"等于",SIZE) else:     print("请输入字符",SIZE) 2.判断登录验证用户名密码 user="desperado" pwd="123456" userna

python学习11——if 语句。

people=int(input("How many people are there in this city?")) cats = int(input("How many cats are there in this city?")) dogs = int(input("How many dogs are there in this city?")) if people < cats: print("Too many cats

python学习-7 条件语句 while循环 + 练习题

1.死循环 while 1 == 1: print('ok') 结果是一直循环 2.循环 count = 0 while count < 10: print(count) count = count +1 print(error) 3.练习题 ~ 使用while循环输出1 2 3 4 5 6   8 9 10 count = 1 while count <= 10 : # 或者count < 11 if count == 7: print( ) # 也可以添加pass,什么也不执行 el