Skip to content

Instantly share code, notes, and snippets.

View Ventsislav-Yordanov's full-sized avatar

Ventsislav Yordanov Ventsislav-Yordanov

View GitHub Profile
@Ventsislav-Yordanov
Ventsislav-Yordanov / Subsetting Lists.py
Last active January 31, 2018 07:25
Indexing and List Slicing in Python
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"]
# 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"]
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]
@Ventsislav-Yordanov
Ventsislav-Yordanov / Function Declaration in Python.py
Last active January 27, 2018 04:23
Function that checks if the passed number is prime or not
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:
# 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 = [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
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
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
# import the "pyplot" submodule from the "matplotlib" package with alias "plt"
import matplotlib.pyplot as plt