转载请注明出处: http://blog.csdn.net/cxsydjn/article/details/71302732
The note introduces basic Python syntax and strings.
Python notes of open courses @Codecademy.
Python is a high level scripting language with object oriented features.
Python programs can be written using any text editor and should have the extension .py.
You may click to Python Basics for more details.
Operators and Maths
Add, subtract, multiply, divide numbers like +, -, *, /Exponentiation: **Modulo (which returns the remainder from a division): %
# Examples for `/` 5 / 2 # 2 5.0 / 2 # 2.5 float(5) / 2 # 2.5When you divide an integer by another integer, the result is always an integer (rounded down, if needed).
When you divide a float by an integer, the result is always a float.To divide two integers and end up with a float, you must first use float() to convert one of the integers to a float.External Resources
Python BasicsBuilt-in TypesString Formatting with %: The % operator after a string is used to combine a string with variables. The % operator will replace a %s in the string with the string variable that comes after it. E.g.,
# Example 1 string_1 = "you" string_2 = "me" print "I love %s! Do you love %s?" % (string_1, string_2) # Example 2 name = raw_input("Question1") quest = raw_input("Question2") color = raw_input("Question3") print "Answer to Q1 is %s, answer to Q2 is %s, " \ "and answer to Q3 is %s." % (A1, A2, A3)Date and Time
datetime: keeps track of time when something happened. datetime.now(): to retrieve the current date and time.Extracting Information: year, month, day, hour, minute, and second from datetime.now().Print as yyyy-mm-dd, mm/dd/yyyy and hh:mm:ss: see examples below
# Using the first row to import the **datetime library**. from datetime import datetime now = datetime.now() # Extracting Information current_year = now.year current_month = now.month current_day = now.day # print date as `yyyy-mm-dd` and `mm/dd/yyyy` print '%s-%s-%s' % (now.year, now.month, now.day) print '%s/%s/%s' % ( now.month, now.day, now.year) # print time as `hh:mm:ss` print '%s:%s:%s' % ( now.hour, now.minute, now.second)