year_of_birth = 1994
height_cm = 170.50
subject = "Data Science"
is_success = True
print(type(year_of_birth), type(height_cm), type(subject), type(is_success))
<class 'int'> <class 'float'> <class 'str'> <class 'bool'>
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"]
fruits[-2:-1] # ["orange"]
fruits[3:] # ["strawberry", "orange", "kiwi"]
fruits[:4] # ["pineapple", "apple", "lemon", "strawberry"]
fruits[:] # ["pineapple", "apple", "lemon", "strawberry", "orange", "kiwi"]
fruits[::-1] # ["kiwi", "orange", "strawberry", "lemon", "apple", "pineapple"]
fruits[::-2] # ["kiwi", "strawberry", "apple"]
fruits[::2] # ["pineapple", "lemon", "orange"]
# 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"]
# Delete values from a list
fruits.remove("mango")
fruits # ["grape", "lemon", "strawberry", "orange", "kiwi", "peach", "fig", "melon"]
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]
ratings_copy # [2.0, 5.0, 3.5, 4.75, 4.0]
characters = ["A", "B", "C"]
characters_copy = list(characters)
characters_copy[-1] = "D"
characters # ["A", "B", "C"]
characters_copy # ["A", "B", "D"]
movies = ["Ex Machina", "Mad Max: Fury Road", "1408"]
ratings = [7.7, 8.1, 6.8]
movie_choice_index = movies.index("Ex Machina")
print(ratings[movie_choice_index]) # 7.7
7.7
ratings = {
"Ex Machina": 7.7,
"Mad Max: Fury Road": 8.1,
"1408" : 6.8
}
print(ratings["Ex Machina"]) # 7.7
7.7
ratings["Deadpool"] = 8.0
print(ratings) # {'Ex Machina': 7.7, 'Mad Max: Fury Road': 8.1, '1408': 6.8, 'Deadpool': 8.0}
ratings["Ex Machina"] = 7.8
print(ratings) # {'Ex Machina': 7.8, 'Mad Max: Fury Road': 8.1, '1408': 6.8, 'Deadpool': 8.0}
del(ratings["1408"])
print(ratings) # {'Ex Machina': 7.8, 'Mad Max: Fury Road': 8.1, 'Deadpool': 8.0}
{'Ex Machina': 7.7, 'Mad Max: Fury Road': 8.1, '1408': 6.8, 'Deadpool': 8.0}
{'Ex Machina': 7.8, 'Mad Max: Fury Road': 8.1, '1408': 6.8, 'Deadpool': 8.0}
{'Ex Machina': 7.8, 'Mad Max: Fury Road': 8.1, 'Deadpool': 8.0}
print("Ex Machina" in ratings) # True
True
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:
return False
current_number = current_number + 6
return True
# 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]
numbers.sort()
print(numbers) # [0, 1, 2, 4, 9, 9, 10]
# Dictionaris methods
ratings = {
"Ex Machina": 7.7,
"Mad Max: Fury Road": 8.1,
"1408" : 6.8
}
print(ratings.keys()) # dict_keys(['Ex Machina', 'Mad Max: Fury Road', '1408'])
print(ratings.values()) # dict_values([7.7, 8.1, 6.8])
print(ratings.items()) # dict_items([('Ex Machina', 7.7), ('Mad Max: Fury Road', 8.1), ('1408', 6.8)])
[10, 9, 9, 2, 0, 4, 1]
[0, 1, 2, 4, 9, 9, 10]
dict_keys(['Ex Machina', 'Mad Max: Fury Road', '1408'])
dict_values([7.7, 8.1, 6.8])
dict_items([('Ex Machina', 7.7), ('Mad Max: Fury Road', 8.1), ('1408', 6.8)])
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") # 1dd
import numpy
numbers = numpy.array([3, 4, 20, 15, 7, 19, 0])
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
# import the "pyplot" submodule from the "matplotlib" package with alias "plt"
import matplotlib.pyplot as plt
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
np.array([False, 42, "Data Science"]) # array(["False", "42", "Data Science"], dtype="<U12")
np.array([False, 42], dtype = int) # array([ 0, 42])
np.array([False, 42, 53.99], dtype = float) # array([ 0. , 42. , 53.99])
# Invalid converting
np.array([False, 42, "Data Science"], dtype = float) # could not convert string to float: 'Data Science'
Execution Error
ValueError: could not convert string to float: 'Data Science'
Execution Error
ValueError: could not convert string to float: 'Data Science'
np.array([37, 48, 50]) + 1 # array([38, 49, 51])
np.array([20, 30, 40]) * 2 # array([40, 60, 80])
np.array([42, 10, 60]) / 2 # array([ 21., 5., 30.])
np.array([1, 2, 3]) * np.array([10, 20, 30]) # array([10, 40, 90])
np.array([1, 2, 3]) - np.array([10, 20, 30]) # array([ -9, -18, -27])
numbers = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
])
numbers[2, 1] # 8
numbers[-1, 0] # 10
numbers[0] # array([1, 2, 3])
numbers[:, 0] # array([ 1, 4, 7, 10])
numbers[0:3, 2] # array([3, 6, 9])
numbers[1:3, 1:3] # array([[5, 6],[8, 9]])
numbers = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12],
[13, 14, 15]
])
numbers.shape # (5, 3)
learning_hours = [1, 2, 6, 4, 10]
grades = [3, 4, 6, 5, 6]
np.mean(learning_hours) # 4.6
np.median(learning_hours) # 4.0
np.std(learning_hours) # 3.2
np.corrcoef(learning_hours, grades) # [[ 1. 0.88964891][ 0.88964891 1. ]]