Python参考手册----第一章: Python简介(一)

本文介绍了Python编程的基础知识,包括变量、算术表达式、条件语句等,并深入探讨了循环、格式化输出和多行注释的使用。通过实例展示了如何在不同场景下灵活运用Python解决问题。
一、Python简介
1.1 print "Hello World"
1.2 变量和算术表达式
#print
principal = 1000
rate = 0.05
numyears = 5
year = 1

while year <= numyears:
    principal = principal * (1 + rate)

    print year, principal 
#    print(year, principal)

    print "%3d %0.2f" % (year, principal)
#    print("%3d %0.2f" % (year, principal))

    print format(year,"3d"), format(principal, "0.2f")
#    print(format(year,"3d"), format(principal,"0.2f"))

    print "{0:3d} {1:0.2f}".format(year,principal)
#    print("{0:3d} {1:0.2f}".format(year,principal))

    year += 1

#同一行上用分号隔开多条语句
#循环主体由缩进表示,每个缩进层次使用4个空格
#格式化字符序列
#%3d将一个整数格式化为在一个宽度为3的列中右对齐
1.3条件语句 a = 1 b = 2 if a < b: print "Computer says yes" else: print "Computer syas no" if a < b: pass # Do nothing else: print "Computer syas no" # or and not product = "game" type1 = "pirate memory" age = 9 if product == "game" and type1 == "pirate memory" \ and not (age < 4 or age > 8): print "I will take it!" else: print "no...." #switch case suffix = ".html" if suffix == ".html": content = "text/html" elif suffix == ".jpg": content = "image/jpeg" elif suffix == ".png": content = "image/png" else: raise RuntimeError("Unknown content type") print content #True False s = 'p' if 'spam' in s: has_spam = True else: has_spam = False print has_spam has_spam = "spam" in s print has_spam

转载于:https://www.cnblogs.com/liulipeng/archive/2012/11/29/2794135.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值