basic Python 1(syntax&strings&dastetime)

  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 datetime
    print 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 datetime
now = datetime.now()

print '%s-%s-%s' % (now.year, now.month, now.day)
# will print: 2017-07-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值