Python Basic
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 assignmentmy_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
so I tried every method to achieve this
first I tried
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.
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
-
"%02d/%02d/%d" % (3,17,2013) #==> '03/17/2013'
-
"{:02d}/{:02d}/{:4d}".format(3,17,2013) #==> '03/17/2013'
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).