SHARE
TWEET

Python Explained

ahmedezzatpy Jan 21st, 2020 (edited) 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [Assertion]
  2.  
  3. assert condition
  4.  
  5. you're telling the program to test that condition, and immediately trigger an error if the condition is false.
  6.  
  7. In Python, it's roughly equivalent to this:
  8.  
  9. if not condition:
  10.     raise AssertionError()
  11.  
  12. ==============================
  13. [Creating list]
  14. cubes = [i**3 for i in range(5)]
  15. squares = [i**2 for i in range(5)] # or remove the **n
  16. evens=[i**2 for i in range(10) if i**2 % 2 == 0]
  17. ==================
  18. [String Methods] #https://www.programiz.com/python-programming/methods/string
  19. msg = "Numbers: {0} {1} {2}". format(nums[0], nums[1], nums[2])
  20.  
  21. print(", ".join(["spam", "eggs", "ham"]))
  22. #prints "spam, eggs, ham"
  23.  
  24. print("Hello ME".replace("ME", "world"))
  25. #prints "Hello world"
  26.  
  27. print("This is a sentence.".startswith("This"))
  28. # prints "True"
  29.  
  30. print("This is a sentence.".endswith("sentence."))
  31. # prints "True"
  32.  
  33. print("This is a sentence.".upper())
  34. # prints "THIS IS A SENTENCE."
  35.  
  36. print("AN ALL CAPS SENTENCE".lower())
  37. #prints "an all caps sentence"
  38.  
  39. print("spam, eggs, ham".split(", "))
  40. #prints "['spam', 'eggs', 'ham']"
  41. ==================
  42. [Other functions]
  43.  
  44. nums = [55, 44, 33, 22, 11]
  45.  
  46. if all([i > 5 for i in nums]):
  47.    print("All larger than 5")
  48.  
  49. if any([i % 2 == 0 for i in nums]):
  50.    print("At least one is even")
  51. ==============
  52. for v in enumerate(nums):
  53.    print(v)
  54. #(0, 55)
  55. #(1, 44)
  56. #(2, 33)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top