和阿热

原创 2013年12月03日 10:37:26
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).
版权声明:本文为博主原创文章,未经博主允许不得转载。

微信Tinker热修复Demo

  • 2017年11月09日 10:16
  • 45.77MB
  • 下载

深入探索Android热修复

  • 2017年11月20日 16:02
  • 26.1MB
  • 下载

名企热招 | 2018年名企内推计划启动,高薪名企聚集,免费内推!

在找工作的时候  大家都想去BAT   但BAT只要精英中的精英   然而,世界那么大    还有很多公司能给你更好的未来! 发展前景好 成...

android热修复Nuwa

  • 2017年10月24日 10:35
  • 6.17MB
  • 下载

Android热修复Tinker所需资源

  • 2017年11月01日 18:33
  • 1.95MB
  • 下载

Cocos2dx-- 资源热更新

前言:游戏上线后,我们常常还会需要更新,如新增玩法,活动等,这种动态的更新资源我们称为游戏的热更新。热更新一般只适用于脚本语言,因为脚本不需要编译,是一种解释性语言,而如C++语言是很难热更新的,其代...

基于JSPatch的热修复demo

  • 2017年02月08日 07:36
  • 142KB
  • 下载

RedHat5.9通过Keepalived双机热备Nginx

  • 2016年05月03日 23:18
  • 207KB
  • 下载

OpenStack+Ceph+热迁移+++一、前提条件

本次搭建我只写Ceph与Openstack的连接以及热迁移方法 准备openstack: 照着我的文章OpenStackMitaka版双节点搭建完成(1-9)的搭建(多加一台计算节点compute2设...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:和阿热
举报原因:
原因补充:

(最多只允许输入30个字)