Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Assertion]
- assert condition
- you're telling the program to test that condition, and immediately trigger an error if the condition is false.
- In Python, it's roughly equivalent to this:
- if not condition:
- raise AssertionError()
- ==============================
- [Creating list]
- cubes = [i**3 for i in range(5)]
- squares = [i**2 for i in range(5)] # or remove the **n
- evens=[i**2 for i in range(10) if i**2 % 2 == 0]
- ==================
- [String Methods] #https://www.programiz.com/python-programming/methods/string
- msg = "Numbers: {0} {1} {2}". format(nums[0], nums[1], nums[2])
- print(", ".join(["spam", "eggs", "ham"]))
- #prints "spam, eggs, ham"
- print("Hello ME".replace("ME", "world"))
- #prints "Hello world"
- print("This is a sentence.".startswith("This"))
- # prints "True"
- print("This is a sentence.".endswith("sentence."))
- # prints "True"
- print("This is a sentence.".upper())
- # prints "THIS IS A SENTENCE."
- print("AN ALL CAPS SENTENCE".lower())
- #prints "an all caps sentence"
- print("spam, eggs, ham".split(", "))
- #prints "['spam', 'eggs', 'ham']"
- ==================
- [Other functions]
- nums = [55, 44, 33, 22, 11]
- if all([i > 5 for i in nums]):
- print("All larger than 5")
- if any([i % 2 == 0 for i in nums]):
- print("At least one is even")
- ==============
- for v in enumerate(nums):
- print(v)
- #(0, 55)
- #(1, 44)
- #(2, 33)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement