关闭

和阿热

486人阅读 评论(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).
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:71484次
    • 积分:1373
    • 等级:
    • 排名:千里之外
    • 原创:56篇
    • 转载:18篇
    • 译文:1篇
    • 评论:62条
    文章分类
    最新评论
    ggggg