Learn python the hard way. python test program 2016.04.27

简介: 1 # this will not be printed in python ! 2 print "I could have code like this." # and the comment after is ignored 3 4 # You can also us...
  1 # this will not be printed in python !
  2 print "I could have code like this." # and the comment after is ignored
  3 
  4 # You can also use a comment to "disable" or comment out a piece of code:
  5 # print "This won't run. "
  6 
  7 print "This won't run. "
  8 
  9 print "I will now count my chickens:"
 10 
 11 print "Hens", 25 + 30 / 6
 12 print "Roosters", 100 - 25 * 3 % 4
 13 
 14 print "Now I will count the eggs:" 
 15 
 16 print 3+2+1-5+4%2-1/4+6
 17 print "Is it true that 3 + 2 < 5 - 7 ?"
 18 
 19 print "What is 3+2 ?", 3+2
 20 print "What is 5-7 ?", 5-7
 21 
 22 print "Oh, that's why it's False."
 23 
 24 print "How about Some more."
 25 
 26 print "Is it greater ?", 5 > -2 
 27 print "Is it greater or equal ?", 5 >= -2 
 28 print "Is it less or equal ?", 5 <= -2
 29 
 30 
 31 
 32 # Exercise 5: More Variable and Printing.
 33 my_name = 'Zed A. Shaw'
 34 my_age = 35 # not a lie
 35 my_height = 74 # inches
 36 my_weight = 180 # lbs
 37 my_eyes = 'Blue'
 38 my_teeth = 'White'
 39 my_hair = 'Brown'
 40 
 41 print "Let's talk about %s." % my_name
 42 print "He's %d inches tall." % my_height
 43 print "He's %d pounds heavy." % my_weight
 44 print "Actually that's not too heavy."
 45 print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
 46 print "His teeth are usually %s depending on the coffee." % my_teeth
 47 
 48 # this line is ticky, try to get it exactly right
 49 print "If I add %d, %d,  and %d I get %d." % (
 50     my_age, my_height, my_weight, my_age + my_height + my_weight)
 51 
 52 
 53 
 54 # Exercise 4: Variables and Names 
 55 cars = 100
 56 space_in_a_car = 4.0
 57 drivers = 30
 58 passengers = 90
 59 cars_not_driven = cars -drivers
 60 cars_driven = drivers
 61 carpool_capacity = cars_driven * space_in_a_car
 62 average_passengers_per_car = passengers / cars_driven
 63 
 64 print "There are", cars, "cars available."
 65 print "There are only", drivers, "drivers available"
 66 print "There will be", cars_not_driven, "empty cars today."
 67 print "We can transport", carpool_capacity, "people today."
 68 print "We have", passengers, "to carpool today."
 69 print "We need to put about", average_passengers_per_car, "in each car."
 70 
 71 
 72 
 73 x = "They are %d types of people." % 10
 74 binary = "binary"
 75 do_not = "don't"
 76 y = "Those who know %s and those who %s." % (binary, do_not)
 77 
 78 print x
 79 print y
 80 
 81 print "I said: %r." % x
 82 print "I also said: '%s'." % y
 83 
 84 hilarious = True
 85 joke_evaluation = "Isn't that joke so funny ?! %r" 
 86 
 87 print joke_evaluation % hilarious
 88 
 89 w = "This is the left side of ..."
 90 e = "a string with a right side."
 91 
 92 print w+e
 93 
 94 
 95 
 96 print "Mary had a little lamb."
 97 print "Its fleece was white as %s." % 'snow'
 98 print "And everywhere that Mary went."
 99 print "." * 10 # what'd that do ?
100 
101 end1 = "C"
102 end2 = "h"
103 end3 = "e"
104 end4 = "e"
105 end5 = "s"
106 end6 = "e"
107 end7 = "B"
108 end8 = "u"
109 end9 = "r"
110 end10 = "g"
111 end11 = "e"
112 end12 = "r"
113 
114 # watch that comma at the end. try removing it to see what happens 
115 print end1 + end2 + end3 + end4 + end5 + end6 ,  # use the , it output: Cheese Burger 
116 print end7 + end8 + end9 + end10 + end11 + end12 # remove the , it output: Cheese \\ Burger;


class Song(object):
def __init__(self, lyrics):
self.lyrics = lyrics

 
 

def sing_me_s_song(self):
for line in self.lyrics:
print line

 
 

happy_bday = Song(["Happy birthday to you",
"I don't want to get sued",
"So, I will stop right there"])

 
 

bulls_on_parade = Song(["There rally around the family",
"With pockets full of shells"])

 
 

happy_bday.sing_me_s_song()

 
 

bulls_on_parade.sing_me_s_song()

 

 

相关文章
|
12天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
12天前
|
程序员 开发者 Python
Python网络编程基础(Socket编程) 错误处理和异常处理的最佳实践
【4月更文挑战第11天】在网络编程中,错误处理和异常管理不仅是为了程序的健壮性,也是为了提供清晰的用户反馈以及优雅的故障恢复。在前面的章节中,我们讨论了如何使用`try-except`语句来处理网络错误。现在,我们将深入探讨错误处理和异常处理的最佳实践。
|
16天前
|
缓存 监控 Python
解密Python中的装饰器:优雅而强大的编程利器
Python中的装饰器是一种强大而又优雅的编程工具,它能够在不改变原有代码结构的情况下,为函数或类添加新的功能和行为。本文将深入解析Python装饰器的原理、用法和实际应用,帮助读者更好地理解和利用这一技术,提升代码的可维护性和可扩展性。
|
1月前
|
编译器 测试技术 C++
【Python 基础教程 01 全面介绍】 Python编程基础全攻略:一文掌握Python语法精髓,从C/C++ 角度学习Python的差异
【Python 基础教程 01 全面介绍】 Python编程基础全攻略:一文掌握Python语法精髓,从C/C++ 角度学习Python的差异
165 0
|
5天前
|
安全 数据处理 开发者
《Python 简易速速上手小册》第7章:高级 Python 编程(2024 最新版)
《Python 简易速速上手小册》第7章:高级 Python 编程(2024 最新版)
18 1
|
5天前
|
人工智能 数据挖掘 程序员
《Python 简易速速上手小册》第1章:Python 编程入门(2024 最新版)
《Python 简易速速上手小册》第1章:Python 编程入门(2024 最新版)
34 0
|
6天前
|
API Python
Python模块化编程:面试题深度解析
【4月更文挑战第14天】了解Python模块化编程对于构建大型项目至关重要,它涉及代码组织、复用和维护。本文深入探讨了模块、包、导入机制、命名空间和作用域等基础概念,并列举了面试中常见的模块导入混乱、不适当星号导入等问题,强调了避免循环依赖、合理使用`__init__.py`以及理解模块作用域的重要性。掌握这些知识将有助于在面试中自信应对模块化编程的相关挑战。
19 0
|
7天前
|
Python
Python金融应用编程:衍生品定价和套期保值的随机过程
Python金融应用编程:衍生品定价和套期保值的随机过程
14 0
|
7天前
|
Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
46 0
|
8天前
|
机器学习/深度学习 算法 定位技术
python中使用马尔可夫决策过程(MDP)动态编程来解决最短路径强化学习问题
python中使用马尔可夫决策过程(MDP)动态编程来解决最短路径强化学习问题
23 1