basic Python 1(syntax&strings&dastetime)

来源:互联网 发布:淘宝售后客服术语大全 编辑:程序博客网 时间:2024/06/02 00:52
  1. A variable stores a piece of data, and gives it a specific name. You do not have to define the type. For example, a=5
  2. A boolean is like a light switch. It can only have two values. Just like a light switch can only be on or off, a boolean can only be True or False. Be aware that the first letter of the word is capitalized.
  3. You can change the value of a variable by “reassigning” it.
  4. In Python, whitespace is used to structure code. Whitespace is important, so you have to be careful with how you use it.
  5. IndentationError: expected an indented blockYou’ll get this error whenever your whitespace is off.
  6. The # sign is for comments. A comment is a line of text that Python won’t try to run as code. It’s just for humans to read.The # sign will only comment out a single line. While you could write a multi-line comment, starting each line with #, that can be a pain.
  7. For multi-line comments, you can include the whole block in a set of triple quotation marks:"""The day is so hot that
    people cannot live without
    airconditioning"""
  8. ** is used to represent exponent in Python. For example , the result of a in a=2**3 is 8.
  9. Modulo returns the remainder from a division. So, if you type 3 % 2, it will return 1, because 2 goes into 3 evenly once, with 1 left over.
  10. Strings need to be within quotes.
  11. Printing a number is as easy as writing print 1, then the console will print 1. Printing strings needs to include quotes, likeprint "Hello world!".
  12. We can use the backslash to fix the problem of 'in the code, for example'There\'s a snake in my boat.'
  13. Each character in a string is assigned a number. This number is called the index. c="cats"[0] In Python, we start counting the index from zero instead of one.
  14. String method len() gets the length (the number of characters) of a string.print len("Hello")
  15. You can use the lower() method to get rid of all the capitalization in your strings."Hello".lower()
  16. Make a string completely upper case"Hello".upper()
  17. The str() method turns non-strings into strings! str(2)
  18. Methods that use dot notation only work with strings.
  19. The + operator between strings will ‘add’ them together, one after the other. Combining strings together like this is called concatenation.print "Life is so "+"beautiful".
  20. Sometimes you need to combine a string with something that isn’t a string. In order to do that, you have to convert the non-string into a string.print "I have " + str(2) + " coconuts!"
  21. The % operator after a string is used to combine a string with variables. The % operator will replace a %sin the string with the string variable that comes after it. You need the same number of %s terms in a string as the number of variables in parentheses:print "The %s who %s %s!" % ("Knights", "say", "Ni")
  22. from datetime import datetimeprint datetime.now()

    The first line imports the datetime library so that we can use it. The second line will print out the current date and time.

  23. Print today’s date in the following format:

from datetime import datetimenow = datetime.now()print '%s-%s-%s' % (now.year, now.month, now.day)# will print: 2017-07-28
原创粉丝点击