This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fruits = ["pineapple", "apple", "lemon", "strawberry", "orange", "kiwi"] | |
fruits[1] # apple | |
fruits[0] # "pineapple" | |
fruits[-1] # "kiwi" | |
fruits[5] # "kiwi" | |
fruits[-3] # "strawberry" | |
# List slicing | |
fruits[::] # ["pineapple", "apple", "lemon", "strawberry", "orange", "kiwi"] | |
fruits[0:2] # ["pineapple", "apple"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add values to a list | |
fruits.append("peach") | |
fruits # ["pineapple", "apple", "lemon", "strawberry", "orange", "kiwi", "peach"] | |
fruits = fruits + ["fig", "melon"] | |
fruits # ["pineapple", "apple", "lemon", "strawberry", "orange", "kiwi", "peach", "fig", "melon"] | |
# Change values from a list | |
fruits[0:2] = ["grape", "mango"] | |
fruits # ["grape", "mango", "lemon", "strawberry", "orange", "kiwi", "peach", "fig", "melon"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
numbers = [10, 42, 28, 420] | |
numbers_copy = numbers | |
numbers_copy[2] = 100 | |
numbers # [10, 42, 100, 420] | |
numbers_copy # [10, 42, 100, 420] | |
ratings = [4.5, 5.0, 3.5, 4.75, 4.00] | |
ratings_copy = ratings[:] | |
ratings_copy[0] = 2.0 | |
ratings # [4.5, 5.0, 3.5, 4.75, 4.0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_prime(n): | |
if n <= 1: | |
return False | |
elif n <= 3: | |
return True | |
elif n % 2 == 0 or n % 3 == 0: | |
return False | |
current_number = 5 | |
while current_number * current_number <= n: | |
if n % current_number == 0 or n % (current_number + 2) == 0: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# String methods | |
text = "Data Science" | |
text.upper() # "DATA SCIENCE" | |
text.lower() # "data science" | |
text.capitalize() # "Data science" | |
# Lists methods | |
numbers = [1, 4, 0, 2, 9, 9, 10] | |
numbers.reverse() | |
print(numbers) # [10, 9, 9, 2, 0, 4, 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
numbers = [10, 30, 55, 40, 8, 30] | |
text = "Data Science" | |
numbers.index(8) # 4 | |
text.index("a") # 1 | |
numbers.count(30) # 2 | |
text.count("i") # 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
numbers = numpy.array([3, 4, 20, 15, 7, 19, 0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np # np is an alias for the numpy package | |
numbers = np.array([3, 4, 20, 15, 7, 19, 0]) # works fine | |
numbers = numpy.array([3, 4, 20, 15, 7, 19, 0]) # NameError: name 'numpy' is not defined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numpy import array | |
numbers = array([3, 4, 20, 15, 7, 19, 0]) # works fine | |
numbers = numpy.array([3, 4, 20, 15, 7, 19, 0]) # NameError: name 'numpy' is not defined | |
type(numbers) # numpy.ndarray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import the "pyplot" submodule from the "matplotlib" package with alias "plt" | |
import matplotlib.pyplot as plt |
OlderNewer