602人阅读 评论(0) 收藏 举报
The Zen of Python
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!

Unit 1: Python Syntax

So far you've learned about the following in Python:
• Variables, which are ways to store values for later use;
• Data types (such as integers, floats, and booleans);
• Whitespace (and why it's significant!);
• Statements (and how Python statements are like statements in regular English);
• Comments (and why they're good for your code!); and
• Arithmetic operations (including+, -, *, /, **, and %).
# Assign the variable total on line 8!

meal = 44.50
tax = 0.0675
tip = 0.15

meal = meal + meal * tax
total = meal + meal * tip

print("%.2f" % total)

Unit 2: Strings & Console Output

• string assignment 
my_string = "Python is fun and I like it!"

• string function
length_of_my_string = len(my_string)
print my_string.upper()
print my_string.lower()

• covert a variable to string
my_birthday = 1994.09.22
print "1994.09.22"
print str(my_birthday)

•and  "+" in print
print "I " + "am " + "19 years" + "old"  

Date and time
Print out the date and time together in the form: mm/dd/yyyy hh:mm:ss
I write

now = datetime.now()
now = datetime.now()
print str(now.month) + "/" + str(now.day) + "/" + str(now.year) + " " + str(now.hour) + ":" + str(now.minute) + ":" +str(now.second)

and the out put is

I passed but not in the format
mm/dd/yyyy hh:mm:ss

so I tried every method to achieve this
first I tried
print str(now.month) + "/" + str(now.day) + "/" + str(now.year) + " " + "0" + str(now.hour) + ":" + str(now.minute) + ":" +str(now.second)

the silly "0"!
but it's so ugly and useless when the time is 2-digit.
I try and try and try but in vain.

so I visit the Q&A page and found this

Does anyone know how to get the date/time to format correctly when the month/day/hour/min is a SINGLE digit? BTW, I'm not talking about adding a "0" to my code, b/c that will obviously be wrong once we have double-digit number. Here is my code:

from datetime import datetime
now = datetime.now()

a = str(now.month) + "/" + str(now.day) + "/" + str(now.year)
b = str(now.hour) + ":" + str(now.minute) + ":" + str(now.second)
print a, b

Gets this answer (notice the "3", not "03"):
3/16/2013 22:15:53 
==> NoneOops, try again. 
Your printed date and time do not seem to be in the right format: mm/dd/yyyy hh:mm:ss

I think his code is beautiful as well as his quest and he also stack here.
the answer of someone 


First of all, because of how the submission correctness test of this exerciseworks, it'll pass if you print a+" "+b instead of printing a, b – so the correctness test does not enforce the two-digit notation.

That being said, if you want to format every number (except for the year, obviously) as a two-digit number with a leading zero for numbers below 10, you have (at least) three options:

  1. Reinvent the wheel (it's much more work than you actually need, but you can learn a lot in the process). Write a function, let's call it two_d (for "two digits"), that takes a number, converts it to a string (using str()), then checks its length (len()) and, if that equals 1, prepend a "0". Then you can call that method for every number: two_d(now.month) and so on.

  2. Use the old syntax for string formatting (format % values), as described here:

    "%02d/%02d/%d" % (3,17,2013)            #==> '03/17/2013'
  3. Use the new string format() method, as documented here:

    "{:02d}/{:02d}/{:4d}".format(3,17,2013) #==> '03/17/2013'

    As you can see, both notations are cryptic, but similar, and easy to learn once you've tried a few examples yourself. The % in the old syntax (and the {} in the new one) tells Python that a value is to be inserted at that point in the format string. What follows the % in the old syntax (or, is included in the braces with a: in the new one), is a format descriptor.

    If you look at the first one and read it right-to-left, it's pretty obvious what it says:02d means we're going to insert a decimal number (specifically, an integer,not a float) with 2 digits, and pad shorter numbers with 0s. For instance,"%03d"%7 yields "007" in the old syntax.

    As you may have noticed, the beauty of an (ugly-looking) formatter string is that it already contains all the separators you need between your values (/ or : in the date/time example), so you no longer have to do those annoying +"/"+string concatenations.

    his answer is quite reliable! and I tried the three method. but I have not learnt python function yet. 

    Unit 3: Control Flow

    From here on out, take for granted that each new course assumes knowledge of the material presented in the previous courses.
    Here's what I have learned in this unit:
    • Basics of control flow;
    • Comparators (such as >, <, and==);
    • Boolean operators (and, or, andnot);
    • And conditional statements (if,else, and elif).

C++学习笔记(一) 基础语法 —参考阿发你好

-###———————————————————————–阿发你好第2章输出指定宽度的数字#include <stdio.h> #include <stdlib.h> int main(){ printf("number is %d\n", 3); ...
  • David_Han008
  • David_Han008
  • 2016-10-29 10:35
  • 3042


Description   阿狸和桃子正在玩一个游戏,游戏是在一个带权图G=(V, E)上进行的,设节点权值为w(v),边权为c(e)。游戏规则是这样的:   1. 阿狸和桃子轮流将图中的顶点染色,阿狸会将顶点染成红色,桃子会将顶点染成粉色。已经被染过色的点不能再染了,而且每一...
  • sunshinezff
  • sunshinezff
  • 2016-04-06 15:45
  • 259

BZOJ_P2563 阿狸和桃子的游戏(贪心)

BZOJ传送门 Time Limit: 3 Sec Memory Limit: 128 MB Submit: 504 Solved: 359 [Submit][Status][Discuss] Description   阿狸和桃子正在玩一个游戏,游戏是在一个带权图G=(V, E)上进...
  • qq_18455665
  • qq_18455665
  • 2016-02-28 16:24
  • 275

【bzoj2563】 阿狸和桃子的游戏 贪心

贪心好题,表示不会做然后膜拜了一下PoPoQQQ的题解http://blog.csdn.net/popoqqq/article/details/44495319。 考虑每个点选对答案的贡献为w,不选的贡献为-w,每一条边两个端点都不选的贡献为-c,选一个端点的贡献为0,选两个端点的贡献为c,于是我...
  • u012288458
  • u012288458
  • 2015-08-04 11:03
  • 584


第5章 初步认识Linux CentOS6是如何启动的 1.内核引导 内核调用加载了init程序,至此内核引导的工作完成 2.运行init init进程是系统所有进程的起点 3.系统初始化 系统初始化,就是去执行/etc/init下的各个配置文件。 4.建立终端 显示一个文本登录界面,这个界面就是我...
  • u010819416
  • u010819416
  • 2016-06-29 20:53
  • 1366


  • annicybc
  • annicybc
  • 2007-12-10 23:50
  • 1074

BZOJ 2563 阿狸和桃子的游戏 贪心

题目大意:给定一张无向图,每个点有点权,每条边有边权,两个人轮流选择点,若一条边的两端点被选择则这条边被选择,两人都想自己的得分-对手的得分最大,求最终先手得分-后手得分 考虑先手选择每个点对答案的影响 一个点如果不选,本身对答案的贡献是-w 一个点如果选,本身对答案的贡献是w 一条边如果两...
  • PoPoQQQ
  • PoPoQQQ
  • 2015-03-20 18:07
  • 2208


我到了阿里地区,我走进了西藏,我看到了班公错, 那绿色的池塘,如果我家后院有这么个池塘我会把自己格一下重装系统变成水陆两栖的绿毛水怪,我家没有后院,所以也没有这么个池塘,所以我到现在还只是个城市病人而没有长出绿毛。绿水三分之一荡着中国,三分之二漾着印度由东向西渐由淡水过度到咸水,在湖里我第一次看到了...
  • cing
  • cing
  • 2005-03-03 07:34
  • 2109


很多人对热备,冷备,云备了解不深,我科普一下IT行业各种备份术语。以后别闹笑话了。 假设你是一位女性,你有一位男朋友,于此同时你和另外一位男生暧昧不清,比朋友 好,又不是恋人。你随时可以甩了现任男友,另外一位马上就能补上。这是冷备份。 假设你是一位女性,同时和两位男性在交往,两位都是你男朋友。并...
  • hynet
  • hynet
  • 2015-09-10 16:43
  • 12335


并查集   一、算法介绍: 并查集(Union-find Sets)是一种非常精巧而实用的数据结构,它主要用于处理一些不相交集合的合并问题。   并查集的基本操作有两个: 1:合并 union(x, y):把元素 x 和元素 y&...
  • hpuhjh
  • hpuhjh
  • 2015-08-21 14:49
  • 2597
    • 访问:73820次
    • 积分:1405
    • 等级:
    • 排名:千里之外
    • 原创:49篇
    • 转载:17篇
    • 译文:1篇
    • 评论:61条