Basic Python knowledge (1)

Source: Internet
Author: User
 
  1. # Starting the Hello world in python is quite simple.
  2. #! User/bin/ENV python3 # This line is shebang, used to indicate which interpreter is used for execution. If yes, it must be in the first line of the program
  3. Print ("Hello world! ") # Hello world!
 
  1. Print (123, "123") #123 123
  2. Print (1 + 2) #3
  3. Route = 866
  4. Print (route, type (route) #866 <class 'int'> type is used to obtain the data type
  5. Route = "North" # variables in python can be assigned at will without worrying about its type. Of course, it does not need to be declared.
  6. Print (route, type (route) # North <class 'str'>

 
  1. X = ["one", "two", "three", "four"]
  2. # Create a list object. I can understand it as an array.
  3. Print (X [0], X [1], X [3]) # One Two four
  4. # The list can be used like an array. The index is a set of consecutive numbers starting with 0.
  5. X. append ("five") # although it is similar to an array, it is indeed an object and can easily perform some operations
  6. Print (x) # ['one', 'two', 'three ', 'four', 'five']
  7. List. append (x, "Six") # You can also use the class to perform related operations.
  8. Print (x) # ['one', 'two', 'three ', 'four', 'five', 'six']
  9. X. insert (0, "zero") # other methods can be used. The first parameter is the index.
  10. Print (x) # ['zero ', 'one', 'two', 'three', 'four ', 'five', 'six']
  11. X. Remove ("six") # You can also delete related list items.
  12. X. Remove ("zero") # The parameter specifies the value to be deleted.
  13. Print (x) # ['one', 'two', 'three ', 'four', 'five']
  14. X [1] = 2 # You can still assign values to a specified list item like an array, and do not need to pay attention to its type.
  15. Print (x) # ['one', 2, 'three ', 'four', 'five']
 
  1. # Identity Operators
  2. A = ["one", 1, none]
  3. B = ["one", 1, none] # declare two identical lists
  4. Print (A is B) # False
  5. B = A # assign values to direct the two objects to the same object
  6. Print (A is B) # True
  7. # The is operator is the memory address to which two objects are referenced, so it is usually faster.
  8. A = "something"
  9. B = none
  10. Print (A is none, B is not none)
  11. # False false can also be compared with the same empty object none (it should be noted that python is case-sensitive none and none are different)
  12. # Comparison Operators
  13. A = 2
  14. B = 6
  15. Print (A = B, A <= B, A> = B,! = B, A> B, A <B)
  16. # False True False true
  17. # There is nothing to pay attention to. It is similar to any programming language.
  18. # Link comparison
  19. A = 5
  20. Print (0 <= A <= 10) # True
  21. # Comparison between computation and identity Computation
  22. A = "paths"
  23. B = "paths"
  24. Print (A is B) # False
  25. Print (A = B) # True
  26. # Here we can see the difference between the reference value
  27. # Member operators
  28. P = (4, "frog", 9,-33, 9, 2)
  29. # Create a list object that can also be enclosed in parentheses
  30. Print (2 in P) # True
  31. Print ("dog" not in P) # True
  32. # Logical operators
  33. Two = 2
  34. Five = 5
  35. Zero = 0
  36. Print (two and five) #5
  37. Print (five and two) #2
  38. Print (zero and five) #0
  39. Print (two and zero) #0
  40. Print (five or two) #5
  41. Print (two or five) #2
  42. Print (zero or five) #5
  43. # There is no description in the book, but if there are two non-zero numbers and, the return operator is followed
  44. # If there are two non-zero numbers or, the first one is returned.

Unfinished (to be continued)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.