标签:usr user ber 布尔 迭代器 循环 表示 foreach 序列
内容
1. Python输入和输出
1.1 print语句和”Hello World!”
1.2 用户输入和内建函数raw_input()
1.3 格式化输出
2. 条件判断、循环和其他语句
2.1 if-else语句
2.2 while循环
2.3 for循环和range()内建函数
2.4 登录程序
一、Python输入和输出
1、print语句和”Hello World!”
linux-iojy:/usr/src/python # python Python 2.7.9 (default, Dec 21 2014, 11:02:59) [GCC] on linux2 Type "help", "copyright", "credits" or "license" for more information. 打印“Hello World!” >>> print ("Hello World!") Hello World!
我们可以把字符串赋值给变量str,再使用print来显示变量的内容:
>>> str = "Hello World!" >>> print (str) Hello World!
Python 的 print 语句,与字符串格式运算符( % )结合使用,可以实现字符串替换功能:
>>> name = "hmlinux" >>> print ("Hello, %s" % name) Hello, hmlinux
%s 表示由一个字符串来替换, 而%d 表示由一个整数来替换, 另外一个很常用的就是%f, 它表示由一个浮点数来替换。
2、用户输入和内建函数raw_input()
从用户那里得到数据输入的最容易的方法是使用 raw_input()内建函数。 它读取标准输入,并将读取到的数据赋值给指定的变量。 在Python3.0中使用input()函数。
>>> user = raw_input("Enter your name: ") Enter your name: hmlinux >>> print ("Your name is: ", user) (‘Your name is: ‘, ‘hmlinux‘) >>> print "Your name is: ", user Your name is: hmlinux >>> num = raw_input("Now enter a number: ") Now enter a number: 1024 >>> print "Doubling your number: %d" % (int(num)*2) Doubling your number: 2048
3、格式化输出程序实例
例 格式化输出程序(formatOutput.py)
#!/usr/bin/env python # _*_ coding:utf-8 _*_ # 格式化输出 name = raw_input("Enter your name: ") age = int(raw_input("Enter your age: ")) job = raw_input("Enter your job: ") salary = raw_input("Enter yuour salary: ") salary = float(salary) # 第一种方式 information = ‘‘‘ ------------------------ Name: %s Age: %d Job: %s Salary: %.2f ‘‘‘ % (name,age,job,salary) # 第二种方式 information2 = ‘‘‘ ------information of {0}------ Name: {0} Age: {1} Job: {2} Salary: {3} ‘‘‘.format(name,age,job,salary) print information print information2
执行结果:
# python formatOutput.py Enter your name: hmlinux Enter your age: 25 Enter your job: IT Enter yuour salary: 3000 ------------------------ Name: hmlinux Age: 25 Job: IT Salary: 3000.00
二、条件判断、循环和其他语句
1、if-else语句
标准if条件判断语句的语法格式如下:
if expression: if_suite
如果表达式的值非 0 或者为布尔值 True, 则代码组 if_suite 被执行; 否则就去执行下一条语句。
Python的else语句,语法如下:
if expression: if_suite else: else_suite
比如,判断成绩,根据分数打印及格或不及格:
>>> score = 68 >>> if score > 60: ... print "Good! pass" ... else: ... print "Sorry! No pass!" ... Good! pass
Python还支持elif(意指”else-if”)语句,语法如下:
if expression1: if_suite elif expression2: elif_suite ... else: else_suite
比如判断成绩的例子,现在需要根据分数划分3个等级,分别是不及格、及格、优秀:
>>> score = 81 >>> if score >= 80: ... print "good" ... elif score >= 60: ... print "passed" ... else: ... print "failed" ... good
2、while循环
标准 while 条件循环语句的语法类似 if。while循环根据表达式判断循环是否结束,只要条件表达式返回结果为真,循环就会一直执行。
while expression: while_suite
语句 while_suite 会被连续不断的循环执行, 直到表达式的值变成 0 或 False; 接着Python 会执行下一句代码。
>>> counter = 0 >>> while counter < 3: ... print "loop #%d" % (counter) ... counter = counter + 1 ... loop #0 loop #1 loop #2
比如,输入一个数字,让用户猜年龄大小,只有猜正确的时才结束循环:
>>> age = 25 >>> while True: ... guess_age = int(raw_input("Enter your age: ")) ... if guess_age == age: ... print "Yes" ... break ... elif guess_age > age: ... print "too bigger..." ... else: ... print "too smaller..." ... Enter your age: 33 too bigger... Enter your age: 10 too smaller... Enter your age: 29 too bigger... Enter your age: 25 Yes
打印1-9的平方数:
>>> n = 0 >>> while n < 10: ... print "%s * %s = " % (n,n), n ** 2 ... n += 1 ... 0 * 0 = 0 1 * 1 = 1 2 * 2 = 4 3 * 3 = 9 4 * 4 = 16 5 * 5 = 25 6 * 6 = 36 7 * 7 = 49 8 * 8 = 64 9 * 9 = 81
从0开始打印不大于N的整数:
>>> N = 10 >>> x = 0 >>> while x < N: ... print x, ... x += 1 ... 0 1 2 3 4 5 6 7 8 9 >>> count = 0 >>> while True: ... print("count: ", count) ... count += 1 ... if count == 10: ... break ... (‘count: ‘, 0) (‘count: ‘, 1) (‘count: ‘, 2) (‘count: ‘, 3) (‘count: ‘, 4) (‘count: ‘, 5) (‘count: ‘, 6) (‘count: ‘, 7) (‘count: ‘, 8) (‘count: ‘, 9)
利用while循环计算100以内奇数的和
>>> sum = 0 >>> x = 1 >>> while x < 100: ... sum = sum + x ... x = x + 2 ... >>> print sum 2500
3、for循环和range()内建函数
Python中的fo 循环与传统的for循环(计数器循环)不太一样,它更象shell脚本里的foreach迭代。Python中的for 接受可迭代对象(例如序列或迭代器)作为其参数,每次迭代其中一个元素。例如,遍历一个列表:
>>> list = [‘Blan‘, ‘Jone‘, ‘Bart‘] >>> for each in list: ... print each, ... Blan Jone Bart
遍历一个字典:
>>> dict = {‘a‘: 1, ‘b‘: 2, ‘c‘: 3} >>> for i in dict: ... print i, dict[i] ... a 1 c 3 b 2
遍历由range()内建函数生成的列表
>>> for i in range(3): ... print "loop:",i ... loop: 0 loop: 1 loop: 2
range()函数经常和len()函数一起用于字符串索引。 在这里我们要显示每一个元素及其索引值
>>> foo = ‘abc‘ >>> for i in range(len(foo)): ... print foo[i], ‘(%d)‘ % i ... a (0) b (1) c (2)
上面的例子还有用enumerate()函数实现:
>>> for i,ch in enumerate(foo): ... print ch, ‘(%d)‘ % i ... a (0) b (1) c (2)
for循环嵌套
>>> for i in range(3): ... print ‘-‘ * 20, i ... for j in range(5): ... print j ... -------------------- 0 0 1 2 3 4 -------------------- 1 0 1 2 3 4 -------------------- 2 0 1 2 3 4
4、用户登录程序
#!/usr/bin/env python import getpass _user = ‘hmlinux‘ _pass = ‘123456‘ username = raw_input("Username: ") password = getpass.getpass("Password: ") if username == _user and password == _pass: print "Welcome user %s login!" % username else: print "Invalid username or password!" # python userLogin.py Username: hmlinux Password: Welcome user hmlinux login! # python userLogin.py Username: hmlinux Password: Invalid username or password!
标签:usr user ber 布尔 迭代器 循环 表示 foreach 序列
原文地址:http://www.cnblogs.com/hmlinux/p/7682998.html